Connecting led with Arduino using strings

Asked

Viewed 208 times

1

Perform code that causes the D1 LED to be erased when it is sent from PC to Arduino an odd decimal numeric digit between {1,3,5,7,9} and accessed if the sent digit is even between {0,2,4,6,8}.

How would I do that?

That’s what I’ve done so far:

#define LED 4
lista[] = ['1','3','5','7','9','0','2','4','6','8']
void setup(){
    Serial.begin(9600);
    pinMode(pin_led, OUTPUT);
}
void loop(){
    if(lista[]=)
        if analogRead(lista[n],HIGH){
            digitalWrite(LED, LOW);
    }
    else{
        digitalWrite(LED, HIGH);
    }
}

1 answer

2


See these links for some inspirations:

Try something like that:

#define LED 4

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

int par(int c) {
    return c == '0' || c == '2' || c == '4' || c == '6' || c == '8';
}

int impar(int c) {
    return c == '1' || c == '3' || c == '5' || c == '7' || c == '9';
}

void loop() {
    while (Serial.available() > 0) {
        inr r = Serial.read();
        if (impar(r)) {
            digitalWrite(LED, LOW);
        } else if (par(r)) {
            digitalWrite(LED, HIGH);
        }
    }
}

The functions par and impar are to determine whether the number received is even or odd. If it is neither (eg a) shall not enter either ifs.

In the pinMode it must be set that the pin attached to the LED (the 4) is output. As it is output, you can use digitalWrite in it.

The number sent by the PC on the serial port is actually a digital character, and therefore should not be used analogRead to read it, and yes Serial.read().

Note: I’ve never worked with Andy, so I don’t know if this will work.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.