trying to use momentary push buttons to increase and decrease delay time HELP!!!

nothing is happening on the serial monitor. The led pin has nothing plugged in as this is the part of a larger project im testing it before adding it below i the code and a describtion of the prototyping board i have set up. PLEASE HELP

int rtime = 1;
int newrtime = 1;
int up;
int down;
void setup() {
// put your setup code here, to run once:
pinMode(2, INPUT);
pinMode(3, INPUT);
pinMode(4, OUTPUT);
Serial.begin(9600);
}

void loop() {
// put your main code here, to run repeatedly:
up = digitalRead(2);
down = digitalRead(3);
if(rtime != newrtime)
{
if(up == HIGH)
{
rtime++;
Serial.println("up");
Serial.println(rtime);
}
else if(down == HIGH)
{
rtime--;
Serial.println(rtime);
Serial.println("off");
}
Serial.println(rtime);
}
newrtime = rtime;

digitalWrite(4, HIGH);
delay(rtime*1000);

set up:

push button 1 pin 1 --> arduino 5V
push button 1 pin 2 --> digital pin 2 and 470 resistor --> ground

push button 2 pin 1 --> arduino 5V
push button 2 pin 2 --> digital pin 3 and 470 resistor --> ground

ground --> arduino grd

delay(rtime*1000);

Did you know delay(); stops your sketch from executing for that period of time.

Delays on the Arduino should always be done (99.99% of the time) using BWD (blink without delay).

Demonstration code for several things at the same time.
http://forum.arduino.cc/index.php?topic=223286.0


Wire your switches as S3 is wired and then look for a LOW to see if the switch is pushed.

Then use:

pinMode(2, INPUT_PULLUP);
pinMode(3, INPUT_PULLUP);
. . .
if(up == LOW)
. . .


else if(down == HIGH). this should be outside the first if()function and HIGH should then be LOW too.


For switch debounce, suggest you scan the switches every ~50ms.

I tried that way wiring and programing and still no change it is acting like neither button is being pushed

There are some issues in your code

  • The code you post is incomplete. I can not see where you turn off LED
  • You used the delay() function, which is a blocking function will make Arduino miss the pressing event.
  • You need to check whether rtime > 0

What does your code do for?

eventually this will be used to change the timing lights are on and off one push button for up one for down. this is just a test to make sure its reading the buttons and its not reading either currently

Did you incorporate ALL the things mentioned in the post ?

When you make changes, and still have problems, always show us the new sketch.

yes second part of the description i mention its part of a larger project and that this is just the test program and set up

When you make changes, and still have problems, always show us the new sketch.

Always show us a full schematic of your circuit too.

Hi,

Please read the post at the start of any forum , entitled "How to use this Forum".
OR
http://forum.arduino.cc/index.php/topic,148850.0.html.
Then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

Thanks.. Tom... :slight_smile:

Here’s a quick drawing of the new set up based on the first reply I’ll post the new code in a minute

picture post attempt 2

here is the new code after reply 1

int rtime = 1;
int newrtime = 1;
int up;
int down;
void setup() {
  // put your setup code here, to run once:
pinMode(2, INPUT_PULLUP);
pinMode(3, INPUT_PULLUP);
pinMode(4, OUTPUT);
Serial.begin(9600);
}

void loop() {
  // put your main code here, to run repeatedly:
  up = digitalRead(2);
  down = digitalRead(3);
if(rtime != newrtime)
{
if(up == LOW)
    {
      rtime++;
      Serial.println("up");
      Serial.println(rtime);
    }
    else if(down == LOW)
    {
      rtime--;
      Serial.println(rtime);
      Serial.println("down");
    }
      Serial.println(rtime);
}
   newrtime = rtime;

   digitalWrite(4, HIGH);
   delay(rtime);
   Serial.println("hi");
}

Does this all make sense to you ?

//Version 1.00

#define isPUSHED                LOW

byte up;
byte down;

byte lastUpSwitchState;
byte lastDownSwitchState;

int rtime                       = 1;
int newrtime                    = 1;

const byte upSwitch             = 2;
const byte downSwitch           = 3;
const byte myLED                = 4;
const byte heartBeatLED         = 13;

unsigned long currentMillis;
unsigned long heartBeatMillis;
unsigned long switchMillis;
unsigned long myLEDmillis;

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

  pinMode(heartBeatLED, OUTPUT);
  pinMode(myLED, OUTPUT);

  pinMode(upSwitch, INPUT_PULLUP);
  pinMode(downSwitch, INPUT_PULLUP);

  pinMode(4, OUTPUT);

} //END of setup()

//*****************************************************************************
void loop()
{
  //leave this line of code at the top of loop()
  currentMillis = millis();

  //*******************************
  //toggle the heartbeat LED every 1/2 second
  if (currentMillis - heartBeatMillis >= 500)
  {
    //start the TIMER
    heartBeatMillis = currentMillis;

    //toggle LED
    digitalWrite(heartBeatLED, !digitalRead(heartBeatLED));
  }

  //*******************************
  //check the switch(es) every 50ms
  if (currentMillis - switchMillis >= 50)
  {
    //start the TIMER
    switchMillis = currentMillis;

    checkSwitch();
  }

  //*******************************
  //Time to toggle the LED ?
  if (currentMillis - myLEDmillis >= rtime * 1000ul)
  {
    //start the TIMER
    myLEDmillis = currentMillis;

    //toggle the myLED
    digitalWrite(myLED, !digitalRead(myLED));

    Serial.println("hi");
  }

} //END of loop()


//                          c h e c k S w i t c h ( )
//*****************************************************************************
void checkSwitch()
{
  byte currentState;

  //*******************************
  //upSwitch
  currentState = digitalRead(upSwitch);

  if (lastUpSwitchState != currentState)
  {
    //update to the current state
    lastUpSwitchState = currentState;

    //we have just gone to the isPUSHED state?
    if (currentState == isPUSHED)
    {
      rtime++;

      if (rtime >= 10)
      {
        //don't go over 10
        rtime = 10;
      }

      //restart the TIMER
      myLEDmillis = currentMillis;

      Serial.println("up");
      Serial.println(rtime);
    }

  }

  //*******************************
  //downSwitch
  currentState = digitalRead(downSwitch);

  if (lastDownSwitchState != currentState)
  {
    //update to the current state
    lastDownSwitchState = currentState;

    //we have just gone to the isPUSHED state?
    if (currentState == isPUSHED)
    {
      rtime--;

      if (rtime < 1)
      {
        //don't go under 1
        rtime = 1;
      }

      //restart the TIMER
      myLEDmillis = currentMillis;

      Serial.println("down");
      Serial.println(rtime);
    }

  }

  //*******************************

} //END of checkSwitch()

//*****************************************************************************

in "if(rtime != newrtime)" is always going to return false becuase newrtime always = rtime

and your delay is 1 millisecond maybe you mean 1000 for 1 second

for the most part yes so basically if this is the first part of my project this would replace the loop part of that program

int ranNum;
int rtime;
int runtime;
void setup() 
{
// Setup 8 output ports for LED's

pinMode(2, OUTPUT);
pinMode(3, OUTPUT);
pinMode(4, OUTPUT);
pinMode(5, OUTPUT);
Serial.begin(9600);
}
void loop() 
{
      Serial.println(ranNum);
      ranNum = random(2,6);
      digitalWrite(ranNum, HIGH);
      delay(rtime);
      digitalWrite(ranNum, LOW);
      delay(50);
}

as for your original code, maybe this is what you wanted?

int rtime = 0;
int up;
int down;
void setup() {
  // put your setup code here, to run once:
pinMode(2, INPUT_PULLUP);
pinMode(3, INPUT_PULLUP);
pinMode(4, OUTPUT);
Serial.begin(9600);
}

void loop() {
  // put your main code here, to run repeatedly:
  up = digitalRead(2);
  down = digitalRead(3);

if(up == LOW){
      rtime++; //rtime = rtime+100; for 100 ms increments 
      Serial.println("up");
      Serial.println(rtime);
    }
    if(down == LOW){
      rtime--;
      Serial.println(rtime);
      Serial.println("down");
    }

   digitalWrite(4, HIGH);
   delay(rtime);
   Serial.println("hi");
}

edited: rtime increment comment

does that code use the diagram i drew up cause im still getting no change

ronnoclax48:
does that code use the diagram i drew up cause im still getting no change

if "up is low, rtime increments by 1 aka"1 milisecond"
if "down" is low it does the opposite.

then the sketch pauses passing delay the value of rtime

tho im not sure what happens if rtime become negative being that it could pass delay a - number

i think the mix/max values of the int are -32,768 to 32,767

Did you try the sketch in post #12 ?

larryd:

Did you try the sketch in post #12 ?

should that sketch work for your program as well