0
\$\begingroup\$

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);
  }

enter image description here

\$\endgroup\$
4
  • \$\begingroup\$ what's the point of using if for most of the program and using case for the rest ... that's just messy programming \$\endgroup\$
    – jsotola
    Mar 22, 2022 at 3:09
  • \$\begingroup\$ I used if because I wanted the serial command response for each of those conditions just printed one time on the serial monitor. And the reason for using switch is because of repeating the function for an unlimited time, while I wouldn't do that with using if. I am still learning and I couldn't find a better solution. If you think there is a better solution for making the code more organized by using just one type of condition, I would be happy to share it with me. \$\endgroup\$
    – a2640
    Mar 22, 2022 at 15:30
  • \$\begingroup\$ the program flow was not clear when I looked at it the first time ... the code inside the if(Serial.available()>0 ) block should be indented one more level so that it is clear which code executes when if(Serial.available()>0 ) is true \$\endgroup\$
    – jsotola
    Mar 22, 2022 at 15:39
  • \$\begingroup\$ if(data == '2') and following ifs should be else if so that they do not waste time evaluating if one of them is true ... could be replaced by case \$\endgroup\$
    – jsotola
    Mar 22, 2022 at 15:41

1 Answer 1

3
\$\begingroup\$

The code of '8' is executed after code of '7' due to missing break statement in the switch-case construct.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.