Turn on led with arduino using strings

1
  

Perform code that causes the D1 LED to be off when an odd decimal numeric digit of between {1,3,5,7,9} is sent from the PC to the arduino and lit if the sent digit is between { 0.2,4,6,8}.

How would I do this?

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);
    }
}
    
asked by anonymous 11.01.2018 / 19:21

1 answer

2

See these links for some inspirations:

Try something like this:

#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 par and impar functions are for determining whether the received number is even or odd. If it is not both (for example a ) it will not enter either of the if s.

In% w / o of% you must define that the pin connected to the LED (the 4) is output. Since it is output, you can use pinMode in it.

The number sent by the PC on the serial port is actually a digital character, so you should not use digitalWrite to read it, but analogRead .

Note: I have never worked with arduino, so I do not know if this will work.

    
11.01.2018 / 22:33