In this Arduino code, I want to when press numbers from 0 to 8, my LED s respond to it. Everything is working fine from 1 to 6 and 8. But when I select 7 and my code doesn't run well and Red and Green breathing interferes with each other. I don't understand why my 7th function doesn't work properly and it mixes to green ?!. I would appreciate you if you could help me to find out my code issue.
int brightness = 0; // how bright the 9 is
char data = 0; //Variable for storing received data
int fadeAmount = 5; // how many points to fade the 9 by
int pinSelector;
void setup()
{
Serial.begin(9600); //Sets the baud for serial data transmission
pinMode(10, OUTPUT); //Sets digital pin 3 as output pin
pinMode(9, OUTPUT);
pinMode(5, OUTPUT);
pinMode(3, OUTPUT);
}
void loop()
{
if(Serial.available()>0 ) // Send data only when you receive data:
{
data = Serial.read(); //Read the incoming data send via serial monitor & store into data
Serial.print(data); //Print Value inside data in Serial monitor
//Serial.print("\n");
Serial.println(); // print an empty line
if(data == '1') //Do looping so that when '1' send via serial monitor, the 9 can blink
{
digitalWrite(5, HIGH);
Serial.print("9 is On");
Serial.println();
}
if(data == '2')
{
digitalWrite(5, LOW); //If value is 0 then 9 turns OFF
Serial.print("9 is Off");
Serial.println();
}
if (data == '3')
{
analogWrite(9, 1);
analogWrite(3, 1);
analogWrite(10, 254); //If value is 0 then 9 turns OFF
Serial.print("Green");
Serial.println();
}
if (data == '4')
{
analogWrite(3, 1);
analogWrite(10, 1);
analogWrite(9, 254); //If value is 0 then 9 turns OFF
Serial.print("Red");
Serial.println();
}
if (data == '5')
{
analogWrite(10, 1);
analogWrite(9, 1);
analogWrite(3, 254); //If value is 0 then 9 turns OFF
Serial.print("Blue");
Serial.println();
}
if (data == '6')
{
analogWrite(3, 254);
analogWrite(10, 254);
analogWrite(9, 254); //If value is 0 then 9 turns OFF
Serial.print("White");
Serial.println();
}
}
switch(data) //Do looping so that when '1' send via serial monitor, the 9 can blink
{
case '7' :
analogWrite(3, 1);
analogWrite(10, 1);
analogWrite(9, 1);
pinSelector = 9;
fade();
case '8' :
analogWrite(3, 1);
analogWrite(10, 1);
analogWrite(9, 1);
pinSelector = 10;
fade();
}
}
void fade()
{
analogWrite(pinSelector, brightness);
// change the brightness for next time through the loop:
brightness = brightness + fadeAmount;
// reverse the direction of the fading at the ends of the fade:
if (brightness <= 0 || brightness >= 255) {
fadeAmount = -fadeAmount;
}
// wait for 30 milliseconds to see the dimming effect
delay(30);
}
if
for most of the program and usingcase
for the rest ... that's just messy programming \$\endgroup\$if(Serial.available()>0 )
block should be indented one more level so that it is clear which code executes whenif(Serial.available()>0 )
is true \$\endgroup\$if(data == '2')
and followingif
s should beelse if
so that they do not waste time evaluating if one of them is true ... could be replaced bycase
\$\endgroup\$break;
\$\endgroup\$