Here is my code for serial monitor to several LEDs.
int led = 13; // Pin 13
int led1 = 3; // D3 Pin 6
int led2 = 4; // D4 Pin 7
void setup()
{
pinMode(led, OUTPUT); // Set pin 13 as digital out
pinMode(led1, OUTPUT); // Set pin 6 as digital out
pinMode(led2, OUTPUT); // Set pin 7 as digital out
// Start up serial connection
Serial.begin(9600); // baud rate
Serial.flush();
}
void loop()
{
String input = "";
// Read any serial input
while (Serial.available() > 0)
{
input += (char) Serial.read(); // Read in one char at a time
delay(20); // Delay for 5 ms so the next char has time to be received
if (input == "on1"){
digitalWrite(led, HIGH); // on pin 13
}
else if (input == "off1"){
digitalWrite(led, LOW); // off
}
if (input == "on2"){
digitalWrite(led1, HIGH); // on D3 pin 6
}
else if (input == "off2"){
digitalWrite(led1, LOW); // off
}
if (input == "on3"){
digitalWrite(led2, HIGH); // on D4 pin 7
}
else if (input == "off3"){
digitalWrite(led2, LOW); // off
}
}
}
When I download the code on Nano and I see LED2 (pin 6) and LED3 (pin7) stay on but not LED pin 13. When I use serial monitor and type "on1" and got the LED (pin 13) light on. Then I type "off1" and got the LED (pin 13) light off. Seem it is working fine. but when I type "off2" and nothing happens and still LED stays on. When I type "on2" and LED2 light goes off. Looks like it is backward. Same with LED3. I could not figure out what wrong with my codes. Can someone help me with it? Also, I would like to put a switch on pin 10. When push the button and the serial monitor got notice. I am not sure how to do this.
Whether HIGH turns your LED on or off is dependent on how the LED is wired. It's just as common for circuits to be active LOW. You can determine which it is with a simple experiment.
It's very confusing that your "on2"/"off2" command controls led1 and your "on3"/"off3" command controls led2. It's very important to choose meaningful variable names. It might seem obvious to you now, but if you continue this practice with more complex programs, or even come back to a simple program after some months, you'll eventually pay the price.
int LED = 13; // Pin 13
int LED1 = 3; // D3 Pin 6
int LED2 = 4; // D4 Pin 7
void setup() {
Serial.begin(9600);
pinMode(LED, OUTPUT); // set the digital pin as output:
pinMode(LED1, OUTPUT); // set the digital pin as output:
pinMode(LED2, OUTPUT); // set the digital pin as output:
}
void loop() {
if(Serial.available()) // if there is data comming
{
String command = Serial.readStringUntil('\n'); // read string until meet newline character
if(command == "on1")
{
digitalWrite(LED, HIGH); // turn on LED
Serial.println("LED 13 is turned on"); // send action to Serial Monitor
}
else
if(command == "off1")
{
digitalWrite(LED, LOW); // turn off LED
Serial.println("LED 13 is turned off"); // send action to Serial Monitor
}
if(command == "on2")
{
digitalWrite(LED1, HIGH); // turn on LED
Serial.println("LED 3 is turned on"); // send action to Serial Monitor
}
else
if(command == "off2")
{
digitalWrite(LED1, LOW); // turn off LED
Serial.println("LED 3 is turned off"); // send action to Serial Monitor
}
if(command == "on3")
{
digitalWrite(LED2, HIGH); // turn on LED
Serial.println("LED 4 is turned on"); // send action to Serial Monitor
}
else
if(command == "off3")
{
digitalWrite(LED2, LOW); // turn off LED
Serial.println("LED 4 is turned off"); // send action to Serial Monitor
}
}
}
Here is my code for three LEDs. I would like to turn on and off several LEDs from the Serial monitor. I got the first LED working on and off. But the second and third stay on when I run the program. I could not figure out what went wrong. When I type on2 and the LED2 went off. Same with type on3 and the LED3 went off. When I type off2 the LED2 went on. Same with LED3 when I type off3, the LED3 went on. I google and I only found an example program for Serial monitor with one LED. I could not find an example code for a serial monitor with several LED.
Grumpy_Mike,
Many thanks. On serial monitor, I changed from No line ending to Carriage return and got it working now. Thank you for your help. One more question about rs485, I understand we can use rs485 for the long wire. I just got a USB convertor dongle and Max485. Can we use Max485, USB dongle with a serial monitor? I want to use a long wire to control Nano from the computer on rs485. I need some examples of it. I do see some examples of RS485 between two Arduin. I could not find example between serial monitor from PC to Arduin on rs485.
One more thing, I would like to add second Nano on RS485. I do have another MAX485. But I am not sure how to address on first Nano and the second Nano? Can we send a special code for (address) then a second code for which LED comes on or off?
The RS485 can send to many other devices on the same line, it is all down to the software protocol. But is is a simplex system dot a duplex one. That means it can only transmit or receive not both at the same time.
Therefore you have to implement a sort of master slave arrangement where the slaves only respond when asked to. Even then you have to be careful about switching from receive to transmit. You have to give sufficient time for the transmit to clear its buffer and then switch over. This means the receiver has to delay its sending to make sure the master has switched to receive.
I used to design access control system and we communicate between a master and the slave RFID and lock release mechanisms. Our software was limited to 32 devices and could spread over about half a kilometre. The protocol was complex and often caused problems. I was in charge of the hardware for this so don’t know the software in detail.
I got Serial Monitor working with one Nano on RS485. Now I add second Nano with Max485. I need to figure out how to send code rather the first Nano or Second Nano LEDs come on. I don't know-how. Does anyone have example code for two Nano on Max485 with Serial Monitor?
Grumpy_Mike, Is it possible to share your code what you have done on RS485? I would like to see the code with RFID.
If you have a serial RS485 connection connected to both Nanos, then they will always read the same data.
This is dealt with by allocating each an address and sending that address (number, or could be a letter, A .. B ..C ..) as the first character on the line of text. Then each Nano checks the first character and completely ignores the remainder of the line if it does not correspond to its own address which you set in the code.
Oh, and do not use "String"s - the ones with the capital "S". Just don't!