I'm currently working on a sumo bot that has some line sensors on the front end of it to detect the color of the outer ring so it will know it needs to turn around. I'm using the interrupt function to ideally read the LOW state of the sensor so it identifies the color has changed. I currently have no errors when I upload, but when I set the robot to drive over a white line the Interrupt function is not triggering. Any help would be greatly appreciated.
int signalPin = 3; //pin 3
int signalPin2 = 2; //pin 2
2.) get those "delay(1)" function calls out of your ISRs
3.) I would venture to guess that your ISRs ARE being called and ARE working, but any input via the ISRs are negligible since after being called, you immediately reset your motors via the main loop(). In other words, you aren't allowing enough time for the bot to actually turn. You need to use the ISRs to set a flag and then do the turning in the loop() via a switch-case statement.
In addition to guide lines mentioned in #1, #2, and #3, you may be liking to revise your program at the appropriate labels as per following extra guide lines:
1. Use recommended function: attachInterrupt(digitalPinToInterrupt(2), isrName, triggerLevel);
2.attachInterrupt() function does/does not do the following: (a) It enables EIMSK0 signal (External Interrupt Request 0 Enable, INT0)
(b) (Global Interrupt Enable Bit has already been activated by the System Program during reset initialization.)
(c) Does not connect the internal pull-up resistor with Pin-4 of the ATmega328. To make Pin-4 (PD2 Port Line) to work as an external interrupt line (INT0-pin), we must connect the internal pull-up or external pull-up. So, add the instruction:
As I am not sure about the nature of the external interrupting device of the third party (it could be a push switch or an opto-coupler), I would always define my interrupt pin to known logic. How good is this idea?
void loop()
{
long RangeInCentimeters;
Serial.println(RangeInCentimeters = ultrasonic.MeasureInCentimeters());
if (RangeInCentimeters <= 15) // checking to see how close an object is
{
else (RangeInCentimeters > 15);
{
// Motor.stop(MOTOR1);
// Motor.stop(MOTOR2);
Motor.speed(MOTOR1, -25);
Motor.speed(MOTOR2, 25); //motor2 rightside motor 1 left side
delay(250);
}
//line sensor commands
{
if(HIGH == digitalRead(signalPin)) //sensor RIGHT
{
Serial.println("black");
}
else
{
Serial.println("white"); // display the color
//delay(1000); // wait for a second
// Motor.stop(MOTOR1);
// Motor.stop(MOTOR2);
Motor.speed(MOTOR1, 100);
delay(250);
}
}
{
if(HIGH == digitalRead(signalPin2)) //sensor LEFT
{
Serial.println("black2");
}
else {
Serial.println("white2");
// Motor.stop(MOTOR1);
// Motor.stop(MOTOR2);
Motor.speed(MOTOR2, 100);
delay(250);
}
}
}
this is the code that I want to use but the problem is the timing because even though I might get an input value for the sensor detecting the white line it has to finish the other task first creating in accuracy for being able to react to the line.
int signalPin = 3; //pin 3
int signalPin2 = 2; //pin 2
#include "Grove_I2C_Motor_Driver.h" //motor controller
// default I2C address is 0x0f
#define I2C_ADDRESS 0x0f
volatile boolean sensorRight = false;
void setup()
{
Motor.begin(I2C_ADDRESS);
Serial.begin(9600);
pinMode(signalPin2, INPUT); //setting the sensors to have their values read
pinMode(signalPin, INPUT);
attachInterrupt(signalPin, turnRight, FALLING); //having the function turnRight activate if the sensor gets a low value
attachInterrupt(signalPin2, turnLeft, FALLING); //having the function turnLeft activate if the sensor gets a low value
delay(3000); //waiting three secounds so I can input power to the arduino
}
void loop()
{
Motor.speed(MOTOR1, -100); //having the motors constantly move forward to observe if the Interrupt fires
Motor.speed(MOTOR2, -100);
if (sensorRight == true) //If the interrupt fires the robot should turn here
{
Motor.stop(MOTOR2);
Motor.speed(MOTOR1, 50);
}
}
void turnRight() //When the interrupt triggers it changes the value of sensorRight
{
sensorRight = true;
}
void turnLeft() //this is in place for later when a solution is found
{
Motor.stop(MOTOR1);
Motor.speed(MOTOR2, 50);
}