Trying to control PWM with the touch of a button

Hello I am trying to control PWM with the touch of a button here is my code:

include

/* ArduKart

This program was written for an Arduino powered GoKart.

Press GO and the kart with ramp up to top speed This is done trough PWM to a motor controller Press REV and the kart will go into reverse with a preset max speed This is also done through PWM to a motor controller

*/

Button goButton(7,PULLDOWN);

int ledPin = 10; // LED connected to digital pin 9 byte goPos = 0; //loop counter

void setup() { Serial.begin (9600); pinMode(ledPin = 10, OUTPUT); }

void loop() {

if(goButton.isPressed()) { if (goPos <= 255) {goPos++;} //for (byte goPos=0; goPos <= 255; goPos++) analogWrite(ledPin, goPos); delay(30); Serial.print(goPos, DEC);

} }

When I press to Go button I want the PWM signal to ramp up to 255 and when I let go of the Go button I want the counter to reset. As of now, when I reach 255 the loop repeats! I just want it to stay at 255 when it is reached. I am having a hard time figuring out what to use - a FOR, WHILE, etc. I am pretty new to this so any help would be greatly appreciated ;)

Try and hold the value at the maximum, like this:-

if(goButton.isPressed())
 {
   if (goPos <= 255) {goPos++;}
   if(goPos > 255) {goPos = 255;}
   analogWrite(ledPin, goPos);
   delay(30);
   Serial.print(goPos, DEC);

Sweet that worked!

Can you look over the rest of my code?

Servo leftMotor; Servo rightMotor;

Button goButton(7,PULLDOWN); Button revButton(8,PULLDOWN);

int goPos = 90; //puts motor controllers @ 90 degrees (neutral) int revPos = 90; //puts motor controllers @ 90 degrees (neutral) int rampSpeed = 100; //time it takes to ramp up to full speed

void setup() { Serial.begin (9600); leftMotor.attach(9); // left motor controller connected to digital pin 9 rightMotor.attach(10); // right motor controller connected to digital pin 10 pinMode(gndPin, OUTPUT);

}

void loop() {

delay(100); //delay for testing if(goButton.isPressed()) { if (goPos < 180) {goPos ++;} if (goPos > 180) {goPos = 180;} leftMotor.write(goPos); rightMotor.write(goPos); delay(rampSpeed); }

if(revButton.isPressed()) { if (revPos <= 90) {revPos --;} if (revPos < 0) {revPos = 0;} leftMotor.write(revPos); rightMotor.write(revPos);

}

else { goPos = 90; revPos = 90; }

I am using two SPST pushbutton switches connected to digital in 7 & 8 common pull-down resistor and common 5v. They seem to be interfering with each other. When I press Go the Go counter counts up from 90 and the Reverse counter counts down from 90. I need them to act totally independently. When I press go the go counter could from 90 to 180 and when I press Rev. I need to count from 90 to 0. I also need to make it so it I accidently press both buttons don't send both sets of numbers to the motor controllers. In fact if I had 1 counter, that would be the counter to send to the motor controllers. I don't know its getting late and I want to figure it out but I just can't.

Do I need to isolate my inputs? - using diodes??? Do I need to have 2 counters?

Heeeeeeeelp!!! :'(

I am using two SPST pushbutton switches connected to digital in 7 & 8 common pull-down resistor and common 5v.

How much was that resistor? You can't afford another one?

If you can't afford another pull-down, I hear Atmel are giving pulll-ups away for free ;D

LOL thanks for the advice! I'm not trying to save resistors I am trying to save my gnd pins! Anyways some real advice would be nice. How can I isolate my 2 switches - can it be done in programming or does it have to be hardware?

You can connect any number of things to one ground pin.

I know but how do I isolate my inputs?

hey Scott, check out this tutorial for some good info about wiring up push buttons and some code suggestions...

http://www.ladyada.net/learn/arduino/lesson5.html

_J