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);
}
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?
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.