Hello Guys,
I am trying to figure out why is that when I use the function state_change once, it all works fine. But when I try to use it with a different argument, it does't behave the way it should be.
What I want is to check if a digital input (limit switch down) goes high, if so, execute a portion of a code for one clock cycle only. I want to use the same function to check a distinct digital input (limit switch high) and run another potion of code just for a single clock cycle.
This is the entire code, I am using Atmega32_8MHz.
#include <ShiftRegister74HC595.h>
#include <Arduino.h>
#include <TM1637Display.h>
// TM1637
#define CLK 25
#define DIO 26
TM1637Display lift_display(CLK, DIO);
// Buttons
#define BT_UP 27
#define BT_DW 26
#define BT_INS_UP 30
#define BT_INS_DW 31
// Calls
#define CALL_F0 21
#define CALL_F1 22
#define CALL_F2 23
#define CALL_F3 24
// Limits Switches
#define UP_LIM_SW 2
#define DW_LIM_SW 1
#define UP_STOP_SW 0
#define DW_STOP_SW 29
// Sensors
#define PHOTOCELL2 12
#define EXT_ALARM1 7
#define EXT_ALARM2 6
#define THERMO 5
#define DOOR_OPEN_BT 4
#define PHOTOCELL1 3
//Security
#define SC2 16
#define SC1 17
#define SC3 18
#define SC4 19
#define SC5 20
// Shift register
#define OUT_DATA 15
#define OUT_STB 14
#define OUT_CLK 13
ShiftRegister74HC595 lift(1, OUT_DATA, OUT_CLK, OUT_STB);
unsigned int current_floor=0, destination_floor;
// State change function
int buttonPushCounter_up = 0; // counter for the number of button presses
int buttonState_up = 0; // current state of the button
int lastButtonState_up = 0; // previous state of the button
///////////////////
int buttonPushCounter_dw = 0; // counter for the number of button presses
int buttonState_dw = 0; // current state of the button
int lastButtonState_dw = 0; // previous state of the button
void setup(){
pinMode(BT_UP, INPUT); ////Inputs
pinMode(BT_DW, INPUT);
pinMode(BT_INS_UP, INPUT);
pinMode(BT_INS_DW, INPUT);
pinMode(CALL_F0, INPUT);
pinMode(CALL_F1, INPUT);
pinMode(CALL_F2, INPUT);
pinMode(CALL_F3, INPUT);
pinMode(UP_LIM_SW, INPUT);
pinMode(DW_LIM_SW, INPUT);
pinMode(UP_STOP_SW, INPUT);
pinMode(DW_STOP_SW, INPUT);
pinMode(PHOTOCELL2 , INPUT);
pinMode(EXT_ALARM1, INPUT);
pinMode(EXT_ALARM2 , INPUT);
pinMode(THERMO , INPUT);
pinMode(DOOR_OPEN_BT , INPUT);
pinMode(PHOTOCELL1, INPUT);
pinMode(SC2, INPUT);
pinMode(SC1, INPUT);
pinMode(SC3, INPUT);
pinMode(SC4, INPUT);
pinMode(SC5, INPUT);
pinMode(DIO, OUTPUT); //// Outputs
pinMode(CLK, OUTPUT);
pinMode(OUT_DATA, OUTPUT);
pinMode(OUT_STB, OUTPUT);
pinMode(OUT_CLK, OUTPUT);
Serial.begin(9600);
lift_display.setBrightness(7);
}
void loop()
{ int i=0;
boolean call = digitalRead(CALL_F0) || digitalRead(CALL_F1) || digitalRead(CALL_F2) || digitalRead(CALL_F3);
if ( digitalRead(CALL_F0)== HIGH)
destination_floor=0;
else if(digitalRead(CALL_F1) == HIGH)
destination_floor=1;
else if(digitalRead(CALL_F2) == HIGH )
destination_floor=2;
else if(digitalRead(CALL_F3) == HIGH)
destination_floor=3;
int difference = destination_floor - current_floor;
Serial.println("-----------------");
Serial.print("Current floor is ");
Serial.println(current_floor, DEC);
Serial.print("Destination floor is ");
Serial.println(destination_floor, DEC);
Serial.print("Difference is ");
Serial.println(difference, DEC);
delay(1000);
switch (call) {
case HIGH:
while (difference > 0) { //// lift is going up
difference = destination_floor - current_floor;
uint8_t UP_HIGH[] = { B01100000 }; //// High speed
lift.setAll(UP_HIGH);
if (state_change_up(UP_STOP_SW) == 1){
buttonPushCounter_up = 0; // counter for the number of button presses
i++;
Serial.print("UP_STOP_SW ==> i= ");
Serial.println(i, DEC);}
buttonPushCounter_up = 0;
if (state_change_dw(DW_STOP_SW) == 1){
buttonPushCounter_dw = 0; // counter for the number of button presses
i++;
Serial.print("DW_STOP_SW ==> i= ");
Serial.println(i, DEC);
}
if (i==2) {
current_floor++;
lift_display.showNumberDec(current_floor);
Serial.print("current_floor= ");
Serial.println(current_floor, DEC);
i=0;}
if (difference == 1){
uint8_t UP_LOW[] = { B01010000 };
lift.setAll(UP_LOW);
}}
lift.setAllLow();
case LOW:
lift.setAllLow();
break;
}}
////////// State Change function variables
int state_change_up(int input_pin)
{
buttonState_up = digitalRead(input_pin);
if (buttonState_up != lastButtonState_up) {
if (buttonState_up == HIGH) {
buttonPushCounter_up++;}
delay(50);
}
lastButtonState_up = buttonState_up;
Serial.println(buttonPushCounter_up, DEC);
delay(250);
return buttonPushCounter_up;
}
//////////////////////////
int state_change_dw(int input_pin)
{
buttonState_dw = digitalRead(input_pin);
if (buttonState_dw != lastButtonState_dw) {
if (buttonState_dw == HIGH) {
buttonPushCounter_dw++;}
delay(50);
}
lastButtonState_dw = buttonState_dw;
return buttonPushCounter_dw;
}
The two digital inputs to monitor are UP_STOP_SW and DW_STOP_SW.
Thank you,