0
votes

This is my Program. It's an Arduino sketch.

int bite = 0;

void setup() {
  Serial.begin(9600); // opens serial port, sets data rate to 9600 bps
  pinMode(3, OUTPUT);
}

void loop() {
  if (Serial.available() > 0) {
  bite = Serial.read();
  if(bite != NULL){
    for (int i=0; i < bite; i++){
      digitalWrite(3, HIGH);
      delay(1000);
      digitalWrite(3, LOW);
      delay(1000);
      }
    }
  }
}

I want the code to check if there is a serial input value and whether it's neither null or a string and blink my LED the number of times as the serial value is received. My code is just blinking the light with and the light is also very dim. It just keeps running. It even happens when I send null to the serial port.

1
For every one byte you receive, normally takes 1 millisecond, your program goes off in the woods for 22 seconds to blink the light. You'll have to type really slow.Hans Passant
Most serial input possibilities (terminal programs, Arduino SerialMonitor) won't allow you to send null bytes. Theoretically, Serial.read() can read any binary byte stream, but the sender side could be the problem...datafiddler

1 Answers

0
votes

Look at this example code:

char incomingByte;
int led=3;

void setup() {
   Serial.begin(9600);
   pinMode(led,OUTPUT);
   Serial.println("LED control");
   Serial.println("0 = LED off)");
   Serial.println("1 = LED on");
}

void loop(){
  if(Serial.available()>0){
    incomingByte = Serial.read();
    if(incomingByte == '0'){
       digitalWrite(led,LOW);
    }
    if(incomingByte == '1'){
       digitalWrite(led,HIGH);
    }
  }
}

So the only thing you probably need to do is to change your int bite to char bite because of the Serial.read()!