Communicating with Arduino Serial - serial-port

I am trying to communicate with my Arduino over the USB port by using Serial:
int previous;
int current = 0;
void turnOn(int pinNumber){
previous = current;
current = pinNumber;
if(previous!=0){
digitalWrite(previous, LOW);
digitalWrite(current, HIGH);
}else{
digitalWrite(current, HIGH);
}
}
void setup(){
pinMode(8, OUTPUT);
pinMode(9, OUTPUT);
pinMode(10, OUTPUT);
Serial.begin(9600);
Serial.write(1);
}
void loop(){
delay(1);
if(Serial.available()>0){
switch(Serial.read()){
case 0:
turnOn(8);
break;
case 1:
turnOn(9);
break;
case 2:
turnOn(10);
break;
default:
Serial.println(Serial.read());
}
}
}
I am trying so that if I send a 0 the rightmost LED will light up, if I send 1, the middle one will and if I send a 2 the leftmost will. However when I send 0,1 or anything else it prints a -1 meaning the default switch has been triggered. How do I fix it?

Try this...
void loop(){
if (Serial.available()) {
char input = Serial.read();
if(input == '0'){
turnOn(8);
}else if(input == '1'){
turnOn(9);
}else if(input == '2'){
turnOn(10);
}
}
}
Tell me if it works or not then we can proceed :)

Related

Arduino Uno and HC-05: Not showing any output on serial monitor

Arduino Uno - HC-05
Connections are: TX-RX, RX-TX, LED-D13, 5V-5V+
For this project, we can supply power to the Arduino through any +5V power source. You can use a USB port from your computer to power the Arduino, but in this project I used my laptop.
while (Serial.available()) is returning 0 and Serial.read() is returning -1.
Need Help!
Used Bluetooth voice recognition tool from playstore-"Arduino Voice Control"
#include <SoftwareSerial.h> //Replace (' ') with (< >)
SoftwareSerial BLU(0,1);
String voice;
int Green = 13; //Connect To Pin #13
//int Yellow = 2; //Connect To Pin #2
//int Red = 3; //Connect To Pin #3
void allon() {
//digitalWrite(Red, HIGH);
//digitalWrite(Yellow, HIGH);
Serial.print("start");
digitalWrite(Green, HIGH);
}
void alloff() {
//digitalWrite(Red, LOW);
//digitalWrite(Yellow, LOW);
digitalWrite(Green, LOW);
}
void setup() {
Serial.begin(9600);
BLU.begin(9600);
//pinMode(Red, OUTPUT);
//pinMode(Yellow, OUTPUT);
pinMode(Green, OUTPUT);
}
void loop() {
//Serial.print("start loop");
//Serial.print(Serial.available());
while (Serial.available()) { //Check if there is an available byte to read
//Serial.print("start");
delay(10); //Delay added to make thing stable
char c = Serial.read(); //Conduct a serial read
//Serial.print(Serial.read());
if (c == '#') {
break; //Exit the loop when the # is detected after the word
}
//Serial.print(c);
voice += c;
//Serial.print(voice+"\n");
}
if (voice.length() > 0) {
Serial.print("Start");
Serial.print(voice);
if (voice == "*turn on all LED") {
allon();
}
else if (voice == "*turn off all LED") {
alloff();
}
/*else if(voice == "*switch on red") {
digitalWrite(Red,HIGH);
}
else if(voice == "*switch on yellow") {
digitalWrite(Yellow,HIGH);
}*/
else if(voice == "*switch on green") {
digitalWrite(Green,HIGH);
}
/*else if(voice == "*switch off red") {
digitalWrite(Red,LOW);
}
else if(voice == "*switch off yellow") {
digitalWrite(Yellow,LOW);
}*/
else if(voice == "*switch off green") {
digitalWrite(Green,LOW);
}
voice=""; //Reset variable
}
}
You need to check for the app output first. If you already know it then mention that in comment otherwise do the following for printing app output first:-
const int LED = 5;
void setup() {
Serial.begin(9600);
pinMode(LED, OUTPUT);
}
void loop() {
while(Serial.available()>0){
switchstate = Serial.read();
Serial.print(switchstate); // First check what output are you getting from the application
Serial.print("\n");
delay(15);
if(switchstate == '1'){ // Compare your app output accordingly
digitalWrite(5, HIGH);
}
else if(switchstate == '0'){
digitalWrite(5, LOW);
}
}
}

Serial Print once only

I am a ON/OFF program in which if I turn On it will serial print to serial monitor 1 "ON" only but still looping and turn On the outputs and if turn Off it will serial print 1 "OFF" only but still looping and turn Off the outputs .
Here's my code:
int pbuttonPin = 7;// push button
int fan = 8;
int water = 9;
int val = 0; // push value from pin 2
int lightON = 0;//light status
int pushed = 0;//push status
void setup() {
Serial.begin(9600);
pinMode(pbuttonPin, INPUT_PULLUP);
pinMode(fan, OUTPUT);
pinMode(water, OUTPUT);
digitalWrite(fan, HIGH);
digitalWrite(water, HIGH);
}
void loop() {
val = digitalRead(pbuttonPin);// read the push button value
if(val == HIGH && lightON == LOW){
pushed = 1-pushed;
delay(100);
}
lightON = val;
if(pushed == LOW){
Serial.print("ON\n");
Serial.println();
digitalWrite(fan, LOW);
digitalWrite(water, LOW);
delay(100);
}
else if(pushed == HIGH) {
Serial.print("OFF\n");
Serial.println();
digitalWrite(fan, HIGH);
digitalWrite(water, HIGH);
delay(100);
}
}
I guess you want something like this but i am not sure
bool buttonState = false, buttonStateBefore = false;
buttonState = !digitalRead(buttonPin); //needs to be inverted because INPUT_PULLUP
if(buttonState > buttonStateBefore) doStuff(); //Serial print and turn on/off ligths
buttonStateBefore = buttonState;
Now doStuff() is only called once when the button is pressed.
Maybe this works for you
Add this at last of the code Sting previousval = val at the end of the code and put a if statement which checks if the previous value has changed or not. If it is, then run the code and put this part which contains the print statement in it . basically this will be your code
if ( previousval =! val){
if(pushed == LOW){
Serial.print("ON\n");
Serial.println();
digitalWrite(fan, LOW);
digitalWrite(water, LOW);
delay(100);
}
else if(pushed == HIGH) {
Serial.print("OFF\n");
Serial.println();
digitalWrite(fan, HIGH);
digitalWrite(water, HIGH);
delay(100);
previousval = val
}
}

ARDUINO : Hold Values & Serial monitor

i would like to ask .
I am doing LED project using serial monitor.
if key for serial monitor 1 is pressed one time , LED 1 will light up until i pressed the 2nd key for serial monitor 2 and LED 2 will light up .
how can i do that?
by delay() ? or hold () ?
You can't use delay() because the time taken until next the next keypress cannot be determined.
You can use flags, as demonstrated below
int ledPin1 = 8, ledPin2 = 9;
boolean flag1, flag2;
void setup()
{
Serial.begin(9600);
pinMode(ledPin1, OUTPUT);
pinMode(ledPin2, OUTPUT);
}
void loop()
{
if(Serial.available())
{
int c = Serial.read();
if(c == 49) { // 1 is pressed
flag1 = true;
flag2 = false;
}
else if(c == 50) { // 2 is pressed
flag2 = true;
flag1 = false;
}
}
if(flag1) {
digitalWrite(ledPin1, HIGH);
digitalWrite(ledPin2, LOW);
}
if(flag2) {
digitalWrite(ledPin1, LOW);
digitalWrite(ledPin2, HIGH);
}
delay(50);
}
Hope this helps.

Serial.read() - XBee - not firing 'else if'

I am currently trying to do some communication test between a PC and Arduino Uno using an XBee in AT mode.
My test is to send characters from the computer to the XBee and process through conditional statements.
I don't believe this issue is with configuration of the XBees, for I am able to communicate successfully when I watch the Serial monitors.
Here is the code I am running on the Arduino:
#include <SoftwareSerial.h>
SoftwareSerial xBee = SoftwareSerial(1, 0);
int Led = 9;
void setup()
{
pinMode(Led, OUTPUT);
xBee.begin(9600);
}
void loop()
{
if (xBee.available()> 0)
{
if (xBee.read() == 'r')
{
digitalWrite(Led, HIGH);
xBee.write("Led On");
delay(10);
}
else if (xBee.read() == 'o')
{
digitalWrite(Led, LOW);
xBee.write("Led Off");
delay(10);
}
else
{
xBee.write("NR"); // Testing for not recognized characters
}
delay(10);
}
delay(10);
}
I can turn on the LED when sending the character 'r' from the PC to the XBee. The intended result is received back as well. When I try to send the character 'o' from the PC the LED stays on, and I get the response of "NR".
This same result happens with different characters in the else if statement, sending character 'o' as the first character, changing to just if statements, and changing the initial condition to - while xBee.available().
How can I fix this problem?
You need to store the input value of xBee.read() and then use it in the if condition.
You can try this
#include <SoftwareSerial.h>
SoftwareSerial xBee = SoftwareSerial(1, 0);
int Led = 9;
void setup()
{
pinMode(Led, OUTPUT);
xBee.begin(9600);
}
void loop()
{
char read_value = xBee.read();
if(xBee.available()> 0)
{
if ( read_value == 'r')
{
digitalWrite(Led, HIGH);
xBee.write("Led On");
delay(10);
}
else if ( read_value == 'o')
{
digitalWrite(Led, LOW);
xBee.write("Led Off");
delay(10);
}
else
{
xBee.write("NR"); // Testing for not recognized characters
}
delay(10);
}
delay(10);
}
The problem is that you are taking the input with xBee.read() but not storing it.
Only your first if works ie,
if ( read_value == 'r')
{
digitalWrite(Led, HIGH);
xBee.write("Led On");
delay(10);
}
The control is not even going in the else if hence condition for o is not tested.

turning 3 leds on/off using serial port

I am trying to write a program that will do the following :
Write code so that if 1 then enter pressed or send clicked, and then 1 again and enter pressed or send clicked causes LED one to turn on, if ‘1’ ‘0’ is entered in a similar way then LED 1 turns off, and so on for LEDs two and three, ie: ‘2’ ‘1’ turns on LED 2, ‘3’ ’0’ turns off LED 3.
So far here is my code:
int incomingVal;
int ledPin = 16;
int ledPin2 = 15;
int ledPin3 = 14;
void setup()
{
Serial.begin(9600); // opens serial port, sets data rate to 9600 bps
Serial.println("starting");
pinMode(ledPin,OUTPUT);
pinMode(ledPin2,OUTPUT);
pinMode(ledPin3,OUTPUT);
}
void checkForRecvdChar ();
void loop()
{
if (Serial.available() > 0 ) //then chars are in the serial buffer
{
incomingVal = Serial.parseInt();
Serial.print("You entered: ");
Serial.println(incomingVal);
if (incomingVal == 10);//turns off led 1
{
digitalWrite(ledPin, LOW);
}
if (incomingVal == 11);//turns on led 1
{
digitalWrite(ledPin, HIGH);
}
if (incomingVal == 20);//turns off led 2
{
digitalWrite(ledPin2, LOW);
}
if (incomingVal == 21);//turns on led 2
{
digitalWrite(ledPin2, HIGH);
}
if (incomingVal == 30);//turns off led 3
{
digitalWrite(ledPin3, LOW);
}
if (incomingVal == 31);//turns on led 3
{
digitalWrite(ledPin3, HIGH);
}
}
}
Right now it turns all LEDs on no matter what combination I enter
you've got semi colons after your if statements, which terminates the if statement. So all your intended if blocks get executed.
for example...
if (incomingVal == 31);//turns on led 3
{
digitalWrite(ledPin3, HIGH);
}
needs to be
if (incomingVal == 31)
{
digitalWrite(ledPin3, HIGH);
}
Ok So First It Will Better To Use Switch Case in this Case:
1. You Can Just Use The Same Number To Turn The Led ON and OFF
Example:
case 1:
{
if(wFlag == false)
{
digitalWrite(LED, HIGH);
wFlag = true;
Serial.println("LED is ON");
}
else if (wFlag == true)
{
digitalWrite(LED, LOW);
wFlag = false;
Serial.println("LED is OFF");
}
break;
}
2. The Way You Do It, Your Problem Is Serial Read Gets Only Byte after Byte
So When You Press 1 or 11 It Sees 1 Because It is The First one "1" and Then Do Your If
Statment.
You Can Fix it Like This:
while (Serial.available())
{
IncomingData = Serial.parseInt();
Temp += String(IncomingData); //Temp String Var
}
DataIN = Temp.toInt(); //DataIN int Var
Temp =""; // Rest The Temp String
Switch(DataIN) // Or if(DataIN == 1)
{
}

Resources