i need help for my project

im newbie in this arduino world and i need help for my arduino project
so i want to make the program to works like this:
the LED wont turn on after i put the text 'A' on virtual terminal, it needed to push a button first to let me turn on the LED by writing 'A' after i push the button, but the LED wont turn on when i only push the button

i kept stuck on when i put text 'A' first then push the button the LED will turn on but when i push the button first then put 'A' LED wont turn on. its in reverse

this is my code:

char iByte;
void setup()
{
pinMode(2, INPUT);
pinMode(10, OUTPUT);
digitalWrite(2, 1);
digitalWrite(10, 1);
Serial.begin(9600);
}

void loop()
{
if(digitalRead(2)==0)
{
Serial.println("PB1 Ditekan");
delay(500);
while (Serial.available() > 0)
{
iByte = Serial.read();
Serial.println();
if (iByte == 'A')
{
digitalWrite(10, 0);
Serial.println("Nyala");
delay(3000);
}
digitalWrite(10, 1);
return;
}
}
}

PB1 Ditekan = push button 1 is been pressed
nyala = turned on

Sorry my english is so bad, still practicing it

First glance .. you can’t set digital pin 2 as an input and then write to it later on.

hammy:
First glance .. you can’t set digital pin 2 as an input and then write to it later on.

Yes, you can - it turns on the built-in pull-up.

Oh ... didn’t know that ! I’ve only ever used the “pull-up” parameter .

apologies for the misdirection !

When you send 'A' to your program from the IDE monitor, you are actually sending 'A' and then '\n' so your program is reading two characters inside the while() loop. This means, the first Serial.read() returns 'A' which turns ON the LED and waits 3 seconds, and then turn the LED OFF

Have a look at the examples in Serial Input Basics - simple reliable ways to receive data.

...R

blh64:
When you send 'A' to your program from the IDE monitor, you are actually sending 'A' and then '\n' so your program is reading two characters inside the while() loop. This means, the first Serial.read() returns 'A' which turns ON the LED and waits 3 seconds, and then turn the LED OFF

So will it be worked? Im still stuck with this code, if i separate the if(digitalRead) with if(Serial.available), but wont it be separated too? Like i dont need to push a button to turn on the light, i just need to put 'A' right?

It is not clear to me what exactly you want to happen...

  1. Press a button (and hold it down?) and then send 'A' causes LED to turn ON (for 3 seconds, forever, ???)
  2. just sending 'A' (or anything) with the button UP does nothing?

Please clarify

blh64:

  1. Press a button (and hold it down?) and then send 'A' causes LED to turn ON (for 3 seconds, forever, ???)
  2. just sending 'A' (or anything) with the button UP does nothing?

That is not at all clear either.

...R

Robin2:
That is not at all clear either.

...R

I was asking if the button had to stay down during Serial reception or if it just needed to be pressed and released before Serial reception.

Hi,
Welcome to the forum.

Please read the first post in any forum entitled how to use this forum.
http://forum.arduino.cc/index.php/topic,148850.0.html then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

Thanks.. Tom... :slight_smile: