Pages: [1]   Go Down
Author Topic: How do i implement button state of change in this sketch turning pin array onoff  (Read 567 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 5
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I have a program that i am running on my arduino uno and i want to implement button to turn on and off the code that i have.I AM NEW as all people seem to be saying! i want to change the provided sketch that when button is pressed once the sketch will start and keep going until i push button again and turning off I checked out the tutorial on button but of course nothing seems to tell you how to write into an existing sketch either . any help with this matter would be appreciated. Thanks in advance for your help.Is there a discussion on how to insert button state of change or a site that i can go to to understand how to do this.This sketch has a button but button has to be held in order to keep going what i would like to do is keep going till i push button one more time then all turns off.

Here is the sketch that i have;

Code:
/*
 You can control how fast the star shoots thanx to the
 variable called "waitNextLed
 waitNextLed variable will change the speed
 You can also control the length of the star's "tail"
 through the variable "tail length"
 connect 10k ohm resistor between pin A0 and ground
 connect 5v pos to one side of button and the other to A0*/

// Variable declaration
const int buttonPin = A0;
int pinArray [] = { 2,3,4,5,6,7,8,9,10,11, }; // Array where I declare the pins connected to the LEDs
int controlLed = 13;
int waitNextLed = 45; // Time before I light up the next LED
int tailLength = 10; // Number of LEDs that stay lit befor I start turning them off, thus the tail
int lineSize = 10; // Number of LEDs connected (which also is the size of the pinArray)
int buttonState = 0;// I assign the sens of the different Pins
void setup()
{
int i;
pinMode (controlLed, OUTPUT);
for (i=0; i< lineSize; i++)
{
pinMode(pinArray, OUTPUT);
}
}
// Main loop
void loop()
{
  int i;
   // read the state of the pushbutton value:
  buttonState = digitalRead(buttonPin);
   // check if the pushbutton is pressed.
  // if it is, the buttonState is HIGH:
  if (buttonState == HIGH) {     // turn LED on:    
int tailCounter = tailLength; // I set up the tail length in a counter
digitalWrite(controlLed, HIGH); // I make sure I enter the loop indicating it with this LED
for (i=0; i<lineSize; i++)
{
digitalWrite(pinArray,HIGH); // I light up consecutively the LEDs
delay(waitNextLed); // This time variable controles how fast I light them up
if (tailCounter == 0)
{
digitalWrite(pinArray[i-tailLength],LOW); // I turn off the LEDs depending on my tailLength
}
else
if (tailCounter > 0)
tailCounter--;
}
for (i=(lineSize-tailLength); i<lineSize; i++)
{
digitalWrite(pinArray,LOW); // I turn off the LEDs
delay(waitNextLed);} // This time variable controles how fast I light them upm, and turn off as well
}}


Sorry to Arrch from california & PaulS from seattle wa for not using the forum correctly but the link that i went to from Arrch's reply sent me to Nick Gammon from melbourne australia which stated that in section 2 that there were some suggestions highlighted for me and my first post was in one of those three? smiley-grin

 smiley-eek confused

I have looked at the button state of change but maybe i am not getting how to implement into this sketch as i would like!
I'll go back and read it all again maybe missing something that can't be spelled out for me?

Sorry If i offend anyone.Hopefully in the right forum  smiley-grin
« Last Edit: June 30, 2013, 02:30:12 pm by outatime4u » Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 549
Posts: 46090
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Part of the guidelines that you didn't read, or didn't feel applies to you, concerned how to post code. Your code doesn't really look like that, does it?

Code:
   // read the state of the pushbutton value:
  buttonState = digitalRead(buttonPin);
   // check if the pushbutton is pressed.
  // if it is, the buttonState is HIGH:
  if (buttonState == HIGH) {     // turn LED on:   

Changing this to "if the switch is pressed now and was not before" is a one line of code change. If you'd really read, and understood, the example, you'd know which line (the last one) to change, and how.
Logged

0
Offline Offline
Sr. Member
****
Karma: 3
Posts: 268
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Just spitballing here, but would something like this work for you?  I haven't tested it but it should toggle On_Off with each button press and release.  Obviously bounce would be an issue but there are plenty of debounce examples out there.  I guess you could also have a missed press if you release at the exact moment after it tests for buttonPress and last_button_press and before it updates last_button_press with the buttonPress value.  I like to use interrupts for this type of thing but I know a lot of people shy away from that.

Code:
boolean On_Off = true;
boolean buttonState = true;
boolean last_button_state = true;

//check to see if the button was pressed and released
//it will detect the change from low to high when the
//button is released
buttonState = digitalRead(buttonPin);
if(buttonState == true && last_button_state == false) On_Off = !On_Off;
last_button_state = buttonState;

//and then test On_Off to decide whether to run your code of not
if(On_Off)
{
  //do all of your stuff here
}
Logged

Arduino Uno;
Mega328

Saskatchewan
Offline Offline
Sr. Member
****
Karma: 15
Posts: 348
When the going gets weird, the weird turn pro. - Hunter S. Thompson
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Most people are going to press the button and wait until it stops or starts then release the button. I think you want a button that will flip a flag when pressed and then do nothing until released and pressed again. You can use the flag to allow the code to run or not.

Oddly I had just done similar code for using a Leonardo to make a sequential shifter for racing sims. (I ended up doing a whole dashboard because ... lots of pins)

Code:
// you'll need some global variables like this
byte buttonPin = 2;                          // whatever pin the button is on
boolean runFlag = false;                // you'll use this flag to allow the code to run if true
const unsigned long DebounceTime = 20;  // how long to debounce the button

// call this function once every loop
// it will flip the runFlag when pressed then do nothing until released
// assumes you've got a button between buttonPin and ground
// with INPUT_PULLUP set during setup
void handleButton(void)
{
  static boolean pressed = false;     // static variables don't die
  int currentState = digitalRead(buttonPin); // read current state
  if (!pressed && currentState == LOW)       // detect fresh press
  {
    delay(DebounceTime);                     // debounce it
    if (digitalRead(buttonPin) == LOW)       // check again
    {
      runFlag = !runFlag;                  // flip our flag
      pressed = true;                        // set our Press flag
    }
  }
  if (pressed && currentState == HIGH)       // check if we should release
  {
    delay(DebounceTime);                     // debounce it
    if (digitalRead(buttonPin) == HIGH)      // check again
    {
      pressed = false;                       // reset the flag to allow another press
    }
  }
}

I shown the logic of handing the button you'll have to figure out how to adapt it to your needs.

Here's a clue:

if (runFlag)
{
     your code here
}
Logged

0
Offline Offline
Tesla Member
***
Karma: 115
Posts: 8930
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
when button is pressed once the sketch will start and keep going until i push button again and turning off

First look at #7 below on how to post your code. You can go back and use the "modify" for your post. Below are examples of "toggle" type code that might be of use. You might search the forum for "toggle" and "flag" for other examples.

http://forum.arduino.cc/index.php/topic,148850.0.html


Code:
//zoomkat LED button toggle test 11-08-2012

int button = 5; //button pin, connect to ground as button
int press = 0;
boolean toggle = true;

void setup()
{
  pinMode(13, OUTPUT); //LED on pin 13
  pinMode(button, INPUT); //arduino monitor pin state
  digitalWrite(5, HIGH); //enable pullups to make pin 5 high
}

void loop()
{
  press = digitalRead(button);
  if (press == LOW)
  {
    if(toggle)
    {
      digitalWrite(13, HIGH);   // set the LED on
      toggle = !toggle;
    }
    else
    {
      digitalWrite(13, LOW);    // set the LED off
      toggle = !toggle;
    }
  }
  delay(500);  //delay for debounce
}


Code:
const int buttonPin = 2;     // the number of the pushbutton pin
const int ledPin =  13;      // the number of the LED pin

byte oldPinState = HIGH;

void setup ()
  {
  pinMode (buttonPin, INPUT_PULLUP); 
  pinMode (ledPin, OUTPUT);
  }  // end of setup

void loop ()
  {
  byte pinState = digitalRead (buttonPin);
  if (pinState != oldPinState)
    {
    delay (10);  // debounce
    if (pinState == LOW)
      digitalWrite (ledPin, !digitalRead (ledPin));  // toggle pin
    oldPinState = pinState;  // remember new state
    }  // end of if pin state change
  }  // end of loop
Logged

Consider the daffodil. And while you're doing that, I'll be over here, looking through your stuff.   smiley-cool

Offline Offline
Newbie
*
Karma: 0
Posts: 5
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thank you all for the input now i must decipher what i have been given to understand smiley-eek
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 5
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Jimmy 60
  What i am looking to do is push a button to turn pin array on and will keep running until i push button again then stop, once button is pushed again it starts all over again.
 Wish i could understand what everyone here is telling me short of writting it into the code that i posted
Spent most of my morning and afternoon,with wife giving me dirty looks to find something else to do, trying to understand what every post on this discussion was trying to implement.
 Still confused  smiley-eek-blue
 I understand that all of you have giving me hints here but i am not good at understanding what is written right in front of me i am a hands on type of person.
 Thanks for your help
Quote
Here's a clue:

if (runFlag)
{
     your code here
}
Sorry Jimmy60 i don't understand your clue and how to write it in maybe i should just sit down for a few months of reading to maybe understand
Logged

London
Offline Offline
Edison Member
*
Karma: 23
Posts: 1065
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I understand that all of you have giving me hints here but i am not good at understanding what is written right in front of me i am a hands on type of person.
Pseudo code:
int count = 1;
setup()
loop()
if (button pressed && count == 1),  // first time
while (button pressed); //do nothing. Wait for button to be released.
count++;
Do your 'turn on' stuff here.
if (button pressed && count ==2),  //second time
while (button pressed); //do nothing. Wait for button to be released.
count--;
Do your 'turn off' stuff here.

If you don't wait for the button to be released, then a long press could be counted as two pushes. Depending on the rest of your code, a 'long press' could be just a few microseconds.
Logged

Pages: [1]   Go Up
Jump to: