Beginner coding help

Hello,
I am really new to arduino and coding. I come from a different background.
I want to change the value of variable in my setup.
so if the button was pressed increment by a value and when released after a period of time move on to run the loop.

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

//#include <LiquidCrystal.h>  //Default Arduino display Librarey is included 
     #define SCREEN_WIDTH 128 // OLED display width, in pixels
    #define SCREEN_HEIGHT 64 // OLED display height, in pixels
    
    Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
 Button Charge_Button(3, 25, false, true);
int batt_h = 2;

void setup() {
  // put your setup code here, to run once:
if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
  Serial.println(F("SSD1306 allocation failed"));
  while (true);}
  Charge_Button.begin();
  Charge_Button.read();
  if (Charge_Button.wasPressed())
   batt_h +.1;
  Serial.println(batt_h);
  if (Charge_Button.pressedFor(1000))
  loop;
}

void loop() {
  // put your main code here, to run repeatedly:

}

Your topic was MOVED to its current forum category as it is more suitable than the original

I think this is a way to do what you want:

  // Check for button presses until a long press
  while (!Charge_Button.pressedFor(1000))
  {
    Charge_Button.read();
    if (Charge_Button.wasPressed())
    {
      // Button pressed. Increment value.
      batt_h += 0.1;
      Serial.println(batt_h);
    }
  }

I'm going to guess this is probably the code you want in loop(). However, it doesn't make sense to add .1 to an integer and you should never call loop from loop(). batt_h +.1 doesn't do anything.

Once I do a long press after incrementing my voltage will it go to the next stage of my code?

Hey,
Yeah I tested it in setup and seen it doesn’t run.
I’m adding .1 as cut off voltage for batteries is different.
Ok so I have it functioning for when button pressed it increments, but I want it to move on to next part of code once I do a long press.

Or if I created an array
Cutoff_voltage =[1,2,3] how would I use the button to select one of these values

@johnwasser has given you code that will work if you change batt_h to a float type. It will keep incrementing until the button is pressed for more than 1000ms.

many thanks for the help and reply

Sorry to pester you again, what would be an example I could put after johns code to show when button is pressed for 1second that it exits the while loop? could i put a print function?

Yes.

thank you @johnwasser. As I am increasing my coding skills,
what about an attachinterrupt to increase or decrease the PWM while the code is running to control current.

would I be best to do it on the Rising or falling when the button is pushed? the documentation is beneficial I was just wondering is there any "real" world examples

As you asked the answer is yes

I would use FALLING because then I could use the internal pull-up resistor instead of an external pull-down resistor.

1 Like

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