cannot turn on LED from Arduino with Visual Basic

Hello
I'm trying to turn on an LED from Visual Basic
with button in Arduino
my problem the code
Serial.begin(9600);
blocks
The button input
and the led stay in HIGH
also the statement else not work
Thanks

code

int value;
int led=13;
int in=3;

void setup() {

Serial.begin(9600);
pinMode(led, OUTPUT);
pinMode(in, INPUT);


}



void loop() {

value=Serial.read();

if(value=='1' and digitalRead(in) == HIGH){

digitalWrite(led, HIGH);

} else {

digitalWrite(led, LOW);
}

Read about serial thingies with an Arduino

learn to post code using </>

int value;
int led=13;
int in=3;
void setup() {
    Serial.begin(9600);
    pinMode(led, OUTPUT);
    pinMode(in, INPUT);
}
void loop() {
    value=Serial.read();
    if(value=='1' and digitalRead(in) == HIGH){
        digitalWrite(led, HIGH);
    } else {
        digitalWrite(led, LOW);
    }

i see several problems with the above code

  • not sure what "in" is. should it be configured as INPUT_PULLUP? (is there a switch connected between it and ground)

  • there's no check using Serial.available() before reading a value from the serial port

  • i hopes it's clearn that the built-in led on pin 13 is active LOW. if a value of 1 is expected to turn it on, it should be set LOW

  • there's a missing closing brace at the end of loop()

On what board is this true? It is not true for Uno/Mega.

very sure led 13 on uno is active LOW. just tested it

Thanks for the help

My code has
end of loop()
My mistake its here
I use Arduino nano

I checked and it works
I forgot to put resistor to the ground in switch

I still have a problem with Else statement its not work
The LED with Else remains in LOW position why?

If I cancel the Else statement
The LED work When I press the switch and switch from Visual Basic

since a pin can be configured with an internal pullup resistor (INPUT_PULLUP) it's common to wire a switch between the pin and ground. the pullup makes the input HIGH. pressing the switch makes it LOW.

if you add an external pull-down resistor do you wire your switch between the pin and 5V?

with a pull-down and the following condition

the switch needs to be closed connecting the pin to 5V

don't understand how this can work with only the if condition. does it only work once?

Very odd, I just tested it on a Uno and when I do digitalWrite(LED_BUILTIN, HIGH) the LED goes on

sorry, i'm wrong.
i'm using a multiFunction shield with 4 LEDs driven by pins 10-13.

i now see that the LED on the Uno actually turns on when HIGH.

thanks for the correction

Thanks to everyone

I checked according to what you said
INPUT_PULLUP
You were right it worked instead of external resistor Before that I checked with a resistor IN 5V, thanks for this advice
When I just put this code

if(digitalRead(in) == HIGH){

} else {

}

Works fine

if i put this code only
led 13 Remains off not work with statement else!

 if(value=='1'){
     //led need stay on   
    } else {
     //led need stay off   
    }

The question is whether the problem is
In Arduino or Visual Basic
What do I need to do to make it work?

Without seeing the VB code, nobody can say for sure. One thing might be that you send e.g. "1\n" in which case '1' will switch the led on and '\n' will switch it off again.

You can test your Arduino code with the Serial Monitor; once that works, you can move to the VB side of things.

I suggest that you have a look / try the approaches from the link in post #2. It gives ideas for the receiving of data by the Arduino; next you can write the matching functionality in VB.

if you don't check for something being available and unconditionally read, the value will not be valid nor will it be '1'

1 Like

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.