Jump to content
43oh

ADC with MSP430


Recommended Posts

Hello everyone,

 

It turned out that in order to get a solid drive I needed a different gate resistor. Having solved that issue, a new issue came up and this one is actually software related. I'm mapping my input values which range from about 0 - 1580 into a range of 0 - 255. From there, I convert the RGB values that I receive into a HSV number range, adjust the V value, then reconvert the numbers to RGB and output it. 

 

The problem is that the output values will be sitting on the edge of the input ranges and swing up and down. The input value will only vary around 1 point after I take a large number of samples but that's enough for it to swing between 2 of the 255 output values. Here is my code :

byte PWM_PIN = P1_3;
byte PWM_PIN2 = P1_4;
byte PWM_PIN3 = P1_5;
byte rout = P8_3;
byte gout = P1_6;
byte bout = P1_7;
 
float pwm_value;
float pwm;
float pwm_value2;
float pwm2;
float pwm_value3;
float pwm3;

const int numReadings = 25;
const int numReadings2 = 25;

int readings[numReadings];      // the readings from the analog input
int readIndex = 0;              // the index of the current reading
float total = 0;                  // the running total
float average = 0;                // the average
int readings2[numReadings];      // the readings from the analog input
int readIndex2 = 0;              // the index of the current reading
float total2 = 0;                  // the running total
float average2 = 0;                // the average
int readings3[numReadings2];      // the readings from the analog input
int readIndex3 = 0;              // the index of the current reading
float total3 = 0;                  // the running total
float average3 = 0;                // the average
int readings4[numReadings];      // the readings from the analog input
int readIndex4 = 0;              // the index of the current reading
int total4 = 0;                  // the running total
float average4 = 0;                // the average
int readings5[numReadings];      // the readings from the analog input
int readIndex5 = 0;              // the index of the current reading
int total5 = 0;                  // the running total
float average5 = 0;                // the average
int readings6[numReadings2];      // the readings from the analog input
int readIndex6 = 0;              // the index of the current reading
int total6 = 0;                  // the running total
float average6 = 0;                // the average
double r;
double g;
double b;
double rd;
double gd;
double bd;
double h;
double s;
double v;
double hsv[2];
double max1;
double min1;
double max2;
double min2;
double max;
double min;
double v1;
double r1;
double r2;
double g1;
double g2;
double b1;
double b2;
double s1;
double r3;
double g3;
double b3;


void setup() {
pinMode(PWM_PIN, INPUT);
pinMode(PWM_PIN2, INPUT);
pinMode(PWM_PIN3, INPUT);
pinMode(rout, OUTPUT);
pinMode(gout, OUTPUT);
pinMode(bout, OUTPUT);
Serial.begin(9600);

//Setup for the 6 averaging functions
for (int thisReading = 0; thisReading < numReadings; thisReading++) {
     readings[thisReading] = 0;
   }
for (int thisReading2 = 0; thisReading2 < numReadings; thisReading2++) {
     readings2[thisReading2] = 0;
   }
for (int thisReading3 = 0; thisReading3 < numReadings2; thisReading3++) {
     readings3[thisReading3] = 0;
   }  

 for (int thisReading4 = 0; thisReading4 < numReadings; thisReading4++) {
     readings4[thisReading4] = 0;
   }
for (int thisReading5 = 0; thisReading5 < numReadings; thisReading5++) {
     readings5[thisReading5] = 0;
   }
for (int thisReading6 = 0; thisReading6 < numReadings2; thisReading6++) {
     readings6[thisReading6] = 0;
   }  
}
void loop() {
analogFrequency(10000);

//This section is mapping the signal and inverting it so that it is correct, it also makes it so max zeroes out properly. 
if (average == 0) {
   pwm = 0;  
}
else {
  pwm = map(average,1,1560,260,6);
}
if (average2 == 0) {
  pwm2 = 0;
 
}
else {
pwm2 = map(average2,1,1560,260, 6);
}
if (average3 == 0) {
  
 pwm3 = 0;  
}
else {
pwm3 = map(average3,1,1560,260,8);
}

average = constrain(average,0 , 1560);
average2 = constrain(average2,0 , 1560);
average3 = constrain(average3,0 , 1560);


r = pwm;
g = pwm2;
b = pwm3;



//This section converts the rgb values into HSV values
double rd = (double) r/255;
double gd = (double) g/255;
double bd = (double) b/255;
double max1 = max(gd, bd);
double max2 = max(rd, max1);
double min1 = min(gd, bd);
double min2 = min(rd, min1);

double h, s, v = max2;
double d = max2 - min2;

s = max2 == 0 ? 0 : d / max2; 

 
if (max2 == min2) {  
h = 0; // achromatic 
    } else { 
         if (max2 == rd) { 
             h = (gd - bd) / d + (gd < bd ? 6 : 0); 
         } else if (max2 == gd) { 
             h = (bd - rd) / d + 2; 
         } else if (max2 == bd) { 
             h = (rd - gd) / d + 4; 
         } 
         h /= 6; 
     } 
//This section adjusts the HSV values
s1=1.08+0.41*logf(s); 
v1 = v*32/100;
s1 = constrain(s1,0,1);

//This section takes the newly adjusted HSV values and converts them back to RGB

float r1, g1, b1;

    int i = int(h * 6);
    float f = h * 6 - i;
    float p = v1 * (1 - s1);
    float q = v1 * (1 - f * s1);
    float t = v1 * (1 - (1 - f) * s1);

    switch(i % 6){
        case 0: r1 = v1, g1 = t, b1 = p; break;
        case 1: r1 = q, g1= v1, b1 = p; break;
        case 2: r1 = p, g1 = v1, b1 = t; break;
        case 3: r1 = p, g1 = q, b1 = v1; break;
        case 4: r1 = t, g1 = p, b1 = v1; break;
        case 5: r1 = v1, g1 = p, b1 = q; break;
    }
 r2 = r1 * 255;
    g2 = g1 * 255;
    b2 = b1 * 255;
 











//Section for taking an average of the 3 inputs
total = total - readings[readIndex];
   readings[readIndex] = pulseIn(PWM_PIN, HIGH, 10000);
   total = total + readings[readIndex];
   readIndex = readIndex + 1;
   if (readIndex >= numReadings) {
     readIndex = 0;
   }
average = total / numReadings;


total2 = total2 - readings2[readIndex2];
   readings2[readIndex2] = pulseIn(PWM_PIN2, HIGH, 10000);
   total2 = total2 + readings2[readIndex2];
   readIndex2 = readIndex2 + 1;
   if (readIndex2 >= numReadings) {
     readIndex2 = 0;
   }
average2 = total2 / numReadings;


total3 = total3 - readings3[readIndex3];
   readings3[readIndex3] = pulseIn(PWM_PIN3, HIGH, 10000);
   total3 = total3 + readings3[readIndex3];
   readIndex3 = readIndex3 + 1;
   if (readIndex3 >= numReadings2) {
     readIndex3 = 0;
   }
average3 = total3 / numReadings2;
//Section for taking an average of new rgb values before output
total4 = total4 - readings4[readIndex4];
   readings4[readIndex4] = r2;
   total4 = total4 + readings4[readIndex4];
   readIndex4 = readIndex4 + 1;
   if (readIndex4 >= numReadings) {
     readIndex4 = 0;
   }
r3 = total4 / numReadings;


total5 = total5 - readings5[readIndex5];
   readings5[readIndex5] = g2;
   total5 = total5 + readings5[readIndex5];
   readIndex5 = readIndex5 + 1;
   if (readIndex5 >= numReadings) {
     readIndex5 = 0;
   }
g3 = total5 / numReadings;


total6 = total6 - readings6[readIndex6];
   readings6[readIndex6] = b2;
   total6 = total6 + readings6[readIndex6];
   readIndex6 = readIndex6 + 1;
   if (readIndex6 >= numReadings2) {
     readIndex6 = 0;
   }
b3 = total6 / numReadings2;




 

Serial.println("b1");
Serial.println(average);
Serial.println("b2");
Serial.println(average2);
Serial.println("b2");
Serial.println(average3);








analogWrite(rout, r3);

analogWrite(gout, g3 );

analogWrite(bout, b3);

//Serial.println("average3");
//Serial.println(;


}

I was also having an issue where in the final stage of calculation it would multiply 0.01 by 255 but it would give me 4.5 and vary to 3.8 without the input 0.01 changing. Why would the result change without the input changing? This was also making my output vary. I would also appreciate any pointers on how to execute code since I'm a novice.

 

Thanks,  

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...