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)
{
}
Related
I'm trying to make a simple program to turn a laser on and off. It was working fine until I tried to be able to make it blink by inputting a 2 to the serial monitor. It blinks, but only once. What I'm trying to make it do is blink forever, while also waiting for the user to input something to be able to stop blinking. Here is my code.
int laserPin = 3;
int laserState;
void setup()
{
pinMode(laserPin, OUTPUT); // set pin10 as output pin
digitalWrite(laserPin, LOW); // set the pin value on low at the begin
Serial.begin(9600);
Serial.println(" Commands");
Serial.println("--------------------");
Serial.println("2- makes laser blink");
Serial.println("1- turns laser on");
Serial.println("0- turns laser off");
}
void loop()
{
Serial.println("Set laser state to: "); //Prompt User for Input
while (Serial.available() == 0) {
// Wait for User to Input Data
}
laserState = Serial.parseInt(); //Read the data the user has input
Serial.println(laserState);
if(laserState == 1){
Serial.println("Turning laser on");
digitalWrite(laserPin, HIGH);
}
else if(laserState == 0){
Serial.println("Turning laser off");
digitalWrite(laserPin, LOW);
}
else if(laserState == 2){
Serial.println("Making laser blink");
while(laserState == 2)
{
digitalWrite(laserPin, HIGH);
delay(1000);
digitalWrite(laserPin, LOW);
Serial.println("Set laser state to: "); //Prompt User for Input
laserState = Serial.parseInt(); //Read the data the user has input
}
}
}
Here is the code I made to make the laser blink:
else if(laserState == 2){
Serial.println("Making laser blink");
while(laserState == 2)
{
digitalWrite(laserPin, HIGH);
delay(1000);
digitalWrite(laserPin, LOW);
Serial.println("Set laser state to: "); //Prompt User for Input
laserState = Serial.parseInt(); //Read the data the user has input
}
}
If you want to make the arduino do more than one thing at a time, you need to let the loop keep running and keep track of the state - rather than holding it up with while loops.
I've taken your code and modified it slightly to allow the serial to be read, and the laser to be flashed.
In this version laserState is normally set to -1 - which means the loop won't do anything apart from check the serial input each time.
When you input a serial int - this will change laserState and the appropriate action will be performed.
Immediately after switching on and off the laser, laserState is set back to -1 so no further actions occur.
However - if laserState is set to 2, it will stay at that value for each loop iteration and the laser will continue to flash until another serial input is received to change the state.
There's also a flag to determine when to display the prompt (although it will display everytime the led flashes).
This could be made more responsive by removing the delays in the flash and having a timer using millis() to determine when to switch the laser on and off - so you don't have to wait for a flash cycle to read the next serial input - but that would be a bit more complicated.
int laserPin = 3;
int laserState = -1;
bool showPrompt = true ;
void setup()
{
pinMode(laserPin, OUTPUT); // set pin10 as output pin
digitalWrite(laserPin, LOW); // set the pin value on low at the begin
Serial.begin(9600);
Serial.println(" Commands");
Serial.println("--------------------");
Serial.println("2- makes laser blink");
Serial.println("1- turns laser on");
Serial.println("0- turns laser off");
}
void loop()
{
if ( showPrompt ) {
Serial.println("Set laser state to: "); //Prompt User for Input
showPrompt = false ;
}
if (Serial.available()) {
// Read the User Input Data
laserState = Serial.parseInt(); //Read the data the user has input
Serial.println(laserState);
}
if(laserState == 1){
Serial.println("Turning laser on");
digitalWrite(laserPin, HIGH);
laserState = -1 ;
showPrompt = true ;
}
else if(laserState == 0){
Serial.println("Turning laser off");
digitalWrite(laserPin, LOW);
laserState = -1 ;
showPrompt = true ;
}
else if(laserState == 2){
Serial.println("Making laser blink");
digitalWrite(laserPin, HIGH);
delay(1000);
digitalWrite(laserPin, LOW);
delay(1000);
showPrompt = true ;
}
}
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);
}
}
}
I am creating 3 LEDs that will light up by a remote. I am able to light up the LEDs individually but I need the power button to shut off all of the LEDs. How can I create a 4th case to turn off all LEDs?
#include <IRremote.h>
int RECV_PIN = 3; // the pin where you connect the output pin of TSOP4838
int led1 = 2;
int led2 = 4;
int led3 = 7;
int itsONled[] = {0,0,0,0};
/* the initial state of LEDs is OFF (zero)
the first zero must remain zero but you can
change the others to 1's if you want a certain
led to light when the board is powered */
#define code1 12495 // code received from button A
#define code2 6375 // code received from button B
#define code3 31365 // code received from button C
IRrecv irrecv(RECV_PIN);
decode_results results;
void setup()
{
Serial.begin(9600); // you can comment this line
irrecv.enableIRIn(); // Start the receiver
pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT);
pinMode(led3, OUTPUT);
}
void loop() {
if (irrecv.decode(&results)) {
unsigned int value = results.value;
switch(value) {
case code1:
if(itsONled[1] == 1) { // if first led is on then
digitalWrite(led1, LOW); // turn it off when button is pressed
itsONled[1] = 0; // and set its state as off
} else { // else if first led is off
digitalWrite(led1, HIGH); // turn it on when the button is pressed
itsONled[1] = 1; // and set its state as on
}
break;
case code2:
if(itsONled[2] == 1) {
digitalWrite(led2, LOW);
itsONled[2] = 0;
} else {
digitalWrite(led2, HIGH);
itsONled[2] = 1;
}
break;
case code3:
if(itsONled[3] == 1) {
digitalWrite(led3, LOW);
itsONled[3] = 0;
} else {
digitalWrite(led3, HIGH);
itsONled[3] = 1;
}
break;
}
Serial.println(value); // you can comment this line
irrecv.resume(); // Receive the next value
}
}
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.
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 :)