How do I turn off my void loop function after a certain IF condition is met? (arduino) - arduino-uno

I am trying to make a simple burglar alarm using magnet value as my door. After pressing button1 it goes into countdown and then the user should be able to press button1 again to insert a 3 digit code with the help of pmeter. Except that after button1 is clicked it calls toggleOnOff() again. How do I block my void toggleOnOff() function after the conditions of the first IF statement are met? Thank you in advance.
int startMagnetValue;
int Code[3] = {1, 2, 3};
int userCode[3] = {0, 0, 0};
int secondsLimit = 20;
int countDown;
int pMeter = 0;
int pMin = 14;
int pMax = 948;
unsigned long time_now = 0;
unsigned long time_then = 0;
unsigned long seconds = 0;
unsigned long secondsbefore = 1;
unsigned long interval = 1000;
void setup() {
initializeBreadboard();
startMagnetValue = analogRead(MAGNETSENSOR);
}
//turns off leds
void turnOffLEDS() {
digitalWrite(LED_RED, LOW);
digitalWrite(LED_BLUE, LOW);
digitalWrite(LED_YELLOW, LOW);
digitalWrite(LED_GREEN, LOW);
}
// returns true when the difference is bigger than 50, returns false when it's less than 50. True meaning door is open and False meaning it is closed
bool isOpen(int magnetValue) {
int magnetValueDifference = abs(startMagnetValue - magnetValue);
if (magnetValueDifference <= 50) {
return true;
} else {
return false;
}
}
//Toggles the alarm On or Off
void toggleOnOff() {
isAlarmOn = !isAlarmOn;
delay(200);
}
//registers if the alarm is on or off, also prints current magnet value to the oled
void loop() {
pMeter = analogRead(POTENTIOMETER);
int currentMagnetValue = analogRead(MAGNETSENSOR);
pMeter = map(pMeter, pMin, pMax, 0, 9); //translates pmeter to simple digits
OLED.printBottom(pMeter);
Serial.println(userCode[1]);
//On Off
if (digitalRead(BUTTON1)) {
toggleOnOff();
}
// Door Open, Alarm On
if(isOpen(currentMagnetValue) && isAlarmOn) {
turnOffLEDS();
digitalWrite(LED_RED, HIGH);
OLED.printTop(secondsLimit);
time_now = millis();
while((time_now - time_then) >= 1000)
{ secondsLimit--;
time_then = time_now;
Serial.println(secondsLimit);
}
if (userCode[1] = 0) {
if (digitalRead(BUTTON1)) {
userCode[1] = pMeter;
}
}
if (userCode[2] = 0) {
if (digitalRead(BUTTON1)) {
userCode[2] = pMeter;
}
}
if (userCode[3] = 0) {
if (digitalRead(BUTTON1)) {
userCode[3] = pMeter;
}
}
if (Code == userCode) {
isAlarmOn = false;
}
if (secondsLimit <= 0) {
digitalWrite(BUZZER, HIGH);
turnOffLEDS();
while (secondsLimit <= 0) {
OLED.print("INTRUDER");
turnOffLEDS();
digitalWrite(LED_RED, HIGH);
delay(100);
turnOffLEDS();
digitalWrite(LED_BLUE, HIGH);
delay(100);
turnOffLEDS();
digitalWrite(LED_YELLOW, HIGH);
delay(100);
turnOffLEDS();
digitalWrite(LED_GREEN, HIGH);
}
}
} else if (isOpen(currentMagnetValue) && !isAlarmOn) {
turnOffLEDS();
digitalWrite(LED_BLUE, HIGH);
//Door closed, Alarm On
} else if (!isOpen(currentMagnetValue) && isAlarmOn) {
turnOffLEDS();
digitalWrite(LED_GREEN, HIGH);
//Door closed, Alarm Off
} else if (!isOpen(currentMagnetValue) && !isAlarmOn) {
turnOffLEDS();
digitalWrite(LED_YELLOW, HIGH);
}
}```

Related

How to read from two serial monitors at a time?

Here's my code:
const int AnalogInPin1 = A1;
const int AnalogInPin2 = A2;
int SerialPrint = 0;
int SerialMonitor = 0;
void setup() {
Serial.begin(9600);
}
void loop() {
SerialPrint = analogRead(AnalogInPin1);
Serial.print("Sensor = ");
Serial.println(SerialPrint);
if (SerialPrint > 400) {
digitalWrite(3, LOW);
digitalWrite(4, HIGH);
delay(500);
}
else{
digitalWrite(4, HIGH);
digitalWrite(3, LOW);
delay(500);
}
}
void setup1() {
Serial.begin();
}
void loop1() {
SerialMonitor = analogRead(AnalogInPin2);
Serial.print("Sensor = ");
Serial.println(SerialMonitor);
if (SerialMonitor > 400) {
digitalWrite(6, LOW);
digitalWrite(5, HIGH);
delay(500);
}
else{
digitalWrite(5, HIGH);
digitalWrite(6, LOW);
delay(500);
}
}
I am trying to make a car guidance system like the ones found in underground parking lots. and I am trying to have two serial monitors read from 2 sensors.
f�������x怘�f�������x怘
The characters above are looping in the serial monitor whilst using 19200 baud but not in 9600 baud why is that?
If your question is about how to display two analogRead values in one SerialMonitor sketch, this should compile and work (if your SerialMonitor is set to 9600, of course):
const int AnalogInPin1 = A1;
const int AnalogInPin2 = A2;
int value1 = 0;
int value2 = 0;
void setup() {
Serial.begin(9600);
pinMode(3, OUTPUT);
pinMode(4, OUTPUT);
pinMode(5, OUTPUT);
pinMode(6, OUTPUT);
}
void loop() {
value1 = analogRead(AnalogInPin1);
Serial.print("Sensor1 = ");
Serial.print(value1);
if (value1 > 400) {
digitalWrite(3, LOW);
digitalWrite(4, HIGH);
}
else{
digitalWrite(4, HIGH);
digitalWrite(3, LOW);
}
value2 = analogRead(AnalogInPin2);
Serial.print("\t Sensor2 = ");
Serial.println(value2);
if (value2 > 400) {
digitalWrite(6, LOW);
digitalWrite(5, HIGH);
}
else{
digitalWrite(5, HIGH);
digitalWrite(6, LOW);
}
delay(500);
}
I left most of your code as is, just made it compile and work. If this is not what you want, please edit your question.

2 Leds, 3 Pusbuttons Arduino UNO and breadboard

So when i press button3 it's supposed to make led3 blink and button1 led1 blink
But when i press button2 it's supposed to make them turn off but it wont
Any ideas what might be wrong with my code?
// C++ code
//
const byte button3 = 4;
const byte button1 = 2;
const byte button2 = 3;
const int led1 = 9;
const int led2 = 8;
const int led3 = 7;
int buttonPushCounterRight = 0;
int buttonPushCounterDominant = 0;
int buttonPushCounterLeft = 0;
boolean buttonState = 0;
boolean lastButtonStateRight = 0;
boolean lastButtonStateDominant = 0;
boolean lastButtonStateLeft = 0;
void setup()
{
pinMode(button1, INPUT_PULLUP);
pinMode(button2, INPUT_PULLUP);
pinMode(button3, INPUT_PULLUP);
pinMode(led3, OUTPUT);
pinMode(led2, OUTPUT);
pinMode(led1, OUTPUT);
pinMode(A0, INPUT);
Serial.begin(9600);
}
void loop()
{
Serial.println (A0);
int potValue = analogRead (A0);
buttonState = digitalRead(button1);
if (buttonState != lastButtonStateLeft)
{
if (buttonState == LOW)
{
buttonPushCounterLeft++;
Serial.println("on");
Serial.print("button pushes: ");
Serial.println(buttonPushCounterLeft);
}
else
{
Serial.println("off");
}
lastButtonStateLeft = buttonState;
}
buttonState = digitalRead(button3);
if (buttonState != lastButtonStateRight)
{
if (buttonState == LOW)
{
buttonPushCounterRight++;
Serial.println("on");
Serial.print("button pushes: ");
Serial.println(buttonPushCounterRight);
}
else
{
Serial.println("off");
}
lastButtonStateRight = buttonState;
}
buttonState = digitalRead(button2);
if (buttonState != lastButtonStateDominant)
{
if (buttonState == LOW)
{
buttonPushCounterDominant++;
Serial.println("on");
Serial.print("button pushes: ");
Serial.println(buttonPushCounterDominant);
}
else
{
Serial.println("off");
}
lastButtonStateDominant = buttonState;
}
Serial.println(digitalRead(button1));
if (buttonPushCounterLeft == 1)
{
digitalWrite(led3, HIGH);
delay (potValue);
digitalWrite(led3, LOW);
delay (potValue);
}
Serial.println(digitalRead(button2));
if (buttonPushCounterDominant == 1)
{
digitalWrite(led3, LOW);
digitalWrite(led1, LOW);
}
Serial.println(digitalRead(button3));
if (buttonPushCounterRight == 1)
{
digitalWrite(led1, HIGH);
delay (potValue);
digitalWrite(led1, LOW);
delay (potValue);
}
}
When i press button2 once it will turn off the 2 leds but it doesn't
Other than that i don't know what to do.
Please guide me through my code and or possible solutions would be nice

While is not working even when it it is false

I have written this Code for a candy Box. The Box will open and close at specific times and days.
It can be adjusted with entering a menu: here void menu(). you can select if it should be a yes or no day. The menu starts with Monday. The Problem now is, I am in an endless loop in
while ((B1val != 1) || (B2val != 1) || (aState != aLastState)) {
If I check the values, they change to 1 but nothing happens maybe Arduino cant read the or (||).
Here is the full Code.
Thanks.
#include <Time.h>
#include <Wire.h>
#include <DS1307RTC.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);
//button Pins
#define L 5 //Lock/ Relay
#define B1 6 //Button for yes/ and unlock
#define B2 7 //Button for no
#define SW 4 //SW to enter menu
#define outputA 2
#define outputB 3
//Led
#define LR 10 //Led Pin for Red
#define LG 11 //Led Pin for green
//integers to safe yes(1) or no(0)
int Monday = 1;
int Tuesday = 1;
int Wednesday = 1;
int Thursday = 1;
int Friday = 1;
int Saturday = 1;
int Sunday = 1;
char Weekday = 'Monday';
int onTime = 1;
int offTime = 24;
int counter = 0;
int aState;
int aLastState;
void setup()
{
lcd.begin();
Serial.begin(9600);
setSyncProvider(RTC.get);
aLastState = digitalRead(outputA);
//set pinmodes
pinMode (outputA, INPUT);
pinMode (outputB, INPUT);
digitalWrite(B1, LOW);
digitalWrite(B2, LOW);
digitalWrite(SW, LOW);
pinMode(B1, INPUT);
pinMode(B2, INPUT);
pinMode(SW, INPUT);
// Turn on the backlight and print a message.
}
void loop() {
LOOP:
tmElements_t tm;
printDay();
int B1val = digitalRead(B1);
int B2val = digitalRead(B2);
int SWval = digitalRead(SW);
delay(10);
Serial.print(B1val);
Serial.print(B2val);
Serial.print(SWval);
if (hour() >= onTime && hour() <= offTime) {
digitalWrite(LG, HIGH);
analogWrite(LG, 50); //set light to 50
Serial.println("greeny");
}
else {
digitalWrite(L, LOW);
digitalWrite(LR, HIGH);
analogWrite(LR, 255); //set light to 50
}
if (B1val == 1) { //trying to unlock
Serial.println("unlock");
if (hour() >= onTime && hour() <= offTime && 'Weekday' == 1)
{ //flash green led
digitalWrite(L, HIGH); //unlock Lock
digitalWrite(LG, HIGH); //turn green Led on
delay(200);
analogWrite(LG, 50); //set light to 50 to not hurt your eyes
delay(200);
digitalWrite(LG, HIGH); //turn green Led on
delay(200);
analogWrite(LG, 50); //set light to 50 to not hurt your eyes
delay(200);
digitalWrite(LG, HIGH); //turn green Led on
delay(200);
analogWrite(LG, 50); //set light to 50 to not hurt your eyes
delay(200);
digitalWrite(LG, HIGH); //turn green Led on
delay(200);
analogWrite(LG, 50); //set light to 50 to not hurt your eyes
delay(200);
digitalWrite(LG, HIGH); //turn green Led on
delay(200);
analogWrite(LG, 50); //set light to 50 to not hurt your eyes
delay(200);
digitalWrite(LG, HIGH); //turn green Led on
delay(200);
analogWrite(LG, 50); //set light to 50 to not hurt your eyes
}
else { //flash red led
Serial.println("unlock false");
digitalWrite(L, LOW); //lock locked
digitalWrite(LR, HIGH); //turn red Led on
delay(200);
analogWrite(LR, 50); //set light to 50 to not hurt your eyes
delay(200);
digitalWrite(LR, HIGH); //turn red Led on
delay(200);
analogWrite(LR, 50); //set light to 50 to not hurt your eyes
delay(200);
digitalWrite(LR, HIGH); //turn red Led on
delay(200);
analogWrite(LR, 50); //set light to 50 to not hurt your eyes
delay(200);
digitalWrite(LR, HIGH); //turn red Led on
delay(200);
analogWrite(LR, 50); //set light to 50 to not hurt your eyes
delay(200);
digitalWrite(LR, HIGH); //turn red Led on
delay(200);
analogWrite(LR, 50); //set light to 50 to not hurt your eyes
delay(200);
digitalWrite(LR, HIGH); //turn red Led on
delay(200);
analogWrite(LR, 50); //set light to 50 to not hurt your eyes
}
}
if (hour() >= onTime && hour() <= offTime) {
Serial.println("unlocked");
analogWrite(LG, 50); //set light to 50
}
else {
Serial.println("locked");
analogWrite(LR, 50); //set light to 50
}
if (SWval == 1) { //to enter menu when rotary encoder switch clicked
menu();
}
else {
lcd.clear();
lcd.noBacklight(); // turn off backlight
}
aLastState = digitalRead(outputA);
}
void menu() {
int B1val = digitalRead(B1);
int B2val = digitalRead(B2);
int SWval = digitalRead(SW);
Serial.println("menu");
Monday:
Serial.println("Monday");
lcd.backlight(); //turn on lcd
aLastState = aState;
aState = digitalRead(outputA);
lcd.print(" Monday"); //display print Monday
while ((B1val != 1) || (B2val != 1) || (aState != aLastState)) {
Serial.println("while");
aState = digitalRead(outputA);
int B1val = digitalRead(B1);
int B2val = digitalRead(B2);
int SWval = digitalRead(SW);
Serial.print(B1val);
Serial.print(B2val);
Serial.print(SWval);
}
if (B1val == HIGH) {
Serial.println("Monday yes");
Monday = 1;
lcd.setCursor(0, 1);
lcd.print(" Yes ");
delay(100);
goto Monday;
}
else if (B2val == HIGH) {
Serial.println("Monday no");
Monday = 0;
lcd.setCursor(0, 1);
lcd.print(" No ");
delay(100);
goto Monday;
}
else if (digitalRead(outputB) != aState) {
if (aState != aLastState) {
goto Tuesday;
}
else {
goto Monday;
}
}
delay(100);
Tuesday:
Serial.println("Tuesday");
aLastState = aState;
aState = digitalRead(outputA);
lcd.clear();
lcd.print(" Tuesday"); //display print Tuesday
while ((B1 != HIGH) || (B2 != HIGH) || (aState != aLastState)) {}
if (B1 == HIGH) {
Serial.println("Tuesday yes");
Tuesday = 1;
lcd.setCursor(0, 1);
lcd.print(" Yes ");
delay(100);
goto Tuesday;
}
else if (B2 == HIGH) {
Serial.println("Tuesday no");
Tuesday = 0;
lcd.setCursor(0, 1);
lcd.print(" No ");
delay(100);
goto Tuesday;
}
else if (digitalRead(outputB) != aState) {
if (aState != aLastState) {
goto Wednesday;
}
else {
goto Tuesday;
}
}
delay(100);
Wednesday:
Serial.println("Wednesday");
aLastState = aState;
aState = digitalRead(outputA);
lcd.clear();
lcd.print(" Wednesday"); //display print
while (B1 != HIGH || B2 != HIGH || aState != aLastState) {}
if (B1 == HIGH) {
Serial.println("Wednesday yes");
Wednesday = 1;
lcd.setCursor(0, 1);
lcd.print(" Yes ");
delay(100);
goto Wednesday;
}
else if (B2 == HIGH) {
Serial.println("Wednesday no");
Wednesday = 0;
lcd.setCursor(0, 1);
lcd.print(" No ");
delay(100);
goto Wednesday;
}
else if (digitalRead(outputB) != aState) {
if (aState != aLastState) {
goto Thursday;
}
else {
goto Wednesday;
}
}
delay(100);
Thursday:
aLastState = aState;
aState = digitalRead(outputA);
lcd.clear();
lcd.print(" Thursday"); //display print
while (B1 != HIGH || B2 != HIGH || aState != aLastState) {}
if (B1 == HIGH) {
Thursday = 1;
lcd.setCursor(0, 1);
lcd.print(" Yes ");
delay(100);
goto Friday;
}
else if (B2 == HIGH) {
Thursday = 0;
lcd.setCursor(0, 1);
lcd.print(" No ");
delay(100);
goto Thursday;
}
else if (digitalRead(outputB) != aState) {
if (aState != aLastState) {
goto Thursday;
}
else {
goto Thursday;
}
}
delay(100);
Friday:
aLastState = aState;
aState = digitalRead(outputA);
lcd.clear();
lcd.print(" Friday"); //display print
while (B1 != HIGH || B2 != HIGH || aState != aLastState)
{}
if (B1 == HIGH) {
Friday = 1;
lcd.setCursor(0, 1);
lcd.print(" Yes ");
delay(100);
goto Friday;
}
else if (B2 == HIGH) {
Friday = 0;
lcd.setCursor(0, 1);
lcd.print(" No ");
delay(100);
goto Friday;
}
else if (digitalRead(outputB) != aState) {
if (aState != aLastState) {
goto Saturday;
}
else {
goto Friday;
}
}
delay(100);
Saturday:
aLastState = aState;
aState = digitalRead(outputA);
lcd.clear();
lcd.print(" Saturday"); //display print
while (B1 != HIGH || B2 != HIGH || aState != aLastState)
{}
if (B1 == HIGH) {
Saturday = 1;
lcd.setCursor(0, 1);
lcd.print(" Yes ");
delay(100);
goto Saturday;
}
else if (B2 == HIGH) {
Saturday = 0;
lcd.setCursor(0, 1);
lcd.print(" No ");
delay(100);
goto Saturday;
}
else if (digitalRead(outputB) != aState) {
if (aState != aLastState) {
goto Sunday;
}
else {
goto Saturday;
}
}
delay(100);
Sunday:
aLastState = aState;
aState = digitalRead(outputA);
lcd.clear();
lcd.print(" Sunday"); //display print
while (B1 != HIGH || B2 != HIGH || aState != aLastState) {}
if (B1 == HIGH) {
Sunday = 1;
lcd.setCursor(0, 1);
lcd.print(" Yes ");
delay(100);
goto Sunday;
}
else if (B2 == HIGH) {
Sunday = 0;
lcd.setCursor(0, 1);
lcd.print(" No ");
delay(100);
goto Sunday;
}
else if (digitalRead(outputB) != aState) {
if (aState != aLastState) {
goto Time;
}
else {
goto Saturday;
}
}
Time:
Serial.println("time");
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("turn on: "); //display print
lcd.print(onTime);
lcd.print(":00");
lcd.setCursor(0, 1); //set cursor
lcd.print("turn off: ");
lcd.print(offTime);
lcd.print(":00");
OnTime:
while (B1 != HIGH) { //accept on time
if (B2 == HIGH) {
goto OffTime;
}
aLastState = aState;
aState = digitalRead(outputA);
if (digitalRead(outputB) != aState) {
if (aState != aLastState) {
onTime + 1;
lcd.setCursor(9, 0);
lcd.print(onTime);
lcd.print(":00");
// lcd.setCursor(9, 1); //set cursor
// lcd.print(offTime);
// lcd.print(":00");
}
else {
onTime - 1;
}
if ( onTime < 0) {
onTime = 24;
}
if ( onTime > 24) {
onTime = 0;
}
lcd.setCursor(9, 0);
lcd.print(onTime);
lcd.print(":00");
// lcd.setCursor(9, 1); //set cursor
// lcd.print(offTime);
// lcd.print(":00");
}
}
aLastState = aState;
OffTime:
while (B2 != HIGH) { //accept off time
if (B1 == HIGH) {
goto OnTime;
}
aLastState = aState;
aState = digitalRead(outputA);
if (digitalRead(outputB) != aState) {
if (aState != aLastState) {
offTime + 1;
Serial.println(offTime);
// lcd.setCursor(9, 0);
// lcd.print(onTime);
// lcd.print(":00");
lcd.setCursor(9, 1); //set cursor
lcd.print(offTime);
lcd.print(":00");
}
else {
offTime - 1;
}
if ( offTime > 24) {
offTime = 0;
}
if ( offTime < 0) {
offTime = 24;
}
// lcd.setCursor(9, 0);
// lcd.print(onTime);
// lcd.print(":00");
lcd.setCursor(9, 1); //set cursor
lcd.print(offTime);
lcd.print(":00");
delay(100);
if(B1val == 1){
goto LOOP;
}
}
}
aLastState = aState;
}
void printDay()
{
int day;
day = weekday();
if (day == 1) {
Weekday = 'Sunday';
}
if (day == 2) {
Weekday = 'Monday';
}
if (day == 3) {
Weekday = 'Tuesday';
}
if (day == 4) {
Weekday = 'Wednesday';
}
if (day == 5) {
Weekday = 'Thursday';
}
if (day == 6) {
Weekday = 'Friday';
}
if (day == 7) {
Weekday = 'Saturday';
}
}
You are re-declaring B1val inside your loop as a second variable!
So you will never be able to change the variable you would like to change...
Remove the int in your while-loop.

Holding a push button for 3 second in a for loop

I have a button that basically has two functions. The aim is:
First push will blink a LED 15 times.
If someone holds the button longer than 3 seconds while the LED is blinking, it should stop and go back to the initial.
So I checked the Arduino's hold a button page and came up with this code. The problem is that I can not properly stop the blinking. The line Serial.println("DONE!!"); never works.
Where should I check if the button is held or not and should I use interrupt to end the for loop?
Here is my code:
int inPin = 2; // the pin number for input (for me a push button)
int ledPin = 9;
int current; // Current state of the button
// (LOW is pressed b/c I'm using the pullup resistors)
long millis_held; // How long the button was held (milliseconds)
long secs_held; // How long the button was held (seconds)
long prev_secs_held; // How long the button was held in the previous check
byte previous = LOW;
unsigned long firstTime; // how long since the button was first pressed
long millis_held2; // How long the button was held (milliseconds)
long secs_held2; // How long the button was held (seconds)
long prev_secs_held2; // How long the button was held in the previous check
byte previous2 = LOW;
unsigned long firstTime2; // how long since the button was first pressed
void setup() {
Serial.begin(9600); // Use serial for debugging
pinMode(ledPin, OUTPUT);
digitalWrite(inPin, INPUT); // Turn on 20k pullup resistors to simplify switch input
}
void loop() {
current = digitalRead(inPin);
// if the button state changes to pressed, remember the start time
if (current == HIGH && previous == LOW && (millis() - firstTime) > 200) {
firstTime = millis();
}
millis_held = (millis() - firstTime);
secs_held = millis_held / 1000;
// This if statement is a basic debouncing tool, the button must be pushed for at least
// 100 milliseconds in a row for it to be considered as a push.
if (millis_held > 50) {
// check if the button was released since we last checked
if (current == LOW && previous == HIGH) {
// HERE YOU WOULD ADD VARIOUS ACTIONS AND TIMES FOR YOUR OWN CODE
// ===============================================================================
//////////////////////////////////////////////////////// Button pressed Blink 15 times.
if (secs_held <= 0) {
for (int i = 0; i < 15; i++) {
ledblink(1, 750, ledPin);
current = digitalRead(inPin);
if (current == HIGH && previous == LOW && (millis() - firstTime2) > 200) {
firstTime2 = millis();
}
millis_held2 = (millis() - firstTime2);
secs_held2 = millis_held2 / 1000;
if (millis_held2 > 50) {
Serial.print("previousA ");
Serial.print(previous);
Serial.print(" currentA ");
Serial.println(current);
current = digitalRead(inPin);
if (current == HIGH && previous2 == HIGH) {
Serial.println("ALMOST! ");
Serial.println(secs_held2);
if (secs_held2 >= 2 && secs_held2 < 6) {
Serial.println("DONE!!");
}
}
}
previous2 = current;
prev_secs_held2 = secs_held2;
}
}
}
}
previous = current;
prev_secs_held = secs_held;
}
void ledblink(int times, int lengthms, int pinnum) {
for (int x = 0; x < times; x++) {
digitalWrite(pinnum, HIGH);
delay (lengthms);
digitalWrite(pinnum, LOW);
delay(lengthms);
}
}
This is how it would be done without using delay():
int inPin = 2;
int ledPin = 9;
long pressTimer;
long blinkTimer;
long blinkHalfDelay = 500; //milliseconds
int blinkCount = 0;
int blinkLimit = 15;
bool blinkFlag = false;
bool blinkLatch = true;
bool currentBtnState = false;
bool lastBtnState = false;
void setup() {
}
void loop() {
lastBtnState = currentBtnState;
currentBtnState = digitalRead(inPin);
//on rising edge, start timers
if(currentBtnState==true && lastBtnState==false)
{
pressTimer = millis();
}
//debounce activation
if(currentBtnState==true && !blinkFlag && millis()-pressTimer > 50)
{
blinkTimer = millis();
blinkCount = 0;
blinkLatch = true;
blinkFlag = true;
//functional execution would go here
}
if(currentBtnState = false || (millis()-pressTimer>3000 && currentBtnState == true))
{
blinkCount=blinkLimit;
blinkLatch = false;
}
if(blinkFlag == true)
{
if(millis()-blinkTimer > blinkHalfDelay)
{
blinkTimer = millis();
if(blinkLatch)
{
digitalWrite(pinnum, HIGH);
}
else
{
digitalWrite(pinnum, LOW);
blinkCount+=1;
}
}
if(blinkCount>blinkLimit)
{
blinkFlag = false;
digitalWrite(pinnum, LOW);
}
}
}
ps: sorry for not correcting your code directly, but it was kinda hard to read...

SG90 MIcro Servo Motor Push Button Arduino for Vending Machie Prototype

I have made my new code for my prototype again and my problems are:
When the arduino started running, the three SG90 Micro servo motors rotated 360 degrees at the same time and didn't stop. When I pushed the round push button it slowed the speed of rotation of the servo motor.
When I tried to simulate my program to 123d.circuits.io, it worked. I used the momentary push button since there is no round push button there.
What I wanted to achieve is that:
When push button A/B/C is pushed, the servo Motor A/B/C will rotate 360 deg to dispense an item for my vending machine prototype.
Here's my code. Hope you can help me.
#include <Servo.h>
const int pushButtonA = 2;
const int pushButtonB = 4;
const int pushButtonC = 7;
Servo myservoA;
Servo myservoB;
Servo myservoC;
int pos = 0;
int buttonStateA = 0;
int buttonStateB = 0;
int buttonStateC = 0;
void setup() {
Serial.begin(9600);
pinMode(pushButtonA, INPUT);
pinMode(pushButtonB, INPUT);
pinMode(pushButtonC, INPUT);
myservoA.attach(3);
myservoB.attach(5);
myservoC.attach(9);
}
void loop() {
buttonStateA = digitalRead (pushButtonA);
buttonStateB = digitalRead (pushButtonB);
buttonStateC = digitalRead (pushButtonC);
if((buttonStateA == LOW) && (buttonStateB == HIGH) && (buttonStateC == HIGH)){
digitalWrite(3, HIGH);
for(pos = 0; pos <=360; pos++);
myservoA.write(pos);
delay(15);
digitalWrite(3, HIGH);
}
if((buttonStateB == LOW) && (buttonStateA == HIGH) && (buttonStateC == HIGH)){
digitalWrite(5, HIGH);
for(pos = 0; pos <=360; pos++);
myservoB.write(pos);
delay(15);
digitalWrite(5, HIGH);
}
if((buttonStateC == LOW) && (buttonStateB == HIGH) && (buttonStateA == HIGH)){
digitalWrite(9, HIGH);
for(pos = 0; pos <=360; pos++);
myservoC.write(pos);
delay(15);
digitalWrite(9, HIGH);
};
}
There are two problems with your code.
As mentioned by #MikeCAT, the for loops in the code are empty because you added a semi-colon after them. See Effect of semicolon after 'for' loop for further clarification.
There is no need to perform digitalWrite in servo pins. For controlling them just use the method write from the Servo class.
Corrected code:
#include <Servo.h>
const int pushButtonA = 2;
const int pushButtonB = 4;
const int pushButtonC = 7;
Servo myservoA;
Servo myservoB;
Servo myservoC;
int pos = 0;
int buttonStateA = 0;
int buttonStateB = 0;
int buttonStateC = 0;
void setup() {
Serial.begin(9600);
pinMode(pushButtonA, INPUT);
pinMode(pushButtonB, INPUT);
pinMode(pushButtonC, INPUT);
myservoA.attach(3);
myservoB.attach(5);
myservoC.attach(9);
}
void loop() {
buttonStateA = digitalRead (pushButtonA);
buttonStateB = digitalRead (pushButtonB);
buttonStateC = digitalRead (pushButtonC);
if((buttonStateA == LOW) && (buttonStateB == HIGH) && (buttonStateC == HIGH)){
for(pos = 0; pos <=360; pos++) {
myservoA.write(pos);
delay(15);
}
}
if((buttonStateB == LOW) && (buttonStateA == HIGH) && (buttonStateC == HIGH)){
for(pos = 0; pos <=360; pos++) {
myservoB.write(pos);
delay(15);
}
}
if((buttonStateC == LOW) && (buttonStateB == HIGH) && (buttonStateA == HIGH)){
for(pos = 0; pos <=360; pos++) {
myservoC.write(pos);
delay(15);
}
}
}

Resources