How to use a button in a while loop in Arduino? - 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;

Related

Make relay be turned on by any of 3 touch sensors

I have a relay and i want to turn on the light with it. I have 2 touch sensors but with this code I can only turn it on with 1 how can i make it work? The third is a switch but it should still work the same. I've tried and it worked with a different code.
int touchPin = 2;
int relayPin = 3;
int val = 0;
int lightON = 0;
int touched = 0;
void setup() {
Serial.begin(9600);
pinMode(touchPin, INPUT);
pinMode(relayPin, OUTPUT);
}
void loop() {
val = digitalRead(touchPin);
if(val == HIGH && lightON == LOW){
touched = 1-touched;
delay(100);
}
lightON = val;
if(touched == HIGH){
Serial.println("Light ON");
digitalWrite(relayPin, LOW);
}else{
Serial.println("Light OFF");
digitalWrite(relayPin, HIGH);
}
delay(100);
}
I suggest reading up on Arduino programming so that you can fully understand what is happening in someone else's program and then modify it to suit. There are tons of resources online just waiting for you :)
This is how I would write the program:
`/*
* As I understand the relay is to turn on a light,
* should either of the touch sensors be triggered
*/
const int touchPin_1 = 2;
const int touchPin_2 = 3;
const int relayPin = 4;
bool isLightOn = false;
void setup() {
pinMode(touchPin_1, INPUT);
pinMode(touchPin_2, INPUT);
pinMode(relayPin, OUTPUT);
Serial.begin(9600);
}
void loop() {
int touchPin_1_val = digitalRead(touchPin_1);
int touchPin_2_val = digitalRead(touchPin_2);
if (touchPin_1_val == HIGH || touchPin_2_val == HIGH) { //if either of the sensors are triggered...
isLightOn = !isLightOn; //toggle light state
}
if (isLightOn) { //if the light state is true, or set to 'on'...
Serial.println("light on");
digitalWrite(relayPin, HIGH); //on
} else {
Serial.println("light off");
digitalWrite(relayPin, LOW); //off
}
delay(100);
}`
Tnx but this is how i wrote it and it works fine
int touchPin = 6;
int touchPin2 = 7;
int touchPin3 = 5;
int relayPin = 9;
int val = 0;
int val2 = 0;
int val3 = 0;
int lightON = 0;
int touched = 0;
void setup() {
Serial.begin(9600);
pinMode(touchPin, INPUT);
pinMode(touchPin2, INPUT);
pinMode(touchPin3, INPUT);
pinMode(relayPin, OUTPUT);
}
void loop() {
val = digitalRead(touchPin);
val2 = digitalRead(touchPin2);
val3 = digitalRead(touchPin3);
if((val == HIGH && lightON == LOW) || (val2 == HIGH && lightON == LOW) || (val3 == HIGH && lightON == LOW)){
touched = 1-touched;
delay(100);
}
lightON = val;
if(touched == HIGH){
Serial.println("Light ON");
digitalWrite(relayPin, LOW);
}else{
Serial.println("Light OFF");
digitalWrite(relayPin, HIGH);
}
delay(100);
}

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

How to write a state change (edge detection) using analogRead() on Arduino?

I'm having trouble creating a toggle switch using an analog input connected to an RF receiver. I'm attempting to make a servo turn 180 degrees when the voltage reads higher than 800 and stay at 180 degrees until the button is pressed again. It only reads higher than 800 when I press a button on my key fob (RF transmitter). In my code it does this, but it doesn't hold at 180 degrees and wait until the button is pressed again to go back to 0 degrees.
#include <Servo.h>
Servo myservo;
const int analogInPin = A0;
int led = 13;
float sensorValue = 0;
void setup(){
Serial.begin(2400);
pinMode(led, OUTPUT);
myservo.attach(6);
myservo.write(0);
}
void loop(){
sensorValue = analogRead(analogInPin);
Serial.print("Voltage Output = ");
Serial.print(sensorValue);
Serial.println(" ");
delay(100);
if (sensorValue > 800) {
digitalWrite(led, HIGH);
myservo.write(180);
delay(100);
}
else{
digitalWrite(led, LOW);
myservo.write(0);
delay(100);
}
}
EDIT: I'm pretty close with this edited code. I added a variable and an if statement. It turns the LED on and it stays on, but when pressed again it doesn't turn off. So close...
const int analogInPin = A0;
int led = 13;
int ledState = 0;
float sensorValue = 0;
void setup(){
Serial.begin(2400);
pinMode(led, OUTPUT);
}
void loop(){
sensorValue = analogRead(analogInPin);
Serial.print("Voltage Output = ");
Serial.print(sensorValue);
Serial.println(" ");
delay(100);
if (sensorValue < 400 && ledState == 0) {
digitalWrite(led, HIGH);
delay(500);
ledState == 1;
}
if (sensorValue < 400 && ledState == 1) {
digitalWrite(led, LOW);
delay(500);
ledState == 0;
}
}
OK. I think I solved it! Take a look. It now turns the LED on when the button is pressed and stays on until the button is pressed again. This is how to create a switch using an analog input reading.
const int analogInPin = A0;
int led = 13;
int ledState = 0;
float sensorValue = 0;
void setup(){
Serial.begin(2400);
pinMode(led, OUTPUT);
}
void loop(){
sensorValue = analogRead(analogInPin);
Serial.print("Voltage Output = ");
Serial.print(sensorValue);
Serial.println(" ");
delay(100);
if (sensorValue < 400 && ledState == 0) {
digitalWrite(led, HIGH);
delay(100);
ledState = 1;
Serial.println(ledState);
delay(500);
}
else {
if (sensorValue < 400 && ledState == 1) {
digitalWrite(led, LOW);
delay(100);
ledState = 0;
Serial.println(ledState);
delay(500);
}
}
}

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