Serial Print once only - arduino

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

Related

How to use a button in a while loop in Arduino?

So i tried to make a little quiz, it can use a lot of improvement but i would like to have a finished project before i better it. I want the user to choose the answer by pressing a button.
My code currently looks like this.
char serialData;
int i = 1;
int redLed = 12;
int greenLed=13;
int buttonA=11;
int buttonB=10;
int buttonC=9;
int lastState = LOW;
int currentState;
void setup()
{
Serial.begin(9600);
pinMode(redLed, OUTPUT);
pinMode(greenLed, OUTPUT);
pinMode(buttonA,INPUT);
pinMode(buttonB,INPUT);
pinMode(buttonC,INPUT);
// read the state of the switch/button:
currentState = digitalRead(buttonA);
if(lastState == HIGH && currentState == LOW)
Serial.println("A");
// save the the last state
lastState = currentState;
delay(100);
}
void loop()
{
vraag1();
delay(1000);
vraag2();
delay(1000);
vraag3();
delay(1000);
vraag4();
delay(1000);
vraag5();
delay(1000);
vraag6();
}
void vraag1()
{ //1
Serial.println(" 3+3=? ");
delay(400);
Serial.println(" A) 6");
delay(200);
Serial.println(" B) 5 ");
delay(200);
Serial.println(" C) 4 ");
delay(200);
while (Serial.available() ==0){} //2-2
/*
This is the part I added to resolve the issue - but it
does not work.
*/
currentState = digitalRead(buttonA);
if(lastState == HIGH && currentState == LOW)
Serial.println("A");
lastState = currentState;
if (Serial.available())
{ //3
serialData = Serial.read();
if (serialData == '6' || serialData == 'a' || serialData == 'A')
{ //4
Serial.println(" Correct ");
i++;
digitalWrite(greenLed, HIGH);
} //4
else
{ //5
Serial.println(" Incorrect ");
digitalWrite(redLed, HIGH);
} //5
}
delay(1000);
digitalWrite(redLed, LOW);
digitalWrite(greenLed, LOW);
}
This is the code I added to try it but it doesn't work at all. Does anyone have another suggestion?
currentState = digitalRead(buttonA);
if(lastState == HIGH && currentState == LOW)
Serial.println("A");
lastState = currentState;

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");
...
}

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.

how to control door using SIM900a

I need help I'm doing project about opening and closing the door using SIM900a.. it says that no errors but it doesn't work !! I used AT commands and it supposes to send notification to the user using sms when theres knocking, motion at home, high temperature and when theirs smoke please help please please
#include <SoftwareSerial.h>
#include "pins_arduino.h"
#include <String.h>
SoftwareSerial SIM900(7, 8); //tx & rx pins
String msg = String("");
char inchar;
int x = 0;
int y = 0;
float z =0;
int PIR_sensor = 12;
int door_lock = 4 ; //close the door
int door_lock1 = 5 ;
int led = 11;
int led1 = 10; //red led
int mic= 6;
int Gas_sensor = A0;
float Tem_sensor = A1;
float temp =0.0;
String textForGAS ;
String textForPIR ;
String textForTAM ;
void setup()
{
Serial.begin(19200);
pinMode(PIR_sensor, INPUT);
pinMode(door_lock, OUTPUT);
pinMode(door_lock1, OUTPUT);
pinMode(led, OUTPUT);
pinMode(led1, OUTPUT);
digitalWrite(door_lock, LOW);
digitalWrite(door_lock1, LOW);
digitalWrite(led, LOW);
digitalWrite(led1, LOW);
// wake up the GSM shield
SIM900.begin(19200);
delay(20000); // give time to log on to network.
SIM900.print("AT+CMGF=1r"); // set SMS mode to text
delay(100);
SIM900.print("AT+CNMI=2,2,0,0,0r");
// blurt out contents of new SMS upon receipt to the GSM shield’s serial out
delay(100);
Serial.println("Ready…");
}
void sendSMS(String message)
{
SIM900.print("AT+CMGF=1\r");
delay(100);
SIM900.println("AT + CMGS = \"+96896089681\"");
delay(100);
SIM900.println(message);
delay(100);
SIM900.println((char)26);
delay(100);
SIM900.println();
delay(5000);
}
void loop()
{
//If a character comes in from the cellular module…
if (SIM900.available())
{
inchar = SIM900.read();
Serial.println(inchar);
if (inchar == '#')
{
delay(10);
inchar = SIM900.read();
if (inchar == 'a')
{
delay(10);
inchar = SIM900.read();
if (inchar == '0')
{
digitalWrite(door_lock, HIGH);
digitalWrite(led1, HIGH);
digitalWrite(led, LOW);
}
else if (inchar == '1')
{
digitalWrite(door_lock1, HIGH);
digitalWrite(led, HIGH);
digitalWrite(led1, LOW);
}
delay(10);
inchar = SIM900.read();
if (inchar == 'b')
{
inchar = SIM900.read();
if (inchar == '0')
{
digitalWrite(PIR_sensor, LOW);
}
else if (inchar == '1')
{
digitalWrite(PIR_sensor, HIGH);
}
}
SIM900.println("AT+CMGD=1,4"); // delete all SMS
}
}
}
Gas_sensor = (analogRead(A0));
PIR_sensor = (digitalRead(12));
Tem_sensor = (analogRead(A1));
temp = Tem_sensor * 0.48828125;
//program for GAS sensor
if (temp > 70.0)
{
textForTAM = "Alarm ! The degree of Tamperture is :n ";
textForTAM.concat(temp);
textForTAM = textForTAM +"C";
Serial.println(textForTAM);
sendSMS(textForGAS);
delay(1000);
do {
z = (analogRead(A0));
}
while (z >= 141.312);
}
//program for GAS sensor
if (Gas_sensor > 500)
{
textForGAS = "Alarm ! there is Gas by rate :n ";
textForGAS.concat(Gas_sensor);
Serial.println(textForGAS);
sendSMS(textForGAS);
delay(1000);
}
//program for PIR sensor
if (digitalRead(12) == HIGH)
{
textForPIR = "Warnning ! n theres's a motion ";
textForPIR.concat(PIR_sensor);
Serial.println(textForPIR);
sendSMS(textForPIR);
delay(1000);
}
}
thank you

Arduino Button Trouble

I am just trying to make it so the light turns on when my button state is high. what am I doing wrong??? I keep getting it randomly turning on and off. Thank you in advance.
const int buttonPin = 2;
const int ledPin = 13;
int buttonState = 0;
boolean curstat = LOW;
boolean lasstat = LOW;
boolean ledState = LOW;
void loop(){
curstat = digitalRead(buttonPin);
if (curstat == HIGH && lasstat == LOW){
Serial.println("pressed");
delay(1);
bucount = bucount + 1;
Serial.println(bucount);
myservo.write(180);
delay(1);
digitalWrite(ledPin, HIGH);
delay(1);
}
else if(curstat == LOW && lasstat == HIGH){
Serial.println("relased");
delay(1);
myservo.write(89);
delay(1);
digitalWrite(ledPin, HIGH);
delay(1);
}
else if(curstat == LOW && lasstat == LOW){
Serial.println("nothing has happened");
digitalWrite(ledPin, LOW);
delay(1);
myservo.write(90);
delay(1);
}
}
first of all you'll need to declare input and output in setup:
void setup(){
pinMode(buttonPin, INPUT)
pinMode(ledPin, OUTPUT)
}
the other important thing is that you read curstat, but you never change lasstat, so Arduino will never understand previous state, consider adding this code:
if (curstat == HIGH && lasstat == LOW){
Serial.println("pressed");
delay(1);
digitalWrite(ledPin, HIGH);
delay(1);
lasstat = HIGH;
}
In this way you'll enter in if statement only once, even if you'll keep your button pressed too long. Don't forget to change lasstat also in other if statements.
Ciao!

Resources