The push of a button

Hello,

This is my first post here and surely not the last Smiley

I have searched around the Arduino command and through the forum and I cant find anything that fits with what I want to do.

I am running a servo FUTABA S3003 directly from my Arduino duemilanove 328.

I am using a code that I found here (Servo control via direct access to AVR PWM registers), and it works great :

I did modify it to my needs : I push a button it turns 90 CCW and a led lights up. When I release the button it goes back to 0 and the LED shuts off.

I would like to have these two state successively on each push of the button( button up wouldn't do nothing). What Arduino command should I use ?

Thanks

I would like to have these two state successively on each push of the button

Not sure what you are saying, could you clarify what you want to do.

Also, it would be easier to suggest changes if you posted your code.

It sounds like what you want is:

  • Move to 0-degrees.
  • Upon a button press move to 90-degrees and turn LED on.
  • When button is released move to 0-degrees and turn LED off.

So what I think you probably want to do is in the setup function set the input and output pins and move the servo to 0-degrees.

In the loop function you could read the state of the button pin then using an if...else move to 90-degrees w/ LED or to 0-degrees w/o LED.

You could make things a little more elaborate by using a state variable so it only moves the servo on a change in state and some debouncing on the button code.

Have fun!

Thank for your interest.

What I would like is : button push, motor turns 90 degrees, led on. button released nothing button push, motors turns back to 0 degrees and led off.

This is the original code (I am a newbie so my first post couldnt contain any link but this is my second post, I guess its gonna work):

http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1222623026/2

Here is my code :

int Switch1=2; int val=0; int Ledpin=13;

define servo1control OCR1A

define servo1null 1500

void setup(){ pinMode(Switch1, INPUT); pinMode(9,OUTPUT); pinMode(Ledpin,OUTPUT); TCCR1B = 0b00011010; // Fast PWM, top in ICR1, /8 prescale (.5 uSec) TCCR1A = 0b10100010; //clear on compare match, fast PWM // to use pin 9 as normal input or output, use TCCR1A = 0b00100010 ICR1 = 39999; // 40,000 clocks @ .5 uS = 20mS servo1control = servo1null; // controls chip pin 15 ARDUINO pin 9

}

void loop(){

val = digitalRead(Switch1); if (val == LOW) { servo1control = 3300; digitalWrite(Ledpin, HIGH); } else { val = digitalRead(Switch1); if (val == HIGH) { digitalWrite(Ledpin, LOW); servo1control = servo1null; } } }

Voilà !! I hope I was clear enough now.

Thank you

Stupid question, why not use the servo library? http://arduino.cc/en/Reference/Servo

Might be easier.

First there's no reason to read the switch input twice in one loop. This code is going to execute in milli-seconds and reading it switch could actually goof up the timing.

Second, the code you have would actually do something more like the pseudo-code I'd posted last where when the button is pressed it goes to 90 (over and over) and if the buttons released it goes to 0 (over and over).

What it sounds like you really want is some kind of state check to see if the button state has changed since last time (HIGH to LOW or LOW to HIGH) and to save what position the servo is in.

I'll work on some code for it. I haven't used the timer control for servos so I'm assuming that part is right.

I haven't tested but this should work:

int Switch1=2;
int val=0;
int Ledpin=13;
#define servo1control OCR1A
#define servo1null 1500

int buttonState = 0;
int servoPosition = 0;

void setup(){
 pinMode(Switch1, INPUT);
 pinMode(9,OUTPUT);
 pinMode(Ledpin,OUTPUT);
 TCCR1B = 0b00011010;          // Fast PWM, top in ICR1, /8 prescale (.5 uSec)
 TCCR1A = 0b10100010;          //clear on compare match, fast PWM
                       // to use pin 9 as normal input or output, use  TCCR1A = 0b00100010
 ICR1 =  39999;                // 40,000 clocks @ .5 uS = 20mS
 servo1control = servo1null;         // controls chip pin 15  ARDUINO pin 9

}

void loop(){
 val = digitalRead(Switch1);        //Read switch value
 if (val == HIGH) {                 //If switch is pressed
   if (buttonState == 0) {          //and switch didn't used to be pressed
     if (servoPosition == 0) {      //and servo is in original position
       servo1control = 3300;        //move servo to other position
       digitalWrite(Ledpin, HIGH);  //turn LED on
       servoPosition = 1;           //and set servo state to other
     }
     else {                         //If servo was already in other position
       servo1control = servo1null;  //move servo to original position
       digitalWrite(Ledpin, LOW);   //turn led off
       servoPosition = 0;           //and set servo state to original
     }
     buttonState = 1;               //either way set button state to pressed
   }
 }
 else {
   buttonState = 0;                 //if button isnt pressed set state as such

I don't think you finished off your code there, seems some brackets fell off at the end

whoops, you're right...

It worked good after sorting out the brackets... I thank you very much anyway for giving me this direction.

The only problem is I get some erratic result sometimes. I tried putting a delay after each move of the servo. It did calm down the weird moves of the motor. If I push the button rapidly the pogram "jumps" a push. I guess this is caused by the switch contact that is poor quality and is sending several HIGH and LOW through one cycle down or up. If I do it slowly its perfect

I will also try using the existing library but it just seems easier to me to have a code right in front of me and not some kind of file that I don't know nothin about...I will learn that too sometime but now I add a project to deliver.

Thank you for your help !!

I will post the final code and a film of the result soon.

That's probably because the code I gave you does not do any debouncing of the pushbutton. Pushbuttons are notorious for giving noisy data especially at the beginning and end of a press.

You can search the forum or google for "debouncing code" and probably find many ways to add it.