Button single, double, triple press

Hello,
I want to make 5V, 9V, 12V charger for powerbank supporting
Qualcomm Quick Charge 3.0 A.
I used button for switching between 5V, 9V, 12V,
single press = 5V
double press = 9V
triple press = 12V

I tried use this code, but I'm beginer and I don't know how to make
working code.

Please help me with the code.

#include <QC3Control.h>
QC3Control quickCharge(8, 7);

int Button = 10;
int qcdelay = 0;
int qc = 0;
int curbut;
int lastbut = 0;


void setup() {
  pinMode(Button, INPUT);
}

void loop() {
  curbut = digitalRead(Button);
  qcdelay = qcdelay + 1;
  if (curbut != lastbut){
    if (curbut == HIGH){
      qc = qc + 1;
      qcdelay = 0;
    }
  }
  if (qcdelay >= 200){
    if (qc == 1){
      quickCharge.set5V();
      qc = 0;
    }
    if (qc == 2){
      quickCharge.set9V();
      qc = 0;
    }
    if (qc == 3){
      quickCharge.set12V();
      qc = 0;
    }
    qcdelay = 0;
  }
  lastbut = curbut;
  delay(1);
}

how is your button wired up?

This picture show it

ProDigy_Ziege:
I used button for switching between 5V, 9V, 12V,
single press = 5V
double press = 9V
triple press = 12V

Even if your code works it would be very easy for a user to do the wrong number of button clicks by accident.

If you insist on using a single button the code in this link shows how to implement different click types.

...R

#include <QC3Control.h>

QC3Control quickCharge(8, 7);

const byte Button = 10;
byte qc;


void setup() {
  pinMode(Button, INPUT);
}

void loop() {
  if (pushButton()) {
    if (qc >= 2) qc = 0;
    else qc++;
  }
  switch (qc) {
    case 0: {
        quickCharge.set5V();
      }
    case 1: {
        quickCharge.set9V();
      }
    case 2: {
        quickCharge.set12V();
      }
  }
}

bool pushButton() {
  static int previousState = LOW;
  static byte debounceFlag = 0;
  static unsigned long debounceTimestamp;
  const static int risingEdge = HIGH - LOW;
  const static int fallingEdge = LOW - HIGH;
  const static unsigned long DebounceInterval = 3UL;//change constant value to change debounce time
  bool b = false;
  if (debounceFlag == 1) {
    if (millis() - debounceTimestamp >= DebounceInterval) debounceFlag = 0;
  }
  else {
    int currentState = digitalRead(Button);
    int stateChange = currentState - previousState;
    if (stateChange == fallingEdge) {
      debounceTimestamp = millis();
      debounceFlag = 1;
    }
    else if (stateChange == risingEdge) {
      debounceTimestamp = millis();
      debounceFlag = 1;
      b = true;
    }
    previousState = currentState;
  }
  return b;
}

This way is too complicated for me (I said "I'm beginer"). I now use very easy code where if I press button it switch to 9V, press again for 12V and again for 5V back. It works and i is very easy, but thank you for your help.

ProDigy_Ziege:
This way is too complicated for me (I said "I'm beginer"). I now use very easy code where if I press button it switch to 9V, press again for 12V and again for 5V back. It works and i is very easy, but thank you for your help.

Beginners should start with the blink sketch and not try to complete a graduate project.
But, if you study the code I presented, hopefully you will learn a little more, as I modified your code.
Robin2 is right. You have no way of knowing if you are outputting 5, 9 or 12 volts or which "press" you are on.

Perehama:
Beginners should start with the blink sketch and not try to complete a graduate project.
But, if you study the code I presented, hopefully you will learn a little more, as I modified your code.
Robin2 is right. You have no way of knowing if you are outputting 5, 9 or 12 volts or which "press" you are on.

This isn't a graduate project. It's not even close to what I would call hard, it's literally just one thing.

ProDigy_Ziege:
This way is too complicated for me (I said "I'm beginer"). I now use very easy code where if I press button it switch to 9V, press again for 12V and again for 5V back. It works and i is very easy, but thank you for your help.

I still recommend investigating why your original code didn't work, because that will understand what you did wrong and what you can do differently when you try it next time. And I think I have found a problem.

You have cases set up for single-click, double-click, and triple-click. But what if you quadruple click? If you do 4 or more rapid clicks, you will go outside the range you are expecting, qc will never get reset and nothing will get triggered again unless you happen to click 65,000 more times to roll it back over to 0 and land back into the window. And the odds of that actually happening are quite low.

When you aren't sure about what's happening, print things to Serial. Right after you add 1 to qc, print its value to the Serial monitor so you can see what it's doing. Find some places to print out the value of qcdelay so you can see if it is working as you expect it. Bottom line is to get more information about the internal workings of your code and make sure that every little gear and lever is doing exactly what you expect it to be doing and not going all crazy.

Jiggy-Ninja:
This isn't a graduate project. It's not even close to what I would call hard, it's literally just one thing.

It was a hyperbole.

Perehama:
It was a hyperbole.

I've often wondered what your nice black hat is called :slight_smile: :slight_smile:

...R

Robin2:
I've often wondered what your nice black hat is called :slight_smile: :slight_smile:

...R

https://akubra.com.au/products/the-arena-mto
Australia's finest.