Hello, i am new in arduino and i need help with coding.
I need to read one digital input and if state is low(0v) send ir code(only once), if state is high(5v) send different ir code(only once). And do nothing until state of digital input changes. Can someone tell me guidelines on what that code should look like. Thanks
const byte PIN = 4;
void setup ()
{
Serial.begin ( 115200 );
pinMode ( PIN, INPUT_PULLUP );
}
void loop ()
{
while ( digitalRead ( PIN ) );
Serial.println ( "IR code 1" ); // send IR code 1 if PIN is LOW
while ( !digitalRead ( PIN ) );
Serial.println ( "IR code 2" ); // send IR code 2 if PIN is HIGH
}
This is just an example and there are no debounce if hardware button is used.
1 Like
Hi,
the OP said: "send ir code(only once), "
this sketch is always sent.
And has ";"" after while case.
RV mineirin
Try this sketch:
const byte PIN = 4;
bool flag = true;
void setup ()
{
Serial.begin ( 115200 );
pinMode ( PIN, INPUT_PULLUP );
}
void loop ()
{
while ( digitalRead ( PIN ) )
{
if (flag == true)
{
Serial.println ( "IR code 1" ); // send IR code 1 if PIN is LOW
flag = false;
}
}
flag = true;
while ( !digitalRead ( PIN ) )
{
if (flag == true)
{
Serial.println ( "IR code 2" ); // send IR code 2 if PIN is HIGH
flag = false;
}
}
flag = true;
}
1 Like
No, it does not always send but only once. I tested on wokwi simulator and works as expected.
1 Like
I still waiting ir diode, so I can't try, butt i also try in simulator and working as expected. thanks a lot.
system
Closed
November 13, 2021, 5:07pm
7
This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.