Hello I am making a project where I have 4 buttons, a menu button (also to confirm the selectors), two selectors and a stop/reset button. I am finding it difficult to make the stop button work even though I have it in a while loop. Can somebody please help? I have already added in other while loops the " && stopState != HIGH" but it doesn't seem to work. Basically this button needs to reset at any given point the whole code as if I just pressed the arduino reset button.
Thanks!
const int pos = 0;
const int buzzer = 8;
const int menuBtn = A0;
const int select1Btn = A1;
const int select2Btn = A2;
const int stopBtn = A3;
const int temp = A4;
const int led1 = A5;
const int led2 = A6;
const int led3 = A7;
const int led4 = A8;
int trigger = 0;
int triggerVal = 0;
int tempValue;
double voltLm35 = 0.0;
double tempLm35 = 0.0;
double tempC = 0;
double tempF =0;
int cont = 0;
int cont2 = 0;
int s1State = 0;
int s1OldState = 0;
int s2State = 0;
int s2OldState = 0;
int menuState = 0;
int menuOldState = 0;
int stopState = 0;
int stopOldState = 0;
int C = 0;
int F = 0;
int contLoop =0;
int contC =0;
int contF = 0;
int ascendingTemp =0;
int lm35Value;
void setup()
{
Serial.begin(9600);
//initialize buzzer as output
pinMode(buzzer, OUTPUT); // buzzer
//initialize buttons and lm35 as input
pinMode(menuBtn, INPUT); // menu
pinMode(select1Btn, INPUT); // selection 1
pinMode(select2Btn, INPUT); // selection 2
pinMode(stopBtn, INPUT); // stop
pinMode(temp, INPUT); // lm35
}
void loop()
{
while (cont == 0){
delay(600);
stopState = 0;
stopOldState = 0;
while(stopState != HIGH ){ // check if stop button is pressed stopState == stopOldState
//while(contLoop == 0){
stopState = digitalRead(stopBtn);
Serial.print("\nCelcius or Farenheit? (select1: Celsius, select2: Farenheit)");
cont2 = 0;
choosingScale();
stopState = digitalRead(stopBtn);
delay(800);
if(C == 1){
choosingTriggerC();
delay(800);
digitalWrite(buzzer,LOW);
Serial.print("\nStarting Temperature: ");
menuOldState = 0;
menuState = 0;
while(stopState != HIGH){
delay(600);
menuState = digitalRead(menuBtn);
tempValue = analogRead(temp);
tempC=(((5 * tempValue)*100)/1024);
Serial.print("Temperature: ");
Serial.print(tempC,2); //2 decimal variable
Serial.println("°C");
Serial.println("\n trigger: ");
Serial.print(triggerVal);
ascendingTemp = triggerVal/4;
Serial.print("\n ascending: ");
Serial.print(ascendingTemp);
if(tempValue <= ascendingTemp){
digitalWrite(led1, HIGH);
digitalWrite(led2, LOW);
digitalWrite(led3, LOW);
digitalWrite(led4, LOW);
}
if(tempValue >= ascendingTemp*2){
digitalWrite(led1, HIGH);
digitalWrite(led2, HIGH);
digitalWrite(led3, LOW);
digitalWrite(led4, LOW);
}
if(tempValue >= ascendingTemp*3 ){
digitalWrite(led1, HIGH);
digitalWrite(led2, HIGH);
digitalWrite(led3, HIGH);
digitalWrite(led4, LOW);
}
if(tempValue >= trigger){
digitalWrite(buzzer,HIGH);
digitalWrite(led1, HIGH);
digitalWrite(led2, HIGH);
digitalWrite(led3, HIGH);
digitalWrite(led4, HIGH);
}
}
}
if (F == 1){
choosingTriggerF();
delay(800);
digitalWrite(buzzer,LOW);
Serial.print("\nStarting Temperature: ");
menuOldState = 0;
menuState = 0;
while(stopState != HIGH){
delay(800);
menuState = digitalRead(menuBtn);
tempValue = analogRead(temp);
tempC=(((5 * tempValue)*100)/1024);
tempF = (tempC * 9.0 / 5.0) + 32.0;
Serial.print(" Temperature: ");
Serial.print(tempF,2); //2 decimal variable
Serial.println("°F");
Serial.println("\n trigger: ");
Serial.print(triggerVal);
ascendingTemp = triggerVal/4;
Serial.print("\n ascending: ");
Serial.print(ascendingTemp);
if(tempValue <= ascendingTemp){
digitalWrite(led1, HIGH);
digitalWrite(led2, LOW);
digitalWrite(led3, LOW);
digitalWrite(led4, LOW);
}
if(tempValue >= ascendingTemp*2){
digitalWrite(led1, HIGH);
digitalWrite(led2, HIGH);
digitalWrite(led3, LOW);
digitalWrite(led4, LOW);
}
if(tempValue >= ascendingTemp*3 ){
digitalWrite(led1, HIGH);
digitalWrite(led2, HIGH);
digitalWrite(led3, HIGH);
digitalWrite(led4, LOW);
}
if(tempValue >= trigger){
digitalWrite(buzzer,HIGH);
digitalWrite(led1, HIGH);
digitalWrite(led2, HIGH);
digitalWrite(led3, HIGH);
digitalWrite(led4, HIGH);
}
}
//contLoop++;
}
} //stop state loop
} //while loop
cont++; //ends all the system
delay(10); // Delay a little bit to improve simulation performance
}
void choosingTriggerF(){
s1OldState = 0;
s2OldState = 0;
menuState = 0;
menuOldState = 0;
cont2 = 0;
int trigger = 50; //start temp
int contAdd =0;
int contSub = 0;
Serial.print("\n Choose a Trigger (50 F - 68 F): " );
Serial.print("\n Trigger: 50" ); //
while(cont2 == 0 && stopState != HIGH){
delay(200); //helps for last button press to not be counted in this
s1State = digitalRead(select1Btn); //checks if there are button presses in each loop
s2State = digitalRead(select2Btn);
menuState = digitalRead(menuBtn);
if (s1State != s1OldState) {
if(trigger < 67){ //prevents it from going past 68
trigger += 2;
floor(trigger);
s1OldState = 0; //returns old state to LOW
Serial.print("\n Trigger:" );
Serial.print(trigger );
}
}
else if (s2State != s2OldState) { //prevents it from going below 50
if(trigger > 51){
trigger -= 2;
s2OldState = 0; //returns old state to LOW
floor(trigger);
Serial.print("\n Trigger:" );
Serial.print(trigger);
}
}
if( menuOldState != menuState){
Serial.print("\n Trigger Confirmed: " );
Serial.print(trigger );
menuState = 0;
menuOldState = menuState; //old state and new both to LOW
cont2++;
}
}
return trigger;
}
////////////////////////////////////////////////////////////////////////
void choosingTriggerC(){
s1OldState = 0;
s2OldState = 0;
menuState = 0;
menuOldState = 0;
cont2 = 0;
int trigger = 10;
int contAdd =0;
int contSub = 0;
Serial.print("\n Choose a Trigger (10 C - 20 C): " );
Serial.print("\n Trigger: 10" );
while(cont2 == 0 && stopState != HIGH){
delay(200); //helps for last button press to not be counted in this
s1State = digitalRead(select1Btn); //checks if there are button presses in each loop
s2State = digitalRead(select2Btn);
menuState = digitalRead(menuBtn);
if (s1State != s1OldState) {
if(trigger < 19){ //prevents it to go past 20
trigger += 2;
s1OldState = 0; //returns old state to LOW
Serial.print("\n Trigger:" );
Serial.print(trigger );
}
}
else if (s2State != s2OldState) { //prevents it to go below 8
if(trigger > 11){
trigger -= 2;
s2OldState = 0; //returns old state to LOW
Serial.print("\n Trigger:" );
Serial.print(trigger);
}
}
if( menuOldState != menuState){
Serial.print("\n Trigger Confirmed: " );
Serial.print(trigger);
triggerVal = trigger;
menuState = 0;
menuOldState = menuState; //old state and new both to LOW
cont2++;
}
}
return trigger;
}
////////////////////////////////////////////////////////////////////////////////////////
void choosingScale(){
contC = 0;
contF = 0;
while(cont2 == 0 && stopState != HIGH){ //only executes one time
s1State = digitalRead(select1Btn); //reads the selector states (pressed or not)
s2State = digitalRead(select2Btn);
menuState = digitalRead(menuBtn); //reads menu state
if (s1State != s1OldState) { // if selector1 was pressed Celcius was chosen
if ( contC == 0){ //will execute only one time
Serial.print("\n Celcius" );
C = 1;
F = 0;
contC++;
s1OldState = s1State;
}
}
else if (s2State != s2OldState) {
if(contF == 0){
Serial.print("\n Farenheit" );
F = 1;
C = 0;
contF++;
s2OldState = s2State;
}
}
if( menuOldState != menuState){ //when menuBtn is pressed confirm the scale
if (F == 1){
Serial.print("\n Scale confirmed: Farenheit" );
}
if (C == 1){
Serial.print("\n Scale confirmed: Celcius" );
}
menuState = 0;
menuOldState = menuState; //both states low
cont2++;
}
}
}