Multiple buttons

Discussion about Z-Uno product. Visit http://z-uno.z-wave.me for more details.
Post Reply
Geirbakke
Posts: 15
Joined: 22 May 2017 16:09

Multiple buttons

Post by Geirbakke » 11 Oct 2017 23:39

HELP!

What am I doing wrong?
button 1-4 works great, relay switches on as it should.
but Button5 and 6 makes the relay flikker. turns on an off.

Code: Select all

#define Relay_PIN_1 9 //Relay 1 
#define Relay_PIN_2 10 //Relay 2 
#define Relay_PIN_3 11 //Relay 3 
#define Relay_PIN_4 12 //Relay 4 
#define Relay_PIN_5 13 //Relay 5 
#define Relay_PIN_6 14 //Relay 6 

#define Button1  8 //Button1
#define Button2  7 //Button2
#define Button3  6 //Button3

#define Button4  18 //Button4
#define Button5  19 //Button5
#define Button6  20 //Button6

#define Sensor_PIN_1  21 //Sensor 1 
#define Sensor_PIN_2  22 //Sensor 2 Garasje


// trengs til forsøk på toogle switch
int Button1State = 0;      // the current state of the output pin
int Button1Reading;           // the current reading from the input pin
int Button1Previous = 0;    // the previous reading from the input pin

int Button2State = 0;      // the current state of the output pin
int Button2Reading;           // the current reading from the input pin
int Button2Previous = 0;    // the previous reading from the input pin

int Button3State = 0;      // the current state of the output pin
int Button3Reading;           // the current reading from the input pin
int Button3Previous = 0;    // the previous reading from the input pin

int Button4State = 0;      // the current state of the output pin
int Button4Reading;           // the current reading from the input pin
int Button4Previous = 0;    // the previous reading from the input pin

int Button5State = 0;      // the current state of the output pin
int Button5Reading;           // the current reading from the input pin
int Button5Previous = 0;    // the previous reading from the input pin

int Button6State = 0;      // the current state of the output pin
int Button6Reading;           // the current reading from the input pin
int Button6Previous = 0;    // the previous reading from the input pin

long time = 0;         // the last time the output pin was toggled
long debounce = 200;   // the debounce time, increase if the output flickers

// stop


#define SWITCH_ON  255
#define SWITCH_OFF  0


ZUNO_SETUP_DEBUG_MODE(DEBUG_ON);


// Global variables to store data reported via getters
int currentRelay_1_Value = 0; //Relay 1 
int currentRelay_2_Value = 0; //Relay 2 
int currentRelay_3_Value = 0; //Relay 3 
int currentRelay_4_Value = 0; //Relay 4 
int currentRelay_5_Value = 0; //Relay 5 
int currentRelay_6_Value = 0; //Relay 6


int LastSensor_1_state = 0; //Sensor 2 Soverommet
word relaxMotion_1 = 0; //Sensor 1 Stuen
int LastSensor_2_state = 0; //Sensor 2 Soverommet
word relaxMotion_2 = 0; //Sensor 2 Soverommet

//int Disable_Sensors = 0; //Disable sensor z wave traffic

ZUNO_SETUP_SLEEPING_MODE(ZUNO_SLEEPING_MODE_ALWAYS_AWAKE);

ZUNO_SETUP_ASSOCIATIONS(ZUNO_ASSOCIATION_GROUP_SET_VALUE); // Send Basic Set to association group


// Set up 32 channels _MAX
ZUNO_SETUP_CHANNELS(
  ZUNO_SWITCH_BINARY(getterSwitch1, setterSwitch1),
  ZUNO_SWITCH_BINARY(getterSwitch2, setterSwitch2),
  ZUNO_SWITCH_BINARY(getterSwitch3, setterSwitch3),
  ZUNO_SWITCH_BINARY(getterSwitch4, setterSwitch4),
  ZUNO_SWITCH_BINARY(getterSwitch5, setterSwitch5),
  ZUNO_SWITCH_BINARY(getterSwitch6, setterSwitch6),
  ZUNO_SENSOR_BINARY_MOTION(getterSensor1),
  ZUNO_SENSOR_BINARY_MOTION(getterSensor2)
);

// the setup routine runs once when you press reset:
void setup() {
  Serial.begin();
    
  Serial.println("Setup");
  
  pinMode(Relay_PIN_1, OUTPUT); // setup pin as output
  pinMode(Relay_PIN_2, OUTPUT); // setup pin as output
  pinMode(Relay_PIN_3, OUTPUT); // setup pin as output
  pinMode(Relay_PIN_4, OUTPUT); // setup pin as output
  pinMode(Relay_PIN_5, OUTPUT); // setup pin as output
  pinMode(Relay_PIN_6, OUTPUT); // setup pin as output

  pinMode(Button1, INPUT_PULLUP);
  pinMode(Button2, INPUT_PULLUP);
  pinMode(Button3, INPUT_PULLUP);
  pinMode(Button4, INPUT_PULLUP);
  pinMode(Button5, INPUT_PULLUP);
  pinMode(Button6, INPUT_PULLUP);

  
  pinMode(Sensor_PIN_1, INPUT_PULLUP); // set Sensor 1 pin as input
  pinMode(Sensor_PIN_2, INPUT_PULLUP); // set Sensor 2 pin as input

  //pinMode(Sensor_A_PIN_1, OUTPUT);
  //pinMode(Sensor_A_PIN_2, OUTPUT);
  
}
// the loop routine runs over and over again forever:
void loop() {
  int currentSensor_1_State;
  int currentSensor_2_State;

 //if (Disable_Sensors != 0) {
 // Trigger motion and wait for relax (about 5 sec) before report idle
  currentSensor_1_State = !digitalRead(Sensor_PIN_1);
 // Serial.print("currentSensor_1_State: ");
  // Serial.println(currentSensor_1_State);
  if (currentSensor_1_State) {
    if (relaxMotion_1 == 0) {
      LastSensor_1_state = 1;
      zunoSendReport(7);
      //zunoSendToGroupSetValueCommand(CTRL_GROUP_1, SWITCH_ON);
    }
    relaxMotion_1 = 1900; // impirical for ~5 sec relax time
  }
  if (LastSensor_1_state == 1 && relaxMotion_1 == 0) {
    LastSensor_1_state = 0; 
    zunoSendReport(7);
    //zunoSendToGroupSetValueCommand(CTRL_GROUP_1, SWITCH_OFF);
  }
  if (relaxMotion_1) relaxMotion_1--;
  

  currentSensor_2_State = !digitalRead(Sensor_PIN_2);
  if (currentSensor_2_State) {
    if (relaxMotion_2 == 0) {
      LastSensor_2_state = 1;
      zunoSendReport(8);
      //zunoSendToGroupSetValueCommand(CTRL_GROUP_1, SWITCH_ON);
    }
    relaxMotion_2 = 1900; // impirical for ~5 sec relax time
  }
  if (LastSensor_2_state == 1 && relaxMotion_2 == 0) {
    LastSensor_2_state = 0; 
    zunoSendReport(8);
    //zunoSendToGroupSetValueCommand(CTRL_GROUP_1, SWITCH_OFF);
  }
  if (relaxMotion_2) relaxMotion_2--;
//  }
    



 
//button1
  Button1Reading = digitalRead(Button1); //Toggle switch for Alarm 
  if (Button1Reading == 0 && Button1Previous == 0 && millis() - time > debounce) {{ 
     Serial.println("0. ");
     
     if (Button1State == 0) {
      Button1State = 1;
      Serial.println("1. Button1State = 1");
      digitalWrite(Relay_PIN_1, 1);  // turn the relay on
      
     } else {
      Button1State = 0;
      Serial.println("2. Button1State = 0");
      digitalWrite(Relay_PIN_1, 0);   // turn the relay off   
  }
    time = millis(); 
    Button1Previous = Button1Reading;
    zunoSendReport(1);
  
   }       
 }

//button2
  Button2Reading = digitalRead(Button2); //Toggle switch for Alarm 
  if (Button2Reading == 0 && Button2Previous == 0 && millis() - time > debounce) {{ 
     Serial.println("0. ");
     
     if (Button2State == 0) {
      Button2State = 1;
      Serial.println("1. Button2State = 1");
      digitalWrite(Relay_PIN_2, 1);  // turn the relay on
      
     } else {
      Button2State = 0;
      Serial.println("2. Button2State = 0");
      digitalWrite(Relay_PIN_2, 0);   // turn the relay off   
  }
    time = millis(); 
    Button2Previous = Button2Reading;
    zunoSendReport(2);
  
   }       
 }

//button3
  Button3Reading = digitalRead(Button3); //Toggle switch for Alarm 
  if (Button3Reading == 0 && Button3Previous == 0 && millis() - time > debounce) {{ 
     Serial.println("0. ");
     
     if (Button3State == 0) {
      Button3State = 1;
      Serial.println("1. Button3State = 1");
      digitalWrite(Relay_PIN_3, 1);  // turn the relay on
      
     } else {
      Button3State = 0;
      Serial.println("2. Button3State = 0");
      digitalWrite(Relay_PIN_3, 0);   // turn the relay off   
  }
    time = millis(); 
    Button3Previous = Button3Reading;
    zunoSendReport(3);
  
   }       
 }

//button4
  Button4Reading = digitalRead(Button4); //Toggle switch for Alarm 
  if (Button4Reading == 0 && Button4Previous == 0 && millis() - time > debounce) {{ 
     Serial.println("0. ");
     
     if (Button4State == 0) {
      Button4State = 1;
      Serial.println("1. Button4State = 1");
      digitalWrite(Relay_PIN_4, 1);  // turn the relay on
      
     } else {
      Button4State = 0;
      Serial.println("2. Button4State = 0");
      digitalWrite(Relay_PIN_4, 0);   // turn the relay off   
  }
    time = millis(); 
    Button4Previous = Button4Reading;
    zunoSendReport(4);
  
   }       
 }

//button5
  Button5Reading = digitalRead(Button5); //Toggle switch for Alarm 
  if (Button5Reading == 0 && Button5Previous == 0 && millis() - time > debounce) {{ 
     Serial.println("0. ");
     
     if (Button5State == 0) {
      Button5State = 1;
      Serial.println("1. Button5State = 1");
      digitalWrite(Relay_PIN_5, 1);  // turn the relay on
      
     } else {
      Button5State = 0;
      Serial.println("2. Button5State = 0");
      digitalWrite(Relay_PIN_5, 0);   // turn the relay off   
  }
    time = millis(); 
    Button5Previous = Button5Reading;
    zunoSendReport(5);
  
   }       
 }


 //button6
  Button6Reading = digitalRead(Button6); //Toggle switch for Alarm 
  if (Button6Reading == 0 && Button6Previous == 0 && millis() - time > debounce) {{ 
     Serial.println("0. ");
     
     if (Button6State == 0) {
      Button6State = 1;
      Serial.println("1. Button6State = 1");
      digitalWrite(Relay_PIN_6, 1);  // turn the relay on
      
     } else {
      Button6State = 0;
      Serial.println("2. Button6State = 0");
      digitalWrite(Relay_PIN_6, 0);   // turn the relay off   
  }
    time = millis(); 
    Button6Previous = Button6Reading;
    zunoSendReport(6);
  
   }       
 }


  }
 
int getterSensor1() {
  Serial.println("Sensor_1_state:  ");
   Serial.println(LastSensor_1_state);
 if (LastSensor_1_state == 0) { // if button is pressed
    return SWITCH_ON;              // return "Triggered" state to the controller
  } else {                    // if button is released
    return SWITCH_OFF;                 // return "Idle" state to the controller
  }
}

int getterSensor2() {
  Serial.println("Sensor_2_state:  ");
     Serial.println(LastSensor_2_state);
 if (LastSensor_2_state == 0) { // if button is pressed
    return SWITCH_ON;              // return "Triggered" state to the controller
  } else {                    // if button is released
    return SWITCH_OFF;                 // return "Idle" state to the controller
  }
}


 


 


void setterSwitch1(int value) {
  Serial.print("setterSwitch1 Value: ");
  Serial.println(value);
  if (value > 0) {               // if greater then zero
    digitalWrite (Relay_PIN_1, 1); //turn the LED on (1 is the voltage level)
  } else {                         // if equals zero
    digitalWrite(Relay_PIN_1, 0);   //turn the LED off by making the voltage 0
  }
  currentRelay_1_Value = value;
}

int getterSwitch1(){
  return currentRelay_1_Value;
}

void setterSwitch2(int value) {
    Serial.print("setterSwitch2 Value: ");
  Serial.println(value);
  if (value > 0) {               // if greater then zero
    digitalWrite (Relay_PIN_2, 1); //turn the LED on (1 is the voltage level)
  } else {                         // if equals zero
    digitalWrite(Relay_PIN_2, 0);   //turn the LED off by making the voltage 0
  }
  currentRelay_2_Value = value;
}

int getterSwitch2(){
  return currentRelay_2_Value;
}


void setterSwitch3(int value) {
    Serial.print("setterSwitch3 Value: ");
  Serial.println(value);
  if (value > 0) {               // if greater then zero
    digitalWrite (Relay_PIN_3, 1); //turn the LED on (1 is the voltage level)
  } else {                         // if equals zero
    digitalWrite(Relay_PIN_3, 0);   //turn the LED off by making the voltage 0
  }
  currentRelay_3_Value = value;
}

int getterSwitch3(){
  return currentRelay_3_Value;
}

void setterSwitch4(int value) {
    Serial.print("setterSwitch4 Value: ");
  Serial.println(value);
    if (value > 0) {               // if greater then zero
    digitalWrite (Relay_PIN_4, 1); //turn the LED on (1 is the voltage level)
  } else {                         // if equals zero
    digitalWrite(Relay_PIN_4, 0);   //turn the LED off by making the voltage 0
  }
  currentRelay_4_Value = value;
}

int getterSwitch4(){
  return currentRelay_4_Value;
}

void setterSwitch5(int value) {
    Serial.print("setterSwitch5 Value: ");
  Serial.println(value);
    if (value > 0) {               // if greater then zero
    digitalWrite (Relay_PIN_5, 1); //turn the LED on (1 is the voltage level)
  } else {                         // if equals zero
    digitalWrite(Relay_PIN_5, 0);   //turn the LED off by making the voltage 0
  }
  currentRelay_5_Value = value;
}

int getterSwitch5(){
  return currentRelay_5_Value;
}

void setterSwitch6(int value) {
    Serial.print("setterSwitch6 Value: ");
  Serial.println(value);
    if (value > 0) {               // if greater then zero
    digitalWrite (Relay_PIN_6, 1); //turn the LED on (1 is the voltage level)
  } else {                         // if equals zero
    digitalWrite(Relay_PIN_6, 0);   //turn the LED off by making the voltage 0
  }
  currentRelay_6_Value = value;
}

int getterSwitch6(){
  return currentRelay_6_Value;
}


User avatar
PoltoS
Posts: 3837
Joined: 26 Jan 2011 19:36

Re: Multiple buttons

Post by PoltoS » 12 Oct 2017 00:31

The code is really big to check it... May be you can strip it to reproduce the problem on something smaller.

We had such problems before on older versions. Please try it on 2.1.1 to check if this is still a problem. The reason was the SPI changing pin states when communicating with the EEPROM

Geirbakke
Posts: 15
Joined: 22 May 2017 16:09

Re: Multiple buttons

Post by Geirbakke » 12 Oct 2017 09:05

If I make It smaller it works, Button 1-4 works but after 5 it does not.

Before I posted the forum message, I tried to upgrade Arduino to 1.8.5 and installed the 2.1.1 Board.
had the same trouble in Arduino 1.6.5 and FW 2.0.9

I have tried to rearange the pin setup for the buttons.

If I increases the "long debounce = 200;" the relay flickers slower.

schmidmi
Posts: 49
Joined: 01 Dec 2016 16:45
Location: Germany (Karlsruhe)

Re: Multiple buttons

Post by schmidmi » 12 Oct 2017 10:16

There is only one thing I can see:
You have some unnecessary brackets in your code.
The bracket before Serial.println("0. "); and after zunoSendReport(..); seem to be useless.

joergm6
Posts: 10
Joined: 03 Nov 2016 21:16

Re: Multiple buttons

Post by joergm6 » 12 Oct 2017 19:00

Roughly overflown: I think there's a design flaw here. "if (Button5Reading == 0 && Button5Previous == 0" seems to be triggered again immediately. I'm using it like this.

Code: Select all

  byte Button5Reading = digitalRead(Button5);
  if (Button5Reading != lastButton5State) { 
    lastButton5State = Button5Reading;
    if (lastButton5State == LOW) {
      Serial.println("press");
      if (Button5on == 0) {
        digitalWrite(Relay_PIN_5, HIGH);
        Button5on = 1;
      } else { 
        digitalWrite(Relay_PIN_5, LOW);
        Button5on = 0;
      }
      zunoSendReport(5);
    }
  }

Geirbakke
Posts: 15
Joined: 22 May 2017 16:09

Re: Multiple buttons

Post by Geirbakke » 12 Oct 2017 22:10

I fond the problem.
the long time=0; declaration should be unsigned long time = 0;

Now the switch are working as it should.

Thanks for looking at it anyway :D

Post Reply

Who is online

Users browsing this forum: No registered users and 6 guests