ADDING SINGLE PUSH BUTTON TO EXISTING CODE USING BRICK SHIELD V4 HELP PLEASe

I would like to add a small pushbutton to my existing code that basically when pressed will turn on both my servo and led lights and when pressed again will shut them off. i am using a brick shield v4 and plugging the buckled cable into the A0 port on the side of the shield. Here are the links to the pushbutton and shield i am using as well as the existing code which i have working correctly.

http://www.trossenrobotics.com/store/p/6365-Electronic-Brick-Shield-V4.aspx

http://www.trossenrobotics.com/store/p/6359-Electronic-Brick-Mini-Push-Button.aspx

#include <Servo.h> //include the servo libary - a bunch of code for a host of servo instructions y9ou can use and don't have to develop yourself

Servo myservo; // create servo object to control a servo

int pos = 0; // variable to store the servo position
int delay1 = 0; // variable to store the delay between servo arm's movement info

int ledPin = 13; // LED connected to digital pin 13
long randOn = 0; // Initialize a variable for the ON time
long randOff = 0; // Initialize a variable for the OFF time

void setup()

{

myservo.attach(9); // attaches pin 9 of the micro controller to the servo object - the servo's "control wire"

randomSeed (analogRead (0)); // randomize
pinMode(ledPin, OUTPUT); // sets the digital pin as output

}

void loop()

{
pos=random(65,115); // ramdomly set the servo arm postion between 65 & 115 degrees. "90" is "straight ahead"
delay1=random(50, 500); // randomly set delay between servo movement between 50 and 500 milliseconds
myservo.write(pos); // tell servo to go to position specified by variable ‘pos’ value
delay(delay1); // waits randomly between 50ms and 500 ms between servo movements

randOn = random (100, 1200); // generate ON time between 0.1 and 1.2 seconds
randOff = random (200, 900); // generate OFF time between 0.2 and 0.9 seconds
digitalWrite(ledPin, HIGH); // sets the LED on
delay(randOn); // waits for a random time while ON
digitalWrite(ledPin, LOW); // sets the LED off
delay(randOff); // waits for a random time while OFF

It appears that mini push button board has some hardware debounce (there's a resistor and a cap on the board, and I think it's safe to assume that is what they're there for).

If I am understanding correctly, you want to toggle your servo/led activity on and off with separate activations of the button.

boolean active = false;
boolean lastState = LOW;
boolean currentState = LOW;
byte buttonPin = A0;

void setup(){
  
  
}

void loop(){
  
  currentState = digitalRead(buttonPin);
  
  //When lastState is LOW and currentState is HIGH, we have detected a rising edge transition
  //ie the input has just gone high.  This is when we toggle are active boolean
  if(lastState == LOW && currentState == HIGH){
    //toggle active state
    active != active;
  }
  
  lastState = currentState;
  
  if(active){
    //Perform actions when active = true;
    
  }
  else{
    //Perform actions when active = false;
    
  }
  
  
  
}

I was unclear exactly what you wanted to happen in those two states, but simply fill the two if blocks with the code you wish to have executed for each state.

so are you saying this is what the code should look like?

#include <Servo.h> //include the servo libary - a bunch of code for a host of servo instructions you can use and don't have to develop yourself
Servo myservo; // create servo object to control a servo

boolean active = false;
boolean lastState = LOW;
boolean currentState = LOW;
byte buttonPin = A0;

int pos = 0; // variable to store the servo position
int delay1 = 0; // variable to store the delay between servo arm's movement info

int ledPin = 13; // LED connected to digital pin 13
long randOn = 0; // Initialize a variable for the ON time
long randOff = 0; // Initialize a variable for the OFF time

void setup(){

myservo.attach(9); // attaches pin 9 of the micro controller to the servo object - the servo's "control wire"

randomSeed (analogRead (0)); // randomize
pinMode(ledPin, OUTPUT); // sets the digital pin as output

}

void loop(){

currentState = digitalRead(buttonPin);

//When lastState is LOW and currentState is HIGH, we have detected a rising edge transition
//ie the input has just gone high. This is when we toggle are active boolean
if(lastState == LOW && currentState == HIGH){

pos=random(65,115); // ramdomly set the servo arm postion between 65 & 115 degrees. "90" is "straight ahead"
delay1=random(50, 500); // randomly set delay between servo movement between 50 and 500 milliseconds
myservo.write(pos); // tell servo to go to position specified by variable ‘pos’ value
delay(delay1); // waits randomly between 50ms and 500 ms between servo movements

randOn = random (100, 1200); // generate ON time between 0.1 and 1.2 seconds
randOff = random (200, 900); // generate OFF time between 0.2 and 0.9 seconds
digitalWrite(ledPin, HIGH); // sets the LED on
delay(randOn); // waits for a random time while ON
digitalWrite(ledPin, LOW); // sets the LED off
delay(randOff); // waits for a random time while OFF

//toggle active state
active != active;
}

lastState = currentState;

if(active){
//Perform actions when active = true;

pos=random(65,115); // ramdomly set the servo arm postion between 65 & 115 degrees. "90" is "straight ahead"
delay1=random(50, 500); // randomly set delay between servo movement between 50 and 500 milliseconds
myservo.write(pos); // tell servo to go to position specified by variable ‘pos’ value
delay(delay1); // waits randomly between 50ms and 500 ms between servo movements

randOn = random (100, 1200); // generate ON time between 0.1 and 1.2 seconds
randOff = random (200, 900); // generate OFF time between 0.2 and 0.9 seconds
digitalWrite(ledPin, HIGH); // sets the LED on
delay(randOn); // waits for a random time while ON
digitalWrite(ledPin, LOW); // sets the LED off
delay(randOff); // waits for a random time while OFF

}
else{
//Perform actions when active = false;

}

}

Almost, but not quite. My last instruction was a bit misleading though, so that's my fault.

I really meant to say fill in the two blocks of the second if statement. The first if is just toggling the active state, and there shouldn't be any other code in there. The second if statement has two blocks (a block being a section of code surrounded by { and }). One will execute while active = true, the other will execute while active = false.

If you don't want anything happeneing while active = false, then leave that section blank.

Delete the code you added in the first if() and see if that does what you want.

ok this code didnt work. the servo stays running however doesnt move and the led doesnt even turn on and nothing happens when i press the button

#include <Servo.h> //include the servo libary - a bunch of code for a host of servo instructions you can use and don't have to develop yourself
Servo myservo; // create servo object to control a servo

boolean active = false;
boolean lastState = LOW;
boolean currentState = LOW;
byte buttonPin = A0;

int pos = 0; // variable to store the servo position
int delay1 = 0; // variable to store the delay between servo arm's movement info

int ledPin = 13; // LED connected to digital pin 13
long randOn = 0; // Initialize a variable for the ON time
long randOff = 0; // Initialize a variable for the OFF time

void setup(){

myservo.attach(9); // attaches pin 9 of the micro controller to the servo object - the servo's "control wire"

randomSeed (analogRead (0)); // randomize
pinMode(ledPin, OUTPUT); // sets the digital pin as output

}

void loop(){

currentState = digitalRead(buttonPin);

//When lastState is LOW and currentState is HIGH, we have detected a rising edge transition
//ie the input has just gone high. This is when we toggle are active boolean
if(lastState == LOW && currentState == HIGH){
//toggle active state
active != active;
}

lastState = currentState;

if(active){
//Perform actions when active = true;

pos=random(65,115); // ramdomly set the servo arm postion between 65 & 115 degrees. "90" is "straight ahead"
delay1=random(50, 500); // randomly set delay between servo movement between 50 and 500 milliseconds
myservo.write(pos); // tell servo to go to position specified by variable ‘pos’ value
delay(delay1); // waits randomly between 50ms and 500 ms between servo movements

randOn = random (100, 1200); // generate ON time between 0.1 and 1.2 seconds
randOff = random (200, 900); // generate OFF time between 0.2 and 0.9 seconds
digitalWrite(ledPin, HIGH); // sets the LED on
delay(randOn); // waits for a random time while ON
digitalWrite(ledPin, LOW); // sets the LED off
delay(randOff); // waits for a random time while OFF

}
else{
//Perform actions when active = false;

}

}

ok i got this code to work, however the servo and leds only turn on when i press and hold the button down... can someone help me make this lil edit to the code so that the servo and leds stay on until i press the button again. i want the button to be my on off switch for this mechanism...thanks

#include <Servo.h> //include the servo libary - a bunch of code for a host of servo instructions y9ou can use and don't have to develop yourself

Servo myservo; // create servo object to control a servo

const int buttonPin = A0; // the number of the pushbutton pin

int pos = 0; // variable to store the servo position
int delay1 = 0; // variable to store the delay between servo arm's movement info

int ledPin = 13; // LED connected to digital pin 13
long randOn = 0; // Initialize a variable for the ON time
long randOff = 0; // Initialize a variable for the OFF time

int buttonState = 0; // variable for reading the pushbutton status

void setup()

{

myservo.attach(9); // attaches pin 9 of the micro controller to the servo object - the servo's "control wire"

randomSeed (analogRead (0)); // randomize
pinMode(ledPin, OUTPUT); // sets the digital pin as output

// initialize the pushbutton pin as an input:
pinMode(buttonPin, INPUT);
}

void loop()

{

// 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) {

pos=random(65,115); // ramdomly set the servo arm postion between 65 & 115 degrees. "90" is "straight ahead"
delay1=random(50, 500); // randomly set delay between servo movement between 50 and 500 milliseconds
myservo.write(pos); // tell servo to go to position specified by variable ‘pos’ value
delay(delay1); // waits randomly between 50ms and 500 ms between servo movements

randOn = random (100, 1200); // generate ON time between 0.1 and 1.2 seconds
randOff = random (200, 900); // generate OFF time between 0.2 and 0.9 seconds
digitalWrite(ledPin, HIGH); // sets the LED on
delay(randOn); // waits for a random time while ON
digitalWrite(ledPin, LOW); // sets the LED off
delay(randOff); // waits for a random time while OFF

}

else {

}
}

you need to keep in mind the state of your servo and change it accordingly as the button is pressed
This will help you

Sorry, I had one incorrect line in my sample code for you. Change the following line:

active != active;

to

active = !active;

That's what I get when I rush something and just compile it without running it.

sweet!!!! it worked, thank u

couple questions now

when the servo is turned off and not moving there is a sound coming from the servo almost like electric current noise, why is this? does this mean it is not really shut off?

and also i notice the leds and servo dont turn off by just pressing the button, but i have to hold it down for a few seconds before they shut off