ARDUINO : Hold Values & Serial monitor - arduino

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.

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
}
}

How do I make the buzzer stay on (LDR Alarm)?

I have a program in Arduino that checks an LDR sensor. If it goes over the set values it will trigger an alarm. How do I set it so once triggered it stays on until say a button push is detected to disarm it?
Code:
const int ledPin = 8;
const int buzzerPin = 4;
const int ldrPin = A0;
void setup () {
Serial.begin(9600);
pinMode(ledPin, OUTPUT);
pinMode(buzzerPin, OUTPUT);
pinMode(ldrPin, INPUT);
}
void loop() {
int ldrStatus = analogRead(ldrPin);
if (ldrStatus >= 30) {
noTone(buzzerPin);
digitalWrite(ledPin, LOW);
} else {
tone(buzzerPin, 100);
digitalWrite(ledPin, HIGH);
delay(100);
noTone(buzzerPin);
digitalWrite(ledPin, LOW);
delay(100);
Serial.println("----------- ALARM ACTIVATED -----------");
}
}
You should use a FLAG to fire the alarm instead of using threshold directly.
if (ldrStatus >= 30) {
AlarmFlag = true; //Set alarm
}
...
if (digitalRead(pushButton) == LOW){
AlarmFlag = false; //Turn off alarm
}
...
if (AlarmFlag == true){
Serial.println("ALARM ON");
...
}

Power button IRremote

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
}
}

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