Arduino Button Trouble - arduino

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!

Related

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 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: wait for serial input

I am writing a simple arduino code. I have two leds and corresponding two switches. When one switch is pressed one led is on and other is off. Then there is a Serial.read function, which reads reset from the computer. Then both switch are off. After that other switch is pressed and other led is on. My problem is when one switch is on other shouldn't be working till the Serial.read happens.But in my case, when led1 is on if I press switch2 led2 is on and led1 is off. But that is not my desired operation. I want to make the logic that when led1 is on if I press switch2 led2 shouldn't be on and wait for the Serial.read to happen. Here is my code. I need to know what should be the correction in the logic:
int switch1 = 2;
int motorled1 = 3;
int switch2 = 4;
int motorled2 = 5;
int d1=2;
int d2=3;
int reset1 = 0;
int reset2= 0;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
pinMode(2, INPUT);
pinMode(4, INPUT);
pinMode(3, OUTPUT);
pinMode(5, OUTPUT);
}
void loop() {
// put your main code here, to run repeatedly:
d1=digitalRead(2);
d2=digitalRead(4);
if (d1==1)
{
digitalWrite(3, HIGH);
digitalWrite(5, LOW);
if (Serial.available() > 0)
{
Serial.write(1);
}
if (Serial.available() > 0)
{
reset1 = (Serial.read());
digitalWrite(motorled1, LOW);
digitalWrite(motorled2, LOW);
}
}
else if (d2==1)
{
digitalWrite(3, LOW);
digitalWrite(5, HIGH);
if (Serial.available() > 0)
{
Serial.write(2);
}
if (Serial.available() > 0)
{
reset2 = (Serial.read());
digitalWrite(motorled1, LOW);
digitalWrite(motorled2, LOW);
}
}
}
You seem to be using some asynchronous code structure. This is a good thing, but I am not really sure if this is what you intended to do.
In its current state, the code will continue looping over and over, checking if one of the buttons is pressed.
Now, there is two ways of achieving this:
Either make a proper state machine design, which would be the preferred way
Or just wait for the serial to be available at some point.
For the second solution, you could replace
if (Serial.available() > 0)
{
reset1 = (Serial.read());
digitalWrite(motorled1, LOW);
digitalWrite(motorled2, LOW);
}
by
while (Serial.available() <= 0)
{}
reset1 = (Serial.read());
digitalWrite(motorled1, LOW);
digitalWrite(motorled2, LOW);
essentially doing nothing while there is nothing on the serial port. Of course, this will completely freeze the rest of the program, and isn't very flexible.
For the first way, there is once again multiple ways of doing it. Since you seem not to be familiar with C programming (no offense), one of the easiest ways of doing it would be to change your if (d1==1) and else if (d2==1) statements by if (d1==1 && serial_read == false) and else if (d2==1 && serial_read == true). Then, at the top of the program, add:
int serial_read = false;
if(Serial.available() > 0)
{
reset1 = (Serial.read());
serial_read = true;
}
This is the basic idea. I will let you sort out the various bugs and improvements (such as setting serial_read to false again) as an exercise.
I also strongly encourage you to read a bit about programming in general, and C programming in particular. I also advise you to try to stick to some convention for indenting your code. There is nothing worse than code with mixed indentation.
This is the answer.
int switch1 = 2;
int motorled1 = 3;
int switch2 = 4;
int motorled2 = 5;
int d1 = 0;
int d2 = 0;
int buzzer;
char reset1;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
pinMode(2, INPUT);
pinMode(4, INPUT);
pinMode(3, OUTPUT);
pinMode(5, OUTPUT);
}
void loop() {
// put your main code here, to run repeatedly:
d1 = digitalRead(2);
d2 = digitalRead(4);
if (d1 == 1 && buzzer == 0) {
digitalWrite(motorled1, HIGH);
buzzer = 1;
} else if (d2 == 1 && buzzer == 0) {
digitalWrite(motorled2, HIGH);
buzzer = 2;
}
if (Serial.available() > 0) {
Serial.write(buzzer);
reset1 = Serial.read();
buzzer = 0;
if (reset1 == 'R') {
Serial.println("LED is off");
digitalWrite(motorled1, LOW);
digitalWrite(motorled2, LOW);
}
}
}

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

Resources