RGB with FSR

Not to sure what is wrong with the code, it is reading the FSR values fine but for some reason it is not changing the colour of the LED. The LEDs I have are Piranha LED/RGB.

 /* project 1 - analog in/analog out */


int sensPin = 2;    // set the touch sensor (analog) input pin on Arduino

int bPin = 9;   // select the pin for the RGB LED & LED array
int gPin = 10;
int rPin = 11;

int val = 0;       // variable to store the value coming from the sensors

int B = 0;         // variables for output
int G = 0;
int R = 0;


int inB = 0;        //variables to prevent the code from being super buggy
int inG = 0;
int inR = 0;


void setup() {
 pinMode(bPin, OUTPUT);  // declare the led pins as OUTPUT
 pinMode(gPin, OUTPUT);
 pinMode(rPin, OUTPUT);


 Serial.begin(9600);     // diagnostic printout
}

void loop() {
 val = analogRead(sensPin);  // read the value from the sensor, set to diagnostic variable

 if(inB <= 340)                // BLUE LED
 B = 0;
 if(inB > 340 && inB <= 510)
 B = map(inB, 341, 510, 0, 255);
 if(inB > 510 && inB <= 850)
 B = 255;
 if(inB > 850)
 B = map(inB, 851, 1023, 255, 0);

 if(inG <= 170)                // GREEN LED
 G = map(inG, 0, 170, 0, 255);
 if(inG > 170 && inG <= 510)
 G = 255;
 if(inG > 510 && inG <= 680)
 G = map(inG, 511, 680, 255, 0);
 if(inG > 680)
 G = 0;

 if(inR <= 170)                 // RED LED
 R = 255;
 if(inR > 170 && inR <= 340)
 R = map(inR, 171, 340, 255, 0);
 if(inR > 340 && inR <= 680)
 R = 0;
 if(inR > 680 && inR <= 850)
 R = map(inR, 681, 850, 0, 255);
 if(inR > 850)
 R = 255;

 analogWrite(bPin, B);    // output RGB values to RGB pins
 analogWrite(gPin, G);
 analogWrite(rPin, R);

 Serial.println(val);  //diagnostic printout
}
 if(inB <= 340)                // BLUE LED
 B = 0;
 if(inB > 340 && inB <= 510)
 B = map(inB, 341, 510, 0, 255);
 if(inB > 510 && inB <= 850)
 B = 255;
 if(inB > 850)
 B = map(inB, 851, 1023, 255, 0);

You're missing a bunch of elses.

 Serial.println(val);  //diagnostic printout

Why aren't you printing R, G, and B, too?

The guy here has a similar problem, similar code and a very same IP address.
Hmm.

Edit. And the same habit of asking the same question across different parts of the forum.(don't worry, I've deleted the other one now)

Well i'm new with this and below is the original code that I had taken but it some code for others part that I didn't want, so I tried to change a few things, but all I want is for the FSR to change the colour of the RGB.

Any tips on what I need to change?

Many thanks

 /* project 1 - analog in/analog out */

int potPin = 2;    // select the input pin for the potentiometer (powered 5V)
int flexPin = 0;   // select the input pin for the FSR (powered 3.3V)

int bPin = 11;   // select the pin for the RGB LED & LED array
int gPin = 10;
int rPin = 9;
int aPin1 = 3;
int aPin2 = 5;
int aPin3 = 6;

int val = 0;       // variable to store the value coming from the sensors

int B = 0;         // variables for output
int G = 0;
int R = 0;
int a1 = 0;
int a2 = 0;
int a3 = 0;

int inB = 0;        //variables to prevent the code from being super buggy
int inG = 0;
int inR = 0;
int in1 = 0;
int in2 = 0;
int in3 = 0;

void setup() {
 pinMode(bPin, OUTPUT);  // declare the led pins as OUTPUT
 pinMode(gPin, OUTPUT);
 pinMode(rPin, OUTPUT);

 pinMode(aPin1, OUTPUT);
 pinMode(aPin2, OUTPUT);
 pinMode(aPin3, OUTPUT);

 Serial.begin(9600);     // diagnostic printout
}

void loop() {
 val = analogRead(flexPin);  // read the value from the sensor, set to diagnostic variable

 in1 = analogRead(flexPin);
 in2 = analogRead(flexPin);
 in3 = analogRead(flexPin);

 if(in1 <= 250)              // LED 1 on array
 a1 = 0;
 if(in1 > 250 && in1 <= 375)
 a1 = map(in1, 251, 375, 0, 255);
 if(in1 > 375 && in1 <= 625)
 a1 = 255;
 if(in1 > 625)
 a1 = map(in1, 626, 750, 255, 0);

 if(in2 <= 125)              // LED 2 on array
 a2 = map(in2, 0, 125, 0, 255);
 if(in2 > 125 && in2 <= 375)
 a2 = 255;
 if(in2 > 375 && in2 <= 500)
 a2 = map(in2, 376, 500, 255, 0);
 if(in2 > 500)
 a2 = 0;

 if(in3 <= 125)              // LED 3 on array
 a3 = 255;
 if(in3 > 125 && in2 <= 250)
 a3 = map(in3, 126, 250, 255, 0);
 if(in3 > 250 && in3 <= 500)
 a3 = 0;
 if(in3 > 500 && in3 <= 625)
 a3 = map(in3, 501, 625, 0, 255);
 if(in3 > 625)
 a3 = 255;

 analogWrite(aPin1, a1);
 analogWrite(aPin2, a2);
 analogWrite(aPin3, a3);

 inB = analogRead(potPin);    // set value to RGB variables
 inG = analogRead(potPin);
 inR = analogRead(potPin);

 if(inB <= 340)                // BLUE LED
 B = 0;
 if(inB > 340 && inB <= 510)
 B = map(inB, 341, 510, 0, 255);
 if(inB > 510 && inB <= 850)
 B = 255;
 if(inB > 850)
 B = map(inB, 851, 1023, 255, 0);

 if(inG <= 170)                // GREEN LED
 G = map(inG, 0, 170, 0, 255);
 if(inG > 170 && inG <= 510)
 G = 255;
 if(inG > 510 && inG <= 680)
 G = map(inG, 511, 680, 255, 0);
 if(inG > 680)
 G = 0;

 if(inR <= 170)                 // RED LED
 R = 255;
 if(inR > 170 && inR <= 340)
 R = map(inR, 171, 340, 255, 0);
 if(inR > 340 && inR <= 680)
 R = 0;
 if(inR > 680 && inR <= 850)
 R = map(inR, 681, 850, 0, 255);
 if(inR > 850)
 R = 255;

 analogWrite(bPin, B);    // output RGB values to RGB pins
 analogWrite(gPin, G);
 analogWrite(rPin, R);

 Serial.println(val);  //diagnostic printout
}

As PaulS has suggested, add a bunch of debug prints.

Might be a stupid question but what are debug prints ?

cheers

Might be a stupid question but what are debug prints ?

 Serial.println(val);  //diagnostic printout

Hi, im using an FSR (force sensitive resistor) to change the colour of the RGB. I have got it working with the code below, but it doesn't really change smoothly. Any suggestions?

/* project 1 - analog in/analog out */

int potPin = 2;    // select the input pin for the potentiometer (powered 5V)
int flexPin = 0;   // select the input pin for the FSR (powered 3.3V)

int bPin = 11;   // select the pin for the RGB LED & LED array
int gPin = 10;
int rPin = 9;

int val = 0;       // variable to store the value coming from the sensors

int B = 0;         // variables for output
int G = 0;
int R = 0;


int inB = 0;        //variables to prevent the code from being super buggy
int inG = 0;
int inR = 0;
int in1 = 0;
int in2 = 0;
int in3 = 0;

void setup() {
 pinMode(bPin, OUTPUT);  // declare the led pins as OUTPUT
 pinMode(gPin, OUTPUT);
 pinMode(rPin, OUTPUT);

 Serial.begin(9600);     // diagnostic printout
}

void loop() {
 val = analogRead(flexPin);  // read the value from the sensor, set to diagnostic variable

 in1 = analogRead(flexPin);
 in2 = analogRead(flexPin);
 in3 = analogRead(flexPin);



 inB = analogRead(potPin);    // set value to RGB variables
 inG = analogRead(potPin);
 inR = analogRead(potPin);

 if(inB <= 340)                // BLUE LED
 B = 0;
 if(inB > 340 && inB <= 510)
 B = map(inB, 341, 510, 0, 255);
 if(inB > 510 && inB <= 850)
 B = 255;
 if(inB > 850)
 B = map(inB, 851, 1023, 255, 0);

 if(inG <= 170)                // GREEN LED
 G = map(inG, 0, 170, 0, 255);
 if(inG > 170 && inG <= 510)
 G = 255;
 if(inG > 510 && inG <= 680)
 G = map(inG, 511, 680, 255, 0);
 if(inG > 680)
 G = 0;

 if(inR <= 170)                 // RED LED
 R = 255;
 if(inR > 170 && inR <= 340)
 R = map(inR, 171, 340, 255, 0);
 if(inR > 340 && inR <= 680)
 R = 0;
 if(inR > 680 && inR <= 850)
 R = map(inR, 681, 850, 0, 255);
 if(inR > 850)
 R = 255;

 analogWrite(bPin, B);    // output RGB values to RGB pins
 analogWrite(gPin, G);
 analogWrite(rPin, R);

}

Any suggestions?

I suggest you quit repeatedly starting new threads/identities for the same subject