Servo servoR, servoG;
long red = 0, green = 0, blue = 0, tR = 0, tG = 0, tB = 0;
bool counterR = false, counterB = false, counterG = false;
int potentio, beltM, angV, redSen, greenSen, blueSen;
double v;
long timeR, timeG, timeB;
void setup()
{
Serial.begin(9600);
}
void loop()
{
pinMode(6,OUTPUT);
digitalWrite(6,HIGH);
delayMicroseconds(15);
digitalWrite(6,LOW);
pinMode(6,INPUT);
timeR = pulseIn(6,HIGH);
redSen = 0.0344*timeR/2;
//Serial.println(redSen);
//Serial.println(red);
pinMode(5,OUTPUT);
digitalWrite(5,HIGH);
delayMicroseconds(5);
digitalWrite(5,LOW);
pinMode(5,INPUT);
timeG = pulseIn(5,HIGH);
greenSen = 0.0344*timeG/2;
//Serial.println(greenSen);
//Serial.println(green);
pinMode(4,OUTPUT);
digitalWrite(4,HIGH);
delayMicroseconds(5);
digitalWrite(4,LOW);
pinMode(4,INPUT);
timeB = pulseIn(4,HIGH);
blueSen = 0.0344*timeB/2;
Serial.println(blueSen);
Serial.println(blue);
if (redSen <= 200 & counterR == false){
red++;
counterR = true;
}
if (counterR == true){
tR++;
}
if (tR == 10){
counterR = false;
tR = 0;
}
if (greenSen <= 200 & counterG == false){
green++;
counterG = true;
}
if (counterG == true){
tG++;
}
if (tG == 10){
counterG = false;
tG = 0;
}
if (blueSen <= 200 & counterB == false){
blue++;
counterB = true;
}
if (counterB == true){
tB++;
}
if (tB == 10){
counterB = false;
tB = 0;
}
}
This is my code for the ultrasound counting system. However, when all colours (red, green and blue) are added in the code, the serial printing messes up and prints the wrong values. However, if i put the code just for 2 colours, the system works perfectly. If there some kind of limit how many if statements i can add?