Arduino does not execute command from serial and slow to execute commands

Hello,

I am kinda of a noob in arduino, I have been playing with it for a few months or so, and I am running in some troubles here and there.

I am having a problem with an arduino project and I am trying to make the arduino make rgb light go on an off in different color upon user input. This sounds basic but unfortunately the arduino does not do it in some situations or is really slow to receive inputs from the user.

I can’t figure out if it’s a problem with the code or the arduino it self.

Here is the bit of code I think it’s the problem, and when trying this menu commands are executed once but then does not drop the loop and throw me back to main menu.

FIY: I am working with an Uno R3 ATMEGA 328

Mind that the code is really really long so this is only a bit of it, for the complete code look below.

    //Party  
      if(mode == "Party" || mode == "party" || mode == "Party mode" || mode == "party mode" || mode == "Party Mode" || mode == "party Mode" || mode == "c"){
        Serial.println("Party mode");
        Serial.println("For selecting another mode and go back please type back");
      int y;
     
      int w;
      Serial.println("How many times? note: 1 time = 20 seconds");
    while(Serial.available()==0){
      direct = Serial.readString();
      
      if(direct == "Back" || direct == "back" ){
        break;
      }
      
      y = 0;
      w = Serial.parseInt();
     
     if(y <= w){ 
    
    for(int i = 0; i < w; i = i+1){
      Serial.println("Initializing");
      Serial.println(w);
    
      for(int fadeValue = 0; fadeValue <= 250; fadeValue +=50){
       analogWrite(ledRR1, fadeValue);
       analogWrite(ledBR1, fadeValue);
       delay(500);
       analogWrite(ledGR1, fadeValue);
       analogWrite(ledGL1,fadeValue);
       analogWrite(ledBL1, 0);
       analogWrite(ledRR1,0);
       analogWrite(ledGL2,0);
       analogWrite(ledRL2,0);
       analogWrite(ledBL2,0); 
       analogWrite(ledGR2,0);
       analogWrite(ledRR2,0);
       analogWrite(ledBR2,0);
    
        
        delay(500);
      }
      analogWrite(ledRR1, 250);
      analogWrite(ledGR2, 250);
         analogWrite(ledBR1, 0);
         analogWrite(ledGR1, 0);
      analogWrite(ledBL1, 0);
         analogWrite(ledRL1,0);
       analogWrite(ledGL2,0);
       analogWrite(ledRL2,0);
       analogWrite(ledBL2,0); 
       analogWrite(ledGR2,0);
       analogWrite(ledRR2,0);
       analogWrite(ledBR2,0);
      delay(1000);
      analogWrite(ledBR1, 250); 
      analogWrite(ledRL1, 250);
      delay(500);
      analogWrite(ledGR1, 250);
      analogWrite(ledRR1, 250);
      analogWrite(ledBL1, 250);
      analogWrite(ledGL1, 150);
      analogWrite(ledRL1, 50);
      delay(1000);
          analogWrite(ledGR1, 0);
      analogWrite(ledRR1, 0);
      analogWrite(ledBL1, 0);
      analogWrite(ledGL1, 0);
      analogWrite(ledRL1, 0);
      delay(500);
        analogWrite(ledGR1, 250);
      analogWrite(ledRR1, 250);
      analogWrite(ledBL1, 250);
      analogWrite(ledGL1, 150);
      analogWrite(ledRL1, 50);
      delay(500);
          analogWrite(ledGR1, 0);
      analogWrite(ledRR1, 0);
      analogWrite(ledBL1, 0);
      analogWrite(ledGL1, 0);
      analogWrite(ledRL1, 0);
      delay(1000);
        analogWrite(ledGR1, 250);
      analogWrite(ledRR1, 250);
      analogWrite(ledBL1, 250);
      analogWrite(ledGL1, 150);
      analogWrite(ledRL1, 50);
      delay(2000);
      
      analogWrite(ledGL1,250);
      analogWrite(ledGR1, 250);
      delay(1000);
        analogWrite(ledGL1,0);
      analogWrite(ledGR1, 0);
      delay(500);
        analogWrite(ledRR1,250);
      analogWrite(ledRL1, 250);
      delay(1000);
        analogWrite(ledRR1,0);
      analogWrite(ledRL1, 0);
      delay(500);
        analogWrite(ledGL1,250);
      analogWrite(ledGR1, 250);
      analogWrite(ledBR1,250);
      analogWrite(ledBL1,250);
      delay(2000);
        analogWrite(ledGL1,0);
      analogWrite(ledGR1, 0);
      delay(1000);
      
        analogWrite(ledRR1, 0);
      analogWrite(ledBR1, 0);
      analogWrite(ledGR1, 0);
      analogWrite(ledGL1,0);
      analogWrite(ledBL1, 0);
      analogWrite(ledRL1,0);
      analogWrite(ledGL2,0);
      analogWrite(ledRL2,0);
      analogWrite(ledBL2,0); 
      analogWrite(ledGR2,0);
      analogWrite(ledRR2,0);
      analogWrite(ledBR2,0);
      
      y = y +1;
    }
     }
     else if(y >= w){
       Serial.println("Please type main to go back to main menu or select another value");
       break;
     }
    }
    
      }

As mentioned the code is really really long so look in the attachments for full code

Thank you!

Arduino_Bluethoot_Remade.ino (13.6 KB)

Try those two things, maybe they help:

  • Convert the "mode" variable and the "direct" variable to upper case (Reference) and get rid of this huge comparement. Now can check the serial data for just one word, in that case: if (mode.toUpperCase() == "PARTY") { ... }
  • Push all the strings you've got in Serial.println("something"); to the flash storage. You can do this by writing the command like that: Serial.println(F("something")); (Reference)

Working with a huge amount of strings on the Arduino can make it easily run out of RAM, so if you really need them, push them to the program storage. If you want to know how I handle that most of the time, feel free to visit my website.

Then you have to take care of the analogWrite() function. You haven't told us what board you're using, but if you use the Arduino UNO, you can't use analogWrite() right away on e.g. pin 4, coz it's just not an PWM pin (indicated by that ~ sign on the board). If you use the UNO, you can make your own PWM signal on non-PWM pins by reading that page here or just google for the right thing.

Hope this helped.

I can't figure out if it's a problem with the code or the arduino it self.

In case you haven't noticed, it is your code. The Arduino can execute sixteen million instructions per second.

deloarts: Try those two things, maybe they help:

  • Convert the "mode" variable and the "direct" variable to upper case (Reference) and get rid of this huge comparement. Now can check the serial data for just one word, in that case: if (mode.toUpperCase() == "PARTY") { ... }
  • Push all the strings you've got in Serial.println("something"); to the flash storage. You can do this by writing the command like that: Serial.println(F("something")); (Reference)

Working with a huge amount of strings on the Arduino can make it easily run out of RAM, so if you really need them, push them to the program storage. If you want to know how I handle that most of the time, feel free to visit my website.

Then you have to take care of the analogWrite() function. You haven't told us what board you're using, but if you use the Arduino UNO, you can't use analogWrite() right away on e.g. pin 4, coz it's just not an PWM pin (indicated by that ~ sign on the board). If you use the UNO, you can make your own PWM signal on non-PWM pins by reading that page here or just google for the right thing.

Hope this helped.

I will try this, thank you, although there is no pin 4 in my code. I used all analog pins marked with ~ and the analog ones.

Sorry about that this is a Uno R3 ATMEGA 328

Note the analogue pins are analogue input only. They can not be used with analogWrite.

deloarts: Try those two things, maybe they help:

  • Convert the "mode" variable and the "direct" variable to upper case (Reference) and get rid of this huge comparement. Now can check the serial data for just one word, in that case: if (mode.toUpperCase() == "PARTY") { ... }
  • Push all the strings you've got in Serial.println("something"); to the flash storage. You can do this by writing the command like that: Serial.println(F("something")); (Reference)

Working with a huge amount of strings on the Arduino can make it easily run out of RAM, so if you really need them, push them to the program storage. If you want to know how I handle that most of the time, feel free to visit my website.

Then you have to take care of the analogWrite() function. You haven't told us what board you're using, but if you use the Arduino UNO, you can't use analogWrite() right away on e.g. pin 4, coz it's just not an PWM pin (indicated by that ~ sign on the board). If you use the UNO, you can make your own PWM signal on non-PWM pins by reading that page here or just google for the right thing.

Hope this helped.

Ok this kinda worked, I am confused on the upper case, how would you implemented, I don't seem to find much resources out there other then for java (yes I know it's similar to arduino), but would you be kind to give me an toUpperCase example. Thx I know I am a noobie! Great website btw!

Here's the example for the upper case: https://www.arduino.cc/en/Tutorial/StringCaseChanges.

In your case, you wrote:

if(mode == "Party" || mode == "party" || mode == "Party mode" || mode == "party mode" || mode == "Party Mode" || mode == "party Mode" || mode == "c")
{
   bla;
}

But you can write it that way:

mode.toUpperCase();
if (mode == "PARTY")
{
    bla;
}

Or that way:

mode.toLowerCase();
if (mode == "party")
{
    bla;
}

But: The toUpperCase/toLowerCase is not the main problem, since the ATmega328 is powerful enough to handle your if ( ... ). It just looks better!

The main issue is for sure what I wrote and what Grumpy_Mike pointed out: You can't use the analog pins as analog output, they are only for analog input. Ok, you can actually use them as digital input AND digital output, but not as analog output with the standard Arduino command.

If you want to use all pins as PWM output pins you can do that by writing your own "analogWrite" function, which I already mentioned before.