Controlling the Arduino over serial

 In the Serial Output tutorial, we've discovered the simplicity of printing data from Arduino to a computer. Yet, this process can be reversed. Within the Arduino IDE's serial monitor window, we possess the ability to input a string and transmit it to the Arduino. In the upcoming sections, you'll delve into the utilization of such strings and explore their potential in controlling various functionalities.

Only a single component is required to execute this procedure – an Arduino board linked to a computer through a USB connection. Establish a connection between the Arduino and the computer to initiate the programming phase. The subsequent code will activate the integrated LED upon receiving the letter 'a'. It will deactivate the LED upon receiving the letter 'x' and will make it blink for a designated duration upon receiving 'b' followed by a numeral from 1 to 9, for instance, 'b4'.


int led = 13;
void setup(){
pinMode(led, OUTPUT);
Serial.begin(9600);
}
void loop(){
if (Serial.available()){
char com = Serial.read();
// Act according to the value received
if (com == 'x'){
// Stop the LED
digitalWrite(led, LOW);
}
else if (com == 'a'){
// Start the LED
digitalWrite(led, HIGH);
}
else if (com == 'b'){
// Blink with a delay corresponding to the value received
after 'b'
if (Serial.peek() > '0' && Serial.peek() <= '9'){
digitalWrite(led, HIGH);
delay((Serial.read() - 48) * 100); // 48 is ASCII for '0'
digitalWrite(led, LOW);
}
}
}
}

This Arduino code sets up a simple program that controls an LED based on commands received through the serial communication interface. Here's a step-by-step explanation of the code:

  1. int led = 13;: This line declares a variable named led and assigns it the value 13, which corresponds to the digital pin number where the LED is connected.

  2. void setup() { ... }: This is the setup function, which runs once when the Arduino is powered on or reset.

    a. pinMode(led, OUTPUT);: This line configures the specified digital pin (led, which is pin 13) as an output pin, indicating that it will be used to control the LED.

    b. Serial.begin(9600);: This line initializes the serial communication with a baud rate of 9600 bits per second. This is the rate at which data will be exchanged between the Arduino and the computer.

  3. void loop() { ... }: This is the main loop function, which repeatedly executes after the setup function has completed.

    a. if (Serial.available()) { ... }: This condition checks if there is any data available to be read from the serial communication.

    • char com = Serial.read();: This line reads a single character from the serial input and stores it in the variable com.

    • if (com == 'x') { ... }: If the received character is 'x', the following block of code executes:

      digitalWrite(led, LOW);: This turns off the LED by setting the specified pin (pin 13) to a low voltage level.

    • else if (com == 'a') { ... }: If the received character is 'a', the following block of code executes:

      digitalWrite(led, HIGH);: This turns on the LED by setting the specified pin (pin 13) to a high voltage level.

    • else if (com == 'b') { ... }: If the received character is 'b', the following block of code executes:

      • if (Serial.peek() > '0' && Serial.peek() <= '9') { ... }: This checks if the next character in the serial input stream is a digit between '1' and '9'.

        digitalWrite(led, HIGH);: This turns on the LED.

        delay((Serial.read() - 48) * 100);: This line reads the next character, converts it to a numeric value, subtracts 48 (ASCII for '0') to get the actual digit, and then multiplies it by 100 to determine the delay duration. This causes the LED to blink for a specified amount of time.

        digitalWrite(led, LOW);: This turns off the LED.

In summary, this code allows you to control an LED connected to pin 13 of the Arduino using serial commands. 'x' turns off the LED, 'a' turns it on, and 'b' followed by a digit (1 to 9) blinks the LED for a corresponding duration.

 see also:

Intruder Sensor with Ultrasonic Sensor

Post a Comment

Previous Post Next Post