Go Down

Topic: Problems Using 'attachInterrupt' for my project (Read 82 times) previous topic - next topic

Hi,

Im working on a project which uses the interrupt command.
I want to achieve a program which will allow my DC motors to run at a set speed but when the robot hits an incline the interrupt is activated to speed up the motors.
Instead of the tilt sensor I am using a pushbutton at the moment to emulate the tilt sensor and once the interrupt is activated the program will turn on an LED and stop the dc motors instead of speeding them up. I chose to do this because I was having problems wondering if the tilt sensor was set up correctly and such and such. I have the 2 DC Motors connected to an H bridge L293D and you can see in the code below what i have written and maybe able to correct me to where im going wrong, Help much appreciated.



 
int IN1 = 8;
int IN2 = 7;
int IN3 = 4;
int IN4 = 3;
int EN1 = 6;
int EN2 = 5;
int pButton = 0;
int LED = 12;

unsigned long buttonTime = 0;     // For Button Debounce
unsigned long lastButtonTime = 0;



void setup(){

  pinMode (IN1, OUTPUT);
  pinMode (IN2, OUTPUT);
  pinMode (IN3, OUTPUT);
  pinMode (IN4, OUTPUT);
  pinMode (EN1, OUTPUT);
  pinMode (EN2, OUTPUT);
  pinMode (pButton, INPUT);
  attachInterrupt(0, Stop, CHANGE);
  pinMode (LED, OUTPUT);
 
 
}
 
void loop(){
 
  analogWrite(EN1, 150);
  analogWrite(EN2, 150);
  digitalWrite(IN1, HIGH);
  digitalWrite(IN2, LOW);
  digitalWrite(IN3, HIGH);
  digitalWrite(IN4, LOW);
 
   
}
void Stop(){
 
  buttonTime = millis();
  if (buttonTime - lastButtonTime > 250) // Button DeBounce
  {
   
  digitalWrite(IN1, LOW);
  digitalWrite(IN2, LOW);
  digitalWrite(IN3, LOW);
  digitalWrite(IN4, LOW);
  digitalWrite (LED, HIGH);
  lastButtonTime = buttonTime; // Button DeBounce
  }
 
}
 



   
 

groundfungus

#1
Dec 07, 2014, 05:13 pm Last Edit: Dec 07, 2014, 07:14 pm by groundfungus
Quote
int pButton = 0;
Interrupt 0 is on pin 2. 
How is the button wired?  Below is the preferred way.  If like below set the interrupt to trigger on LOW.  Edit:  Per Robin2 below, FALLING is better for sure.

Thanks for your post. I did try to use button debounce with the software (Arduino) rather than the external set up as in your picture. Does my code look correct though, As ive tried this set up as you have attached, however it still doesnt work. What happens is this.....The motors spin, and the LED stays on. Thats it, nothing else happens, the button doesnt change anything upon pressing it.

Robin2

Why are you using a CHANGE interrupt?
RISING or FALLING is usually simpler.

Why do you need to debounce?
If one interrupt is sufficient to stop your motor, what harm will 27 do?

Why have you
Code: [Select]
buttonTime = millis();
  if (buttonTime - lastButtonTime > 250) //


instead of the shorter and simpler
Code: [Select]
if (millis() - lastButtonTime > 250) //
Do you need to use buttonTime elsewhere ?

...R

#4
Dec 07, 2014, 06:45 pm Last Edit: Dec 07, 2014, 06:56 pm by scientificfreek Reason: Forgot to add the closing brackets in my code
I found it on the net, it was used in an interrupt example, Maybe ive wrote it wrong, could you show me an example of how I could get the interrupt to work upon a tilt sensor being tilted? Yous can see what im trying to achieve via my code ive pasted below, I want the DC motors to slow down via the interrupt command once the tilt sensor is activated. Forget the software button debounce for a moment, I was just experimenting and got a little out of my depth. This is what ive managed to do so far but it doesnt seem to work.

*The tiltSwitch is connected to pin 2 on the UNO, which is one of the pins able to use the interrupt command.
* I have used analogWrite on the enable pins of the H Bridge L293 circuit to allow me to use PWM so that I can vary the speed.

I cant seem to see where ive whent wrong yet it doesnt work

Kind regards



int IN1 = 8;
int IN2 = 7;
int IN3 = 4;
int IN4 = 3;
int EN1 = 6;
int EN2 = 5;
int tiltSwitch = 0;

void setup(){

 pinMode (IN1, OUTPUT);
 pinMode (IN2, OUTPUT);
 pinMode (IN3, OUTPUT);
 pinMode (IN4, OUTPUT);
 pinMode (EN1, OUTPUT);
 pinMode (EN2, OUTPUT);
 pinMode (tiltSwitch, INPUT);
 attachInterrupt(0, fastMotors, LOW);

 
 
}
 
void loop(){
 
 analogWrite(EN1, 150);
 analogWrite(EN2, 150);
 digitalWrite(IN1, HIGH);
 digitalWrite(IN2, LOW);
 digitalWrite(IN3, HIGH);
 digitalWrite(IN4, LOW);
 
   
}
void fastMotors(){
 
 analogWrite(EN1, 200);
 analogWrite(EN2, 200);
 digitalWrite(IN1, HIGH);
 digitalWrite(IN2, LOW);
 digitalWrite(IN3, HIGH);
 digitalWrite(IN4, LOW);
 
}



   
 

groundfungus

Code: [Select]
int tiltSwitch = 0;

Quote
*The tiltSwitch is connected to pin 2 on the UNO, which is one of the pins able to use the interrupt command.
Your code says different.

And as Robin2 says FALLING is the mode to use.  My bad.  I claim caffeine deficiency.

Thanks guys. Ive changed 'int tiltSwitch = 0' to 'int tiltSwitch = 2' and it seems to work. for some reason i had it in my mind that you put the interrupt pin rather 0 or 1 in whereas in reality you do! its just you only do this within the interrupt command itself.

Kind regards

Rob ;-)

Many thanks.

Go Up