-1
void setup()
{
Serial.begin(9600);
pinMode(13,OUTPUT);
}
void loop() {
if(Serial.available()>0)
{s_num =Serial.read();}
if(s_num=='0'){
digitalWrite(13,HIGH);
delay(100); //the specified time for 0
digitalWrite(13,LOW);
delay(100);}
if(s_num=='1'){
digitalWrite(13,HIGH);
delay(200);
digitalWrite(13,LOW);
delay(200);}
if(s_num=='2'){
digitalWrite(13,HIGH);
delay(300);
digitalWrite(13,LOW);
delay(300);}
char s_num; if(s_num=='3'){
digitalWrite(13,HIGH);
delay(400);
digitalWrite(13,LOW);
delay(400);}
if(s_num=='4'){
digitalWrite(3,HIGH);
delay(500);
digitalWrite(13,LOW);
delay(500);}
if(s_num=='5'){
digitalWrite(13,HIGH);
delay(600);
digitalWrite(13,LOW);
delay(600);}
if(s_num=='6'){
digitalWrite(13,HIGH);
delay(700);
digitalWrite(13,LOW);
delay(700);}
if(s_num=='7'){
digitalWrite(13,HIGH);
delay(800);
digitalWrite(13,LOW);
delay(800);}
if(s_num=='8'){
digitalWrite(13,HIGH);
delay(900);
digitalWrite(13,LOW);
delay(900);}
if(s_num=='9'){
digitalWrite(13,HIGH);
delay(1000);
digitalWrite(13,LOW);
delay(1000);}
digitalWrite(13,LOW);
s_num=10;
}
1
  • 3
    If you are getting a compilation error, you need to include the full error message (including the information, where the error happened) in your question.
    – chrisl
    Nov 24 '20 at 7:58
1

One tip to prevent these kind of error: next time align the { and } and you see the error very fast:

void setup()
{
  Serial.begin(9600);
  pinMode(13,OUTPUT);
}

void loop() 
{
  char s_num; 
  if(Serial.available()>0)
  {
    s_num =Serial.read();
  }

  if(s_num=='0')
  {
    digitalWrite(13,HIGH);
    delay(100); //the specified time for 0
    digitalWrite(13,LOW);
    delay(100);
  }

  if(s_num=='1')
  {
    digitalWrite(13,HIGH);
    delay(200);
    digitalWrite(13,LOW);
    delay(200);
  }

  if(s_num=='2')
  {
    digitalWrite(13,HIGH);
    delay(300);
    digitalWrite(13,LOW);
    delay(300);
  }
 
  if(s_num=='3')
  {
    digitalWrite(13,HIGH);
    delay(400);
    digitalWrite(13,LOW);
    delay(400);
  }
  
  if(s_num=='4')
  {
    digitalWrite(3,HIGH);
    delay(500);
    digitalWrite(13,LOW);
    delay(500);
  }

  if(s_num=='5')
  {
    digitalWrite(13,HIGH);
    delay(600);
    digitalWrite(13,LOW);
    delay(600);
  }

  if(s_num=='6')
  {
    digitalWrite(13,HIGH);
    delay(700);
    digitalWrite(13,LOW);
    delay(700);
  }
  if(s_num=='7')
  {
    digitalWrite(13,HIGH);
    delay(800);
    digitalWrite(13,LOW);
    delay(800);
  }
  if(s_num=='8')
  {
    digitalWrite(13,HIGH);
    delay(900);
    digitalWrite(13,LOW);
    delay(900);
  }
  if(s_num=='9')
  {
    digitalWrite(13,HIGH);
    delay(1000);
    digitalWrite(13,LOW);
    delay(1000);
  }
  digitalWrite(13,LOW);
  s_num=10;
}

As you can see char s_name needs to be be declared in the front of the method as you need it in the second line, so you get the following code that compiles:

void loop() 
{
  if(Serial.available()>0)
  {
    s_num =Serial.read();
  }

  if(s_num=='0')
  {
    digitalWrite(13,HIGH);
    delay(100); //the specified time for 0
    digitalWrite(13,LOW);
    delay(100);
  }

  if(s_num=='1')
  {
    digitalWrite(13,HIGH);
    delay(200);
    digitalWrite(13,LOW);
    delay(200);
  }

  if(s_num=='2')
  {
    digitalWrite(13,HIGH);
    delay(300);
    digitalWrite(13,LOW);
    delay(300);
  }
  char s_num; 
  
  if(s_num=='3')
  {
    digitalWrite(13,HIGH);
    delay(400);
    digitalWrite(13,LOW);
    delay(400);
  }
  
  ...
}

Now you also can see that you process the s_num variable even if there is not a new value from the serial class, so you should put it probably inside, getting:

void loop() 
{
  if(Serial.available()>0)
  {
    char s_num =Serial.read();
 
    if(s_num=='0')
    {
      digitalWrite(13,HIGH);
      delay(100); //the specified time for 0
      digitalWrite(13,LOW);
      delay(100);
    }
  }
}

The last statement s_num=10; does not anything.

Now, to greatly reduce your code, all digits perform more or less the same code. You can combine this by calculating the delay time (0 -> 100, 9 -> 1000), and you get the following code:

void setup()
{
  Serial.begin(9600);
  pinMode(13, OUTPUT);
}

void loop() 
{
  if (Serial.available() > 0)
  {
    char s_num = Serial.read();
    if ((s_num >= '0') && (s_num <= '9'))
    {
      uint16_t delayTime = (s_num - '0' + 1) * 100;
      digitalWrite(13, HIGH);
      delay(delayTime);
      digitalWrite(13, LOW);
      delay(delayTime);
    }
    else
    {
      digitalWrite(13, LOW);
    }
  }
}

By subtracting '0' from a character digit ('0' to '9'), you get its value.

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.