-1

Ok, this is my code:

int rosso = 13;
int verde = 12;
int blu = 11;
int count = 0;
String command_on, command_off;
String s;
String enough = "stop";

void setup(){
  pinMode(rosso, OUTPUT);
  pinMode(verde, OUTPUT);
  pinMode(blu, OUTPUT);
  Serial.begin(9600);
  command_on = String("on");
  command_off = String("off");
}

void redToBlue(){
  if(count < 100) {   
  rgb(0,255,0);
  delay(100);
  rgb(0,0,0);
  delay(100);
  rgb(255,0,0);
  delay(100);
  rgb(0,0,0);
  count = count+1;  
  }
}

void switchOff(){
  rgb(0,0,0);
  delay(100);
}

boolean wait_command(){
  if(s == command_on){
    redToBlue();
  }
  if(s == command_off){
  switchOff();
  }
}

void accendi(){
  if(s == command_on){
  redToBlue();
  }

  if(s == command_off){
  switchOff();
  }
}

void rgb(int r, int g, int b){
  analogWrite(rosso, r);
  analogWrite(blu, b);
  analogWrite(verde, g);
}

void loop(){
 s = Serial.readString();
 Serial.println("What I have to do? ");
 wait_command();
}

On the serial monitor when i type "on" the function redToblue() must have loop 100 times, all that i got is that it loop only then stops. I think I have to change something inside loop() function and on redToBlue()...

0

I'm not sure exactly what you want, but if you expect after a single command on to have it looped 100 times, you need to call the redToBlue function in every loop (within wait_command, that checks for a boolean if the on command has been requested, and when so, call the redToBlue function. When the off command is set, you reset the boolean.

Also wait_command is not a good name, as it does not wait for a command, but processes it.

2
  • yes I would like that If I type "on" loop 100 times, and if I type "off" doesn't do nothing, and rgb led is off.
    – tommy
    Jun 12 '20 at 13:57
  • It will also stop before 100 if you type "off" when 100 loops are not reached yet. Jun 12 '20 at 14:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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