Issues having 2 buttons in existing script

Hi all,
So i have been working on this script for sometime and couldnt work it out. I have done a lot of google and youtube research to get to this point. Long story short,

Arduino boots and gets to the 'Ready' screen awaiting input
Press and release button A
Relay is activated (this will turn on a motor with a cam to press button b repeatedly)
Button B will be pressed by the motor and the screen will show the number of times it has been pressed
After 10 presses Relay powering the motor is de-activated and LED is lit.

So far i have been unable to work out how to add button A to this project, however everything is working with buttonB being the control button. Could anyone help me out? I will be using an Arduino Nano to run this.

// Clickermajig Switch Break-in machine
// Print design modified from "Don's Switch-breaker-in-er-er"

#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

#define BUTTON_PINB 2
#define LED_PIN 3
#define RELAY_PIN 5
#define SCREEN_WIDTH 128 // OLED display width,  in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels

// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
#define OLED_RESET     4 // Reset pin # (or -1 if sharing Arduino reset pin)
Adafruit_SSD1306 oled(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);


unsigned lastCount = 0;
unsigned currentCount = 0;
unsigned endCount = 10;
char buffer[100];

void setup() {
  pinMode(LED_PIN, OUTPUT);
  pinMode(RELAY_PIN, OUTPUT);

  Serial.begin(9600);

  // initialize OLED display with address 0x3C for 128x64
  if (!oled.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
    Serial.println(F("SSD1306 allocation failed"));
    while (true);
  }

  delay(2000);         // wait for initializing
  oled.clearDisplay(); // clear display
  oled.setTextSize(2);          // text size
  oled.setTextColor(WHITE);     // text color

//show splash screen
  oled.setCursor(18,5);
  oled.println("Sabitech");
  oled.setCursor(24, 25);
  oled.println("Designs");
  oled.display();      // show on OLED
  delay(5000);
  oled.clearDisplay();


//Ready for use  
  oled.setCursor(18, 25);
  oled.println("Ready...");
  oled.display();      // show on OLED

  pinMode(BUTTON_PINB, INPUT_PULLUP);
  attachInterrupt(digitalPinToInterrupt(BUTTON_PINB), buttonPressed, RISING);
}

void loop() {


  if (lastCount != currentCount && currentCount < endCount) {
    Serial.println(currentCount); // print count to Serial Monitor
    
    // OLED stuff
    oled.clearDisplay(); // clear display
    oled.setCursor(0, 0);
    oled.println("Count:");
    snprintf(buffer, 100, "%u", currentCount);
    oled.println(buffer); // display count
    oled.display();      // show on OLED

    //Relay run
      digitalWrite(RELAY_PIN, HIGH);

    //Update counter
    lastCount = currentCount;
  }

  else if (lastCount != currentCount && currentCount == endCount) {
    Serial.println(currentCount); // print count to Serial Monitor
    
    // OLED stuff
    oled.clearDisplay(); // clear display
    oled.setCursor(0, 0);
    oled.println("Count:");
    snprintf(buffer, 100, "%u", currentCount);
    oled.println(buffer); // display count
    oled.println("COMPLETE:)");
    oled.display();      // show on OLED

    //LED stuff
    digitalWrite(LED_PIN, HIGH);


    //Relay stuff goes here (i.e. turn relay off)
    digitalWrite(RELAY_PIN, LOW);
    
    
    //Update counter
    lastCount = currentCount;
  }

  else{
    // Stuff that only happens if shit goes wrong (i.e. currentCount is bigger than endCount)
    oled.println("ERROR OCCURED!");
  }

}

void buttonPressed() {
  currentCount++;
}

depending on how many hours

needed.
It might be more effective to read through a real beginner programming-tutorial

The code above uses an interrupt to count the button-presses.
Interrupts are a somehow advanced tecnique and have their own pros and cons

I am unsure if understood right

A motor is started through a relais and the running motor is pressing a button?

You count the button-presses and after 10 button-presses you switch off the motor?

Is this some kind of an Art-Project?
Depending an the real physics = the mechanical thing of this microcontroller-application a different solution might work better.
It depends on the real situation.

You have asked for the detail "How do I add a button A to ..."

The program that you posted in combination with your question shows, that you did not yet fully understand what the code does.
Helping you in this situation with ready to add code would mean to increase your depenendancy on other users knowledge and their will to help.

This kind of help will come. ---- .... ---- ... over time.
I just want to say this would ne non optimal way of realising this project
You should start learning the basics of programming.

Take a look into this tutorial:

Arduino Programming Course

It is easy to understand and has a good mixture between explaining important concepts and example-codes to get you going. So give it a try and report your opinion about this tutorial.

best regards Stefan

CPlusPlusScript

Thanks for the reply. long story short it is a kind of robot to break-in keyboard switches.
so the goal is to have it boot, i press button A and then it starts the motor controlled by the relay.
the motor has a cam on it that will rotate and press the button B 10 times.
Once it has counted to 10, the relay will stop the motor as the process is complete.

sorry wha? i don't see?

consider (simulated on my hardware)

// Clickermajig Switch Break-in machine
// Print design modified from "Don's Switch-breaker-in-er-er"

# include <Wire.h>

// ---------------------------------------------------------
#undef MyHW
#ifdef MyHW
enum { WHITE, SSD1306_SWITCHCAPVCC };
struct Adafruit_SSD1306 {
    Adafruit_SSD1306 (int w, int h, TwoWire *wire, bool rst) { }
    bool begin        (int c, int r)    { return true; }
    void clearDisplay (void)    { }
    void display      (void)    { }
    void setCursor    (int c, int r)    { }
    void setTextSize  (int x)           { }
    void setTextColor (int x)           { }
    void print        (const char *s)   { Serial.println (s); }
    void println      (const char *s)   { Serial.println (s); }
};

# define LED_PIN      13
# define RELAY_PIN    12
const byte ButPins [] = { A1, A2 };

// -------------------------------------
#else
# include <Adafruit_GFX.h>
# include <Adafruit_SSD1306.h>

# define LED_PIN      3
# define RELAY_PIN    5
const byte ButPins [] = { 2, 4 };       // ??
#endif

// ---------------------------------------------------------
#define SCREEN_WIDTH 128 // OLED display width,  in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels

// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
#define OLED_RESET     4 // Reset pin # (or -1 if sharing Arduino reset pin)

Adafruit_SSD1306 oled (SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);

#define Nbut sizeof(ButPins)
byte butState [Nbut];

#define CntMax  10
unsigned count;

char buffer[100];

enum { Off = HIGH, On = LOW };

// -----------------------------------------------------------------------------
void
dispOled (
    const char *buffer )
{
    oled.clearDisplay ();
    oled.setCursor    (0, 0);
    oled.println      (buffer);
    oled.display      ();
}

// -----------------------------------------------------------------------------
void loop ()
{
    for (unsigned n = 0; n < Nbut; n++)  {
        byte but = digitalRead (ButPins [n]);
        if (butState [n] != but)  {
            butState [n] = but;
            delay (10);         // debounce

            if (LOW == but) {   // pressed
                if (0 == n) {   // button A
                    digitalWrite (RELAY_PIN, On);
                    digitalWrite (LED_PIN,   Off);
                }
                else if  (1 == n) { // button B
                    count++;

                    if (CntMax == count)  {
                        digitalWrite (RELAY_PIN, Off);
                        digitalWrite (LED_PIN,   On);
                        sprintf (buffer, "Count: %2d Complete", count);
                    }
                    else
                        sprintf (buffer, "Count: %2d", count);
                    dispOled (buffer);
                }
            }
        }
    }
}

// -----------------------------------------------------------------------------
void setup () {
    Serial.begin (9600);

    digitalWrite (LED_PIN,   Off);
    digitalWrite (RELAY_PIN, Off);

    pinMode (LED_PIN,   OUTPUT);
    pinMode (RELAY_PIN, OUTPUT);

    // initialize OLED display with address 0x3C for 128x64
    if (!oled.begin (SSD1306_SWITCHCAPVCC, 0x3C)) {
        Serial.println (F ("SSD1306 allocation failed"));
        while (true);
    }
    delay (2000);         // wait for initializing
    oled.clearDisplay (); // clear display
    oled.setTextSize (2);          // text size
    oled.setTextColor (WHITE);     // text color
    //show splash screen
    oled.setCursor (18,5);
    oled.println ("Sabitech");
    oled.setCursor (24, 25);
    oled.println ("Designs");
    oled.display ();      // show on OLED
    delay (5000);
    oled.clearDisplay ();
    //Ready for use
    oled.setCursor (18, 25);
    oled.println ("Ready...");
    oled.display ();      // show on OLED

    for (unsigned n = 0; n < Nbut; n++)  {
        pinMode (ButPins [n], INPUT_PULLUP);
        butState [n] = digitalRead (ButPins [n]);
    }
}

The code you posted uses an external interrupt to read switch B. The interrupt handling violates several important rules that you must follow to make an interrupt work reliably. Also, there is no debouncing of the switch input.

You have not even declared a pin for button A (unless I missed it). If it were my project, I would at least have written and tested a separate test sketch just to prove that button A actually works. That means, you actually haven't tried adding it. If you don't even know how to begin doing that, it probably means that both, the program is beyond your understanding, and that your level of understanding is low.

Given that the program is complex, but also extremely flawed, your best bet is to abandon it and learn how to do it yourself.

You have discovered how pretentious and broken, technical projects mostly are, in the "commons" of YT and similar brag sites. You can utilize some of the information that is presented there, but it is not a college where you can learn because it's too hard to sort out the garbage.

I appreciate your reply. I have tested this but abandonded it as i thought it would be best to reach out to the community for advice with the original known working version of the code.

In my testing debounce has not been needed as it is working, perhaps this is bad form but there is limited information regarding this and conflicting notes preferring one over the other.

In relation to the interrupt, this was suggested by one of the members in the Arduino Discord as it was aparently a better method than my count option i had previously.

I am slowly learning but i learn by doing and have been slowly adding to this by process of compile test, compile test and repeat.

If i'm honest i don't want to learn the intricacies of a system i will seldom use but if i am able to cobble something together that works that is good for me.

Thank you for this,
I will give it a run through this evening. i appreciate your time and help.

you really need to understand the code i posted and if it does what you want

don't you worry, i will be having a read through to check my understanding before running it.
looks like i get most of it at a glance but will need to check my understanding. some looks very exotic

i'm curious what you think is exotic? just something you haven't seen before? a learning experience

Yes, the debounce issue is moot if you are just running once. But the interrupt issues are serious, and you have to be willing to drill down and cover all the bases. From what you've said, that doesn't interest you.

The quick solution: If it currently works but without the button, attach a toggle switch to the power, as a substitute for the button. To run a test, just turn on the toggle and let it run to completion.

Then you are a much sharper programmer than I.

definitely a learning experience, i also thought Setup always had to be before loop.
lots of reading to do tonight!
Also not seen pins / stuff set with "const byte ButPins [] = { 2, 4 }; // ??"
but i guess it makes sense to make the button pins constant values on these pins.
few other bits in there but going to work through it tonight and see what i can understand and what i cant haha!

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.