Interrupts
hello,
i'm working on sumo bot has line sensors on front end of detect color of outer ring know needs turn around. i'm using interrupt function ideally read low state of sensor identifies color has changed. have no errors when upload, when set robot drive on white line interrupt function not triggering. appreciated.
int signalpin = 3; //pin 3
int signalpin2 = 2; //pin 2
#include "grove_i2c_motor_driver.h" //motor controller
// default i2c address 0x0f
#define i2c_address 0x0f
void setup()
{
motor.begin(i2c_address);
serial.begin(9600);
pinmode(signalpin2, input);
pinmode(signalpin, input);
attachinterrupt(signalpin, turnright, low);
attachinterrupt(signalpin2, turnleft, low);
delay(3000);
}
void loop()
{
{
motor.speed(motor1, -100);
motor.speed(motor2, -100);
}
}
void turnright()
{
delay(1);
motor.stop(motor2);
motor.speed(motor1, 50);
}
void turnleft()
{
delay(1);
motor.stop(motor1);
motor.speed(motor2, 50);
}
i'm working on sumo bot has line sensors on front end of detect color of outer ring know needs turn around. i'm using interrupt function ideally read low state of sensor identifies color has changed. have no errors when upload, when set robot drive on white line interrupt function not triggering. appreciated.
int signalpin = 3; //pin 3
int signalpin2 = 2; //pin 2
#include "grove_i2c_motor_driver.h" //motor controller
// default i2c address 0x0f
#define i2c_address 0x0f
void setup()
{
motor.begin(i2c_address);
serial.begin(9600);
pinmode(signalpin2, input);
pinmode(signalpin, input);
attachinterrupt(signalpin, turnright, low);
attachinterrupt(signalpin2, turnleft, low);
delay(3000);
}
void loop()
{
{
motor.speed(motor1, -100);
motor.speed(motor2, -100);
}
}
void turnright()
{
delay(1);
motor.stop(motor2);
motor.speed(motor1, 50);
}
void turnleft()
{
delay(1);
motor.stop(motor1);
motor.speed(motor2, 50);
}
you have couple of issues.
first off, don't want use low. causes interrupt continuously keep firing on , on long pin stays low. that's not useful often. want falling know went low , can react , if interrupt you'll know has seen white line again , need turn again.
secondly, delay doesn't work in isr. , may hang processor forever. should read on interrupts before try using them. there technical things need understand keep out of trouble.
lastly, think interrupt does. sets 1 motor off , other motor in different direction. code execution returns loop. , loop motor commands? writes them both right same direction , speed. it's going in turning state few microseconds before goes right going straight.
i doubt need interrupt this. without it, bot detect white line in few microseconds or millisecond or 2 @ most. should more fast enough react.
but if want use interrupt here, need let interrupt set flag white line detected , let loop take appropriate action of turning bot in response flag being set. way loop function can know whether in turning state or going straight state.
but still, easier without interrupt.
first off, don't want use low. causes interrupt continuously keep firing on , on long pin stays low. that's not useful often. want falling know went low , can react , if interrupt you'll know has seen white line again , need turn again.
secondly, delay doesn't work in isr. , may hang processor forever. should read on interrupts before try using them. there technical things need understand keep out of trouble.
lastly, think interrupt does. sets 1 motor off , other motor in different direction. code execution returns loop. , loop motor commands? writes them both right same direction , speed. it's going in turning state few microseconds before goes right going straight.
i doubt need interrupt this. without it, bot detect white line in few microseconds or millisecond or 2 @ most. should more fast enough react.
but if want use interrupt here, need let interrupt set flag white line detected , let loop take appropriate action of turning bot in response flag being set. way loop function can know whether in turning state or going straight state.
but still, easier without interrupt.
Arduino Forum > Using Arduino > Programming Questions > Interrupts
arduino
Comments
Post a Comment