Go Down

Topic: Logic problems (Read 1 time) previous topic - next topic

DWRIGHT02

Apr 19, 2012, 04:48 am Last Edit: Apr 19, 2012, 05:13 am by DWRIGHT02 Reason: 1
Fixed previous error,

Now I'm having issue with interrupts.

I need to interrupt my servo when the button is LOW, I don't want to post my whole code, but unsure on what is needed and not, for the interrupt.

Code: [Select]
#include <Servo.h> 
Servo myservo;  // create servo object to control a servo
int buttonPin = 3;               
int pos = 0;    // variable to store the servo position
volatile int state = HIGH;

void setup()

  pinMode (buttonPin, INPUT);
  attachInterrupt(0, stop, LOW);
  Serial.begin(9600);
  myservo.attach(2);  // attaches the servo on pin 9 to the servo object
}

void loop()
{
while (digitalRead(buttonPin) == HIGH)
{
  for (pos = 0; pos < 180; pos += 1)
  {
   myservo.write(pos);              // tell servo to go to position in variable 'pos'
   delay(15);   
   Serial.println(pos);
  }
  for (pos = 180; pos>=1; pos-=1)     // goes from 180 degrees to 0 degrees                           
  {
   myservo.write(pos);              // tell servo to go to position in variable 'pos'
   delay(15); 
   Serial.println(pos);     // waits 15ms for the servo to reach the position
  }
  digitalRead(state);
}
}

void stop()
{
  state = !state;
}

Techone

Change :

Code: [Select]


attachInterrupt(0, stop, LOW);

// to

attachInterrupt(1, stop, FALLING);



Why ?   1 is pin 3, 0 is pin 2. LOW is not working well. I did that once. I change to FALLING or RISING. Work much better.

DWRIGHT02

thats great, its finally sort of working. Now if only i can get it to stay off once I push the button.

Osgeld

thats a bit of software, in pure example form

byte oldbutton = 0
button = *arduino pin number*

if(!button && oldbutton)
{
  oldbutton = 0
}


if(button && !oldbutton)
{
  do something
  oldbutton = 1
}


in a nutshell that should make a single shot button (though its late here and thats totally off the top of my head)
http://arduino.cc/forum/index.php?action=unread;boards=2,3,4,5,67,6,7,8,9,10,11,66,12,13,15,14,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,86,87,89,1;ALL

PaulS

Quote
Now if only i can get it to stay off once I push the button.

Which button? If you used switches, instead, perhaps it would work.

You have a variable called state, that you toggle in the ISR. The only use of state in loop() is as a pin number. Why?
The art of getting good answers lies in asking good questions.

Go Up