While is not working even when it it is false - time

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.

Related

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

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

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

Processing-A shape is selected by either pressing a key ‘1’ or or key ‘2’ so that shape 1 or shape 2 can be picked up respectively

Draw two shapes on the sketch (e.g. a rectangle and a circle). Use the UP, DOWN, LEFT and RIGHT keys to control the movement of the selected shape. A shape is selected by either pressing a key ‘1’ or or key ‘2’ so that shape 1 or shape 2 can be picked up respectively.I want to select the shape by pressing the key "1" or "2", but they cannot run.`
int x = 0;
int y = 0;
int ex= 0;
int ey= 0;
void setup(){
size (400, 400);
}
void draw(){
background(80);
rect(x, y, 25,25);
ellipse(50, 50, 50, 50);
}
void keyPresse() {
if ( (key == '1')) {
if (keyCode == UP) {
y -= 10;
} else if (keyCode == DOWN) {
y += 10;
} else if (keyCode == LEFT) {
x -= 10;
} else if (keyCode == RIGHT) {
x += 10;
}
} else if ((key == '2')){
if (keyCode == UP) {
ey -= 10;
} else if (keyCode == DOWN) {
ey += 10;
} else if (keyCode == LEFT) {
ex -= 10;
} else if (keyCode == RIGHT) {
ex += 10;
}
}
}
There is a typo. The name of the key board callback is keyPressed. However, there are also some logical issues.
Crate an array for the x and y coordinates. And an index variabel (shape_i):
int x[] = new int[]{100, 100};
int y[] = new int[]{200, 100};
int shape_i = 0;
Draw the shapes on its position. (x[0], y[0]) is the position of the rectangle and (x[1], y[1]) is the position of the ellipse:
void draw(){
background(80);
rect(x[0], y[0], 25, 25);
ellipse(x[1], y[1], 50, 50);
}
Change the index (shape_i) when 1 or 2 is pressed. Change (x[shape_i], y[shape_i]) when an arrow key is pressed:
void keyPressed() {
if (key == '1') {
shape_i = 0;
} else if (key == '2') {
shape_i = 1;
} else if (keyCode == UP) {
y[shape_i] -= 10;
} else if (keyCode == DOWN) {
y[shape_i] += 10;
} else if (keyCode == LEFT) {
x[shape_i] -= 10;
} else if (keyCode == RIGHT) {
x[shape_i] += 10;
}
}
Complete example:
int x[] = new int[]{100, 100};
int y[] = new int[]{200, 100};
int shape_i = 0;
void setup(){
size (400, 400);
}
void draw(){
background(80);
rect(x[0], y[0], 25, 25);
ellipse(x[1], y[1], 50, 50);
}
void keyPressed() {
if (key == '1') {
shape_i = 0;
} else if (key == '2') {
shape_i = 1;
} else if (keyCode == UP) {
y[shape_i] -= 10;
} else if (keyCode == DOWN) {
y[shape_i] += 10;
} else if (keyCode == LEFT) {
x[shape_i] -= 10;
} else if (keyCode == RIGHT) {
x[shape_i] += 10;
}
}

How to exit or break from 'for loop' on Arduino C Program

I'm not new to the programming world, but for last 2 weeks I'm working with Arduino C++. I have a strange problem. In general, I have a 7-segment display that will act as an up counter when the condition is 01, down counter when the condition is 10, 00 for reset to zero, and 11 for stop (pause in that number).
The problem is, when the up or down counter is still counting and I've change the state, the 7-segment is still counting! Besides that, I've printed the status of state when state is changed, but nothing happens. I think maybe there's mistakes in my code, I need some advice from you guys. Here is my code:
*FYI, I've try to use break, return, also call loop(), but nothing happens
*saklar1 = switch1
*saklar2 = switch2
#include <SevSeg.h>
SevSeg sevseg;
const int saklar1 = 2;
const int saklar2 = 3;
void setup() {
byte sevenSegments = 1;
byte commonPins[] = {};
byte sevenPins[] = {11, 10, 9, 8, 7, 6, 5, 4};
bool resistor = true;
sevseg.begin(COMMON_CATHODE, sevenSegments, commonPins, sevenPins, resistor);
sevseg.setBrightness(90);
pinMode(saklar1, INPUT);
pinMode(saklar2, INPUT);
Serial.begin(9600);
}
void loop() {
checking(digitalRead(saklar1), digitalRead(saklar2));
}
void checking(bool saklar1, bool saklar2){
if (saklar1 == LOW && saklar2 == LOW) {
setSevSeg(0);
} else if (saklar1 == LOW && saklar2 == HIGH) {
for (int i=0; i<=9; i++) {
setSevSeg(i);
if (saklar1 != LOW || saklar2 != HIGH) {
Serial.println("STATE CHANGED"); //THIS LINE IS NOT EXECUTED OR PRINTED
checking(digitalRead(saklar1), digitalRead(saklar2));
}
}
} else if (saklar1 == HIGH && saklar2 == LOW) {
for (int i=9; i>=0; i--) {
setSevSeg(i);
if (saklar1 != HIGH || saklar2 != LOW) {
checking(digitalRead(saklar1), digitalRead(saklar2));
}
}
} else if (saklar1 == HIGH && saklar2 == HIGH) {
delay(100); //Stop
}
}
void setSevSeg(int num) {
sevseg.setNumber(num);
sevseg.refreshDisplay();
delay(500); //1 detik
}
In the following code, the control exits the for loop when the sensor value exceeds the threshold.
Reference: [https://www.arduino.cc/reference/en/language/structure/control-structure/break]
int threshold = 40;
for (int x = 0; x < 255; x++) {
analogWrite(PWMpin, x);
sens = analogRead(sensorPin);
if (sens > threshold) { // bail out on sensor detect
x = 0;
break;
}
delay(50);
}

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