Hello
Do you looking for "simple" solution or do you learn how to code?
hi thanks im looking for both simple solution and understand what the solution is
Hello, please make the following changes.
if(data=='R')//LED ON
{
digitalWrite(11,LOW);
digitalWrite(12,LOW);
digitalWrite(13,HIGH);
}
else (data=='S');//LED Off
{
digitalWrite(11,LOW);
digitalWrite(12,LOW);
digitalWrite(13,LOW);
}
if(data=='Y')//LED ON
{
digitalWrite(11,LOW);
digitalWrite(13,LOW);
digitalWrite(12,HIGH);
}
else (data=='Z');//LED Off
{
digitalWrite(11,LOW);
digitalWrite(12,LOW);
digitalWrite(13,LOW);
}
if(data=='G')//LED ON
{
digitalWrite(12,LOW);
digitalWrite(13,LOW);
digitalWrite(11,HIGH);
}
else (data=='H');//LED Off
{
digitalWrite(11,LOW);
digitalWrite(12,LOW);
digitalWrite(13,LOW);
}
Hello
This a very "simple" solution using OOP. I“ve made an object and a method to handle the activities.
/* BLOCK COMMENT
ATTENTION: This Sketch contains elements of C++.
https://www.learncpp.com/cpp-tutorial/
https://forum.arduino.cc/t/arduino-uno-controlling-leds-on-outputs/936156
*/
#define ProjectName "Arduino UNO controlling ledās on outputs"
#define OutPutTest
constexpr unsigned long OutPutTestTime {1000};
struct KEYLED {
byte key;
byte pin;
bool state_;
};
KEYLED keyLeds[] = {
{'R', 13, HIGH},
{'S', 13, LOW},
{'Y', 12, HIGH},
{'Z', 12, LOW},
{'G', 11, HIGH},
{'H', 11, LOW},
};
// -------------------------------------------------------------------
void setup() {
Serial.begin(9600);
Serial.println(F("."));
Serial.print(F("File : ")), Serial.println(__FILE__);
Serial.print(F("Date : ")), Serial.println(__DATE__);
Serial.print(F("Project: ")), Serial.println(ProjectName);
pinMode (LED_BUILTIN, OUTPUT); // used as heartbeat indicator
// https://www.learncpp.com/cpp-tutorial/for-each-loops/
for (auto keyLed : keyLeds) pinMode(keyLed.pin, OUTPUT);
#ifdef OutPutTest
// check outputs
for (auto keyLed : keyLeds) digitalWrite(keyLed.pin, HIGH), delay(OutPutTestTime);
for (auto keyLed : keyLeds) digitalWrite(keyLed.pin, LOW), delay(OutPutTestTime);
#endif
}
void loop () {
digitalWrite(LED_BUILTIN, (millis() / 500) % 2);
if (Serial.available()) {
byte data = Serial.read();
Serial.println((char) data);
for (auto &keyLed : keyLeds) if (data == keyLed.key) digitalWrite( keyLed.pin, keyLed.state_);
}
}
Have a nice day and enjoy coding in C++.
Please follow the advice given in the link below when posting code, in particular the section entitled 'Posting code and common code problems'
Use code tags (the </> icon above the compose window) to make it easier to read and copy for examination
Oh... Ok Thank you. Actually I am new here. Still learning the rules.
When you tried to post your code without code tags did you get a message suggesting that you use code tags ?
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.