Sure, I'll help you complete both Arduino codes according to the instructions provided.
First Code: Arduino Receiver
Instructions:
- When it receives 'H', it turns on the onboard LED.
- When it receives 'L', it turns off the onboard LED.
Here's the completed code:
/* ARDUINO RICEVENTE
QUANDO RICEVE H ACCENDE IL LED ON BOARD
QUANDO RICEVE L SPEGNE IL LED ON BOARD
*/
byte dato;
void setup()
{
pinMode(LED_BUILTIN, OUTPUT); /* Configurazione del LED integrato */
Serial.begin(9600); /* Inizializza la comunicazione seriale */
}
void loop()
{
if (Serial.available() > 0) /* Controlla se c'è un dato in arrivo */
{
dato = Serial.read(); /* Legge il dato seriale */
if (dato == 'H') /* Se il dato è 'H' */
{
digitalWrite(LED_BUILTIN, HIGH); /* Accende il LED integrato */
}
else if (dato == 'L') /* Se il dato è 'L' */
{
digitalWrite(LED_BUILTIN, LOW); /* Spegne il LED integrato */
}
}
}
Second Code: Arduino Transmitter
Instructions:
- The character is sent upon pressing the button.
- Sends 'H' if the button is pressed.
- Sends 'L' if the button is not pressed.
Here's the completed code:
/* ARDUINO TRASMETTITORE
L'INVIO DEL CARATTERE AVVIENE A SEGUITO DELLA
PRESSIONE DEL PULSANTE
H SE IL PULSANTE È PREMUTO
L SE IL PULSANTE NON È PREMUTO
*/
int in; /* Inizializzazione della variabile del segnale */
void setup()
{
pinMode(2, INPUT_PULLUP); /* Configurazione del pin input con resistenza di pull-up interna */
Serial.begin(9600); /* Inizializza la comunicazione seriale */
}
void loop()
{
in = digitalRead(2); /* Lettura dell’input ricevuto dal pulsante */
delay(100); /* Piccola pausa per evitare letture multiple */
if (in == LOW) /* Condizione di invio del segnale: pulsante premuto */
{
Serial.write('H'); /* Invia 'H' se il pulsante è premuto */
}
else
{
Serial.write('L'); /* Invia 'L' se il pulsante non è premuto */
}
}
Note on the Second Code:
pinMode(2, INPUT_PULLUP);: Configures pin 2 as an input with an internal pull-up resistor. This means the pin will read HIGH when the button is not pressed and LOW when pressed (assuming the other side of the button is connected to ground).
- Debouncing and Delay: The
delay(100); provides a short pause to prevent bouncing issues and limits how frequently characters are sent.
- Serial Communication: Remember to connect both Arduinos properly if you're using separate boards or set up the communication channel if they're on the same board.
Final Remarks:
- Ensure that both Arduinos are connected correctly for serial communication.
- Test the codes individually before integrating them to make sure each part works as expected.
- Adjust pin numbers and configurations according to your specific hardware setup.
Hope this helps you complete your project!