Go Down

Topic: Start/Stop Button on Arduino Mega (Read 412 times) previous topic - next topic

alfredfoby

So i am trying to stop an operation when the stop button is pressed and start an operation when start button is pressed. The problem I am having is when the button is pressed it is HIGH but when it is released it becomes LOW. So I am looking for a way to essentially have the stop/start button keep track of the fact that they have been pressed. Like for example if the stop button is pressed then it stops the operation but when it released and it is LOW then it doesn't affect the state it is in. I don't know if i'm making any sense just let me know so I can explain better.

I attached the arduino file for your reference.

alfredfoby

Code: [Select]
#include <Wire.h> //For I2C comms
#include <Adafruit_GFX.h>
#include <LedHelper.h>
#include "Adafruit_LEDBackpack.h"
#include "Adafruit_LiquidCrystal.h"

 
#include "Adafruit_FONA.h"
#include "Adafruit_MQTT.h"
#include "Adafruit_MQTT_FONA.h"
#include <SoftwareSerial.h>

 
SmallMatrix smallMatrix[3] = {SmallMatrix(0x70), SmallMatrix(0x71), SmallMatrix(0x72) };
LargeMatrix bigMatrix[3] = {LargeMatrix(0x73), LargeMatrix(0x74), LargeMatrix(0x75) };
Adafruit_LiquidCrystal lcd(0);

/*----------------------------------------PIN DEFINITIONS--------------------------------------*/
const int ESTOP_BREAK = 40;
const int LED_PWR = 22;
const int TRACO_24VDC = 23;

//Analog inputs
// Pressures
const int gas_input = A10; //PT0
const int gas_output = A12; //PT1
const int mast_output = A13; //PT2

float gInput_pressure = 0;
float gOutput_pressure = 0;
float mOutput_pressure = 0;


// Temperatures
const int MASTINPUT_tc = A7;
const int MASTOUTPUT_tc = A7;
const int GASPANEL_tc = A7;
const int O_TEMP_READ = 42; //multiplexer pin to read temp

float mastip_tc;
float mastop_tc;
float gasp_tc;


// gas sniffer
const int gas_sniffer = A8;

// Digital Inputs
const int start_input = 53;
const int stop_input = 31;
const int disp_estop = 48;
const int sch_status = 50;
const int moore_status = 52;
const int flame_status = 14;
const int rem_estop = 15;
 
// Digital Outputs
const int green_light = 47;
const int amber_light = 45;
const int red_light = 43;
const int esl_relay = 41;
const int lp_solenoid = 49;
const int vent_solenoid = 51;

/*----------------Other Variables----------------*/
int counter = 0;
unsigned long prevReadTime = 0;
unsigned long currentTime = 0;
const unsigned int READ_INTERVAL = 500;

unsigned long lastDebounce = 0;
unsigned long startDebounce = 0;
unsigned long stopDebounce = 0;
unsigned long debounceDelay = 100;

int greenButtonState = 0;
int redButtonState = 0;
int amberButtonState = 0;
int dispState = 0;

boolean pressedRed = false;
boolean notPressedRed = false;

int lastButtonState = LOW;
int lastStartButtonState = LOW;
volatile int pressureCeiling = 8000;

 
void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  TCCR5B = (TCCR5B & 0xF8) | 0x01; // Very important: Sets PWM frequency for PWM pins 44/45/46 to 31.4 KHz instead of 490Hz
  pinModeSetup();
  matrixSetup(" TURNING ON ", "");
 
}

void loop() {
  // put your main code here, to run repeatedly
  currentTime = millis();
  if((currentTime - prevReadTime) > READ_INTERVAL){
   
    prevReadTime = currentTime;
    gInput_pressure = gasInput_calc(10);
    gOutput_pressure = gasOutput_calc(10);
    mOutput_pressure = mastOutput_calc(10);

    bigMatrix[0].writeInt(gInput_pressure);
    bigMatrix[1].writeInt(gOutput_pressure);
    bigMatrix[2].writeInt(mOutput_pressure);
   
  }

  if(gas_sniffer > 160 || digitalRead(flame_status) == HIGH || digitalRead(moore_status) == HIGH || digitalRead(sch_status)= HIGH || digitalRead(esl_relay) == HIGH){
    digitalWrite(amber_light, HIGH);
    digitalWrite(disp_estop, HIGH);
    digitalWrite(lp_solenoid, LOW);
    //HMI to show fault
   
  }else {
    int reading = digitalRead(disp_estop);
    if(reading != lastButtonState){
      lastDebounce = millis();
    }
    if((millis() - lastDebounce) > debounceDelay){
      if(reading != dispState){
        dispState = reading;
       
        if (dispState == HIGH){
          if(gInputPressure < pressureCeiling){
            // HMI to display input Pressure (PT0)
            // read stop button - debounce button
            int newReading = digitalRead(stop_input);
            if(newReading != lastStopButtonState){
              stopDebounce = millis();
            }
            if((millis() - stopDebounce) > debounceDelay){
              redButtonState = newReading;
              if(redButtonState == HIGH || pressedRed == true){
                pressedRed = !pressedRed;
                digitalWrite(red_input, HIGH);
                digitalWrite(lp_solenoid, LOW);
              }else{
                digitalWrite(red_input, LOW);
              }
            }
            redButtonState = newReading;
           
          }
        }
      }
     
    }
    lastButtonState = reading;
  }
  lcdDisplay();

 
   
}

groundFungus

#2
Jul 01, 2020, 03:26 am Last Edit: Jul 01, 2020, 03:29 am by groundFungus
Sounds like you need to detect when the button becomes pressed, not when it is pressed or in other words you need to detect the transition from HIGH to LOW (or LOW to HIGH).  The state change detection example shows how to do that.

Where is the pinModeSetup() function.  Do the switches have pulldown or pullup resistors.
You will save everyone's time if you read and follow the forum guidelines.  https://forum.arduino.cc/index.php/topic,148850.0.html

dougp

So i am trying to stop an operation when the stop button is pressed and start an operation when start button is pressed.
Consider using a boolean flag as a run/stop signal.  When stop is pressed, reset the boolean.  Even if the switch bounces the flag isn't going to change back to set as long as the start switch isn't closed at the same time.  When start is pressed, set the flag.
I don't trust atoms.  They make up everything.

No private consultations undertaken!

alfredfoby

Sounds like you need to detect when the button becomes pressed, not when it is pressed or in other words you need to detect the transition from HIGH to LOW (or LOW to HIGH).  The state change detection example shows how to do that.

Where is the pinModeSetup() function.  Do the switches have pulldown or pullup resistors.
The code I have has more than 9000 characters so i wasn't allowed to post which is why i removed the pinModesetup. but each of those inputs have pull down resistors. Also I don't think that tutorial helps me because if the button is pressed once it should do something, but because you release the button the state of the button will actually change from HIGH to LOW.

alfredfoby

Consider using a boolean flag as a run/stop signal.  When stop is pressed, reset the boolean.  Even if the switch bounces the flag isn't going to change back to set as long as the start switch isn't closed at the same time.  When start is pressed, set the flag.
yes i did something similar to what you said but it doesn't seem to work appropriately, take a look at what i did maybe you can spot what went wrong.

Code: [Select]
const int ESTOP_BREAK = 40;
const int LED_PWR = 22;
const int TRACO_24VDC = 23;

//Analog inputs
// Pressures
const int gas_input = A10; //PT0
const int gas_output = A12; //PT1
const int mast_output = A13; //PT2

float gInput_pressure = 0;
float gOutput_pressure = 0;
float mOutput_pressure = 0;


// Temperatures
const int MASTINPUT_tc = A7;
const int MASTOUTPUT_tc = A7;
const int GASPANEL_tc = A7;
const int O_TEMP_READ = 42; //multiplexer pin to read temp

float mastip_tc;
float mastop_tc;
float gasp_tc;


// gas sniffer
const int gas_sniffer = A8;

// Digital Inputs
const int start_input = 53;
const int stop_input = 31;
const int disp_estop = 48;
const int sch_status = 50;
const int moore_status = 52;
const int flame_status = 14;
const int rem_estop = 15;

// Digital Outputs
const int green_light = 47;
const int amber_light = 45;
const int red_light = 43;
const int esl_relay = 41;
const int lp_solenoid = 49;
const int vent_solenoid = 51;

/*----------------Other Variables----------------*/
int counter = 0;
unsigned long prevReadTime = 0;
unsigned long currentTime = 0;
const unsigned int READ_INTERVAL = 500;

unsigned long lastDebounce = 0;
unsigned long startDebounce = 0;
unsigned long stopDebounce = 0;
unsigned long debounceDelay = 100;

int greenButtonState = 0;
int redButtonState = 0;
int amberButtonState = 0;
int dispState = 0;

boolean pressedRed = false;
boolean pressedGreen = false;

int lastButtonState = LOW;
int lastStartButtonState = LOW;
int lastStopButtonState = LOW;
volatile int pressureCeiling = 8000;
volatile int mastCeiling = 800;


void setup() {
 // put your setup code here, to run once:
 Serial.begin(9600);
 TCCR5B = (TCCR5B & 0xF8) | 0x01; // Very important: Sets PWM frequency for PWM pins 44/45/46 to 31.4 KHz instead of 490Hz
 pinModeSetup();
 matrixSetup(" TURNING ON ", "");
 
}

void loop() {
 // put your main code here, to run repeatedly
 currentTime = millis();
 if((currentTime - prevReadTime) > READ_INTERVAL){
  
   prevReadTime = currentTime;
   gInput_pressure = gasInput_calc(10);
   gOutput_pressure = gasOutput_calc(10);
   mOutput_pressure = mastOutput_calc(10);

   bigMatrix[0].writeInt(gInput_pressure);
   bigMatrix[1].writeInt(gOutput_pressure);
   bigMatrix[2].writeInt(mOutput_pressure);
  
 }

 if(gas_sniffer > 160 || digitalRead(flame_status) == HIGH || digitalRead(moore_status) == HIGH || digitalRead(sch_status)= HIGH || digitalRead(esl_relay) == HIGH){
   digitalWrite(amber_light, HIGH);
   digitalWrite(disp_estop, HIGH);
   digitalWrite(lp_solenoid, LOW);
   //HMI to show fault
  
 }else {
   int reading = digitalRead(disp_estop);
   if(reading != lastButtonState){
     lastDebounce = millis();
   }
   if((millis() - lastDebounce) > debounceDelay){
     if(reading != dispState){
       dispState = reading;
      
       if (dispState == HIGH){
         if(gInputPressure < pressureCeiling && mOutputPressure < mastCeiling){
           // HMI to display input Pressure (PT0) and mast pressure(PT2)
           // read stop button - debounce button
           int newReading = digitalRead(stop_input);
           if(newReading != lastStopButtonState){
             stopDebounce = millis();
           }
           if((millis() - stopDebounce) > debounceDelay){
             redButtonState = newReading;
             if(redButtonState == HIGH || pressedRed == true){
               pressedRed = true;
               digitalWrite(red_input, HIGH);
               digitalWrite(lp_solenoid, LOW);
               int greenReading = digitalRead(green_input);
               if(greenReading != lastStartButtonState){
                 startDebounce = millis();
               }
               if((millis() - startDebounce) > debounceDelay){
                 greenButtonState = greenReading;
                 if(greenButtonState == HIGH || pressedGreen == true){
                   pressedRed = false;
                   pressedGreen = true;
                   digitalWrite(green_input, HIGH);
                   digitalWrite(lp_solenoid, HIGH);
                  
                 }
               }
               greenButtonState = greenReading;
             }else{
               digitalWrite(red_input, LOW);
             }
           }
           redButtonState = newReading;
          
         }
       }
     }
    
   }
   lastButtonState = reading;
 }
 lcdDisplay();

 
  
}

void pinModeSetup(){
 pinMode(ESTOP_BREAK, OUTPUT);
 digitalWrite(ESTOP_BREAK, HIGH);
 pinMode(LED_PWR, OUTPUT);
 digitalWrite(LED_PWR, HIGH);
 pinMode(TRACO_24VDC, OUTPUT);
 digitalWrite(TRACO_24VDC, HIGH);

 //Digital Inputs
 pinMode(start_input, INPUT_PULLUP);
 pinMode(stop_input, INPUT_PULLUP);
 pinMode(disp_estop, INPUT_PULLUP);
 pinMode(sch_status, INPUT);
 pinMode(moore_status, INPUT);
 pinMode(flame_status, INPUT);
 pinMode(rem_estop, INPUT_PULLUP);

 //Digital Outputs
 pinMode(green_light, OUTPUT);
 pinMode(amber_light, OUTPUT);
 pinMode(red_light, OUTPUT);
 pinMode(green_light, OUTPUT);
 pinMode(lp_solenoid, OUTPUT);
 pinMode(vent_solenoid, OUTPUT);

 digitalWrite(green_light, LOW);
 digitalWrite(amber_light, LOW);
 digitalWrite(red_light, LOW);
 digitalWrite(esl_relay, LOW);
 digitalWrite(lp_solenoid, LOW);
 digitalWrite(vent_solenoid, LOW);


}

groundFungus

The state change detection allows you to do something when the switch goes from low to high and ignore the high to low transition.
You will save everyone's time if you read and follow the forum guidelines.  https://forum.arduino.cc/index.php/topic,148850.0.html

alfredfoby

The state change detection allows you to do something when the switch goes from low to high and ignore the high to low transition.
Oh i see what you mean now, I'll try it Thank you very much

Go Up