0

I use android to send the data to Arduino. when i send 0x02, LED turn on, and the breathing LED code are work. But when i send 0x03, it can not turn off the LED. i use UNO R3 the bluetooth is HC06

char data;
void setup()
{
  Serial.begin(9600);
  pinMode(9, OUTPUT);
}

void loop()
{

  if (Serial.available())
  {
    data = Serial.read();

  switch(data){

    case 0x02:             

      for (int a = 0 ; a < 255; a=a-1){  
        analogWrite(9, a);
        delay(10);
      }
      for (int a = 255; a>0; a=a-1){  
        analogWrite(9, a);  
        delay(10);  
      }  

      break;
    case 0x03 :  
      digitalWrite(9, LOW);
      break;
    default:
      break;
    }
  }
}
  • how is the LED connected? between 9 and Gnd or 9 and Vcc? – Jaromanda X Apr 5 '18 at 5:00
  • 2
    Is it really working? Spot the mistake: for (int a = 0 ; a < 255; a=a-1){. It should breathe once and then it'll stop. However it's hard to interrupt something, when you are busy with breathing – KIIV Apr 5 '18 at 5:27
  • LED connected between 9 and GND. How can i fix it, when i send 0x03 to let the LED stop – li lv Apr 5 '18 at 22:10
-2

this may be because of using digitalWrite in 0x03. Try using analogWrite(9, 0); instead.

  • i try, is not working – li lv Apr 5 '18 at 22:21
  • you can try printing it back on serial same char you receive. because like KIIV pointed out for loop will go on for a very long iteration this may be one of reason I am asking you to check whether Serail.read() executed or no by printing back the same char. Also modify for (int a = 0 ; a < 255; a=a-1){ to for (int a = 0 ; a < 255; a++){ – Devidas Apr 6 '18 at 6:29

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.