8 buttons ,3 states and PJON Arduino

Joined
Jan 15, 2022
Messages
1
Reaction score
0
Hi i need help witch code down below. Everything works with a single button but i need to have 8 buttons and i cant figure it out. Anyone can help please ?
Thanks.

C:
#include <PJONSoftwareBitBang.h>



const int recieverid = 45;           // Address of the reciever to send data

PJONSoftwareBitBang bus(11);         // Address of this device



const int button = 2;                // Gpio toogle switch pin

 

unsigned long previousMillis = 0;   // Mills time stored when hold pressedd

const long sendingInterval = 200;   // Sending interval when hold pressed

int holdCounter = 0;                // Counter variable when hold pressed

int holdInterval = 15;              // Max counter when hold pressed



int bounceTime = 50;                 // Debounce time for Gpio pin

int holdTime = 250;                  // Time after which hold state happends

int doubleTime = 500;                // Time which you need to double press



int lastReading = LOW;               // Last reading state

int hold = 0;                        // Hold state

int single = 0;                      // Single predd state



long onTime = 0;                    // On time when held

long lastSwitchTime = 0;            // Last switch time



void setup() {

  pinMode(button, INPUT);

  bus.strategy.set_pin(12);        // PJON bus pin

  bus.begin();                     // Start PJON bus

  Serial.begin(115200);

  Serial.println("Starting");

  Serial.print("PJON - Sender's device id: ");

  Serial.println(bus.device_id());  // PJON this  device ID

}



void loop() {

         bus.update();            // Update PJON buss

         unsigned long currentMillis = millis();

         int reading = digitalRead(button);

      



//////////////////////////////////////////BUTTON LOGIC/////////////////////////////////



//First pressed

  if (reading == HIGH && lastReading == LOW) {

    onTime = millis();

  }



///////////////////////////Held///////////////////////////

  if (reading == HIGH && lastReading == HIGH) {

      if ((millis() - onTime) > holdTime && holdCounter < holdInterval) {

      if (currentMillis - previousMillis >= sendingInterval) {

      previousMillis = currentMillis;

      bus.send(recieverid, "1C", 2);

      hold = 1;

      holdCounter++;

      Serial.println("Holding           |      Send C");

      

   }

 }

}



///////////////////////////Released///////////////////////////

  if (reading == LOW && lastReading == HIGH) {

    if (((millis() - onTime) > bounceTime) && hold != 1) {

      onRelease();

    }

    if (hold == 1) {

      hold = 0;

      holdCounter = 0;

      Serial.print("Held              |      holdCounter is: ");

      Serial.println(holdCounter);

    }

  }

  lastReading = reading;

 

///////////////////////////Single press///////////////////////////

  if (single == 1 && (millis() - lastSwitchTime) > doubleTime) {

    bus.send(recieverid, "1A", 2);

    single = 0;

    Serial.println("Single press      |      Send A");

  }

}



void onRelease() {



  if ((millis() - lastSwitchTime) >= doubleTime) {

    single = 1;

    lastSwitchTime = millis();

    return;

  }



///////////////////////////Double press///////////////////////////

  if ((millis() - lastSwitchTime) < doubleTime) {

    bus.send(recieverid, "1B", 2);

    single = 0;

    lastSwitchTime = millis();

    Serial.println("Double press      |      Send B");

  }

}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top