I was able to keep the wiring for the Sabertooth and the LCD screen. I had to look up data sheets for the PIR and IR sensors to see which pins needed connected. Both the PIR and IR sensors went to power and ground and they each had a signal wire. I was able to use a PWM for both of these sensors. I did not use the encoders because I thought that they would cause too many problems while the program was running. (Sorry the pictures are upside-down. I flipped them but they still uploaded this way.)
The programming wasn't as bad as I thought it would be. I split the programming up into two parts to make it easier. First I worked with the PIR Sensor. I read the data sheet to familiarize myself with it and then found some basic code. I needed to set the pin as an Input because I was using a PWM port. That part was easy but the more difficult part was setting the PIR variable to whatever the PIR sensor reads. After some thinking, I figured out that this could be done by putting that line in a loop that would kick out once it changed to HIGH. The IR and remote was much harder. I had to download a library to make the IR sensor receive signals from the remote and then decode those signals. It made it a lot easier to print those values to the Serial Monitor and write them down to determine the code that each button sent to the IR sensor. Once I had all of the codes, I used a switch case structure inside of a loop to decide what the robot would do if a certain button was pressed. I used 2, 4, 6, and 8 for forward, left turn, right turn, and backwards respectfully.
/*
Brian Swanson
I used a PIR sensor to first detect movement anad then take movement
commands from a remote.
Parts:
Arduino Mega
Sabertooth Speed Controller
2 Motors
1 Breadboard
1 LCD screen
1 PIR sensor
1 IR sensor with remote
Connections:
Both motors to the Sabertooth
Sabertooth to TX2
LCD to TX3
IR sensor to PIN 11
PIR to PIN 2
*/
#include <IRremote.h> //library needed for the ir sensor and remote
int PIR = 0; //sets PIR sensor to 0
byte clrscn = 12; //variable to clear the LCD screen
int RECV_PIN = 11; //sets RECV_PIN to pin 11
IRrecv irrecv(RECV_PIN); //sets the IR sensor to pin 11
decode_results results; //statement to decode the results from the IR sensor
void halt()
{ //function to stop the robot
Serial2.write(0);
delay(500);
}
void forward() //function to make the robot move forward
{
Serial2.write(100);
Serial2.write(228);
delay(500);
}
void backwards() //function to make the robot move backwards
{
Serial2.write(28);
Serial2.write(155);
delay(500);
}
void turnLeft() //function to turn left
{
Serial2.write(100);
Serial2.write(154);
delay(250);
}
void turnRight() //function to turn right
{
Serial2.write(28);
Serial2.write(228);
delay(250);
}
void setup()
{
pinMode(2, INPUT); //makes the PIR sensor an input
Serial.begin(9600); //makes the Serial monitor, Sabertooth, and the LCD screen on the same level
Serial2.begin(9600);
Serial3.begin(9600);
irrecv.enableIRIn(); //starts the IR receiver
}
void loop()
{
halt();
PIR = 0; //sets the PIR sensor to 0. should already be at 0 but to double-check
Serial3.write(clrscn);
delay(5);
Serial3.print("DO NOT MOVE");
delay(30000); //setup time for PIR sensor between 10-60s. mine is 30
Serial3.write(clrscn);
delay(5);
Serial3.print("Setup Ready");
delay(1000);
Serial3.write(clrscn);
delay(5);
Serial3.print("Looking");
while (PIR == LOW) //loops until the PIR sensor sees something
{
delay(100);
PIR = digitalRead(2); //sets PIR equal to whatever the PIR sensor reads
}
Serial3.write(clrscn);
delay(5);
Serial3.print("Movement");
delay(2000);
while(true) //loops forever to enter the remote phase
{
if (irrecv.decode(&results)) //enters this loop if the IR sensor detects a signal
{
long int code = results.value; //puts the signal received by the remote into an integer type long
Serial.println(results.value); //prints to the serial monitor to troubleshoot
switch(code) //switch case structure for the results that the IR sensor receives from the remote
{
case 2050: //if 2 is pressed
Serial3.write(12); //clears the screen
delay(5);
Serial3.print("two");
forward();
delay(50);
halt();
irrecv.resume(); // Receive the next value
break;
case 2: //if 2 is pressed
Serial3.write(12);
delay(5);
Serial3.print("two");
forward();
delay(50);
halt();
irrecv.resume(); // Receive the next value
break;
case 8: //if 8 is pressed
Serial3.write(12);
delay(5);
Serial3.print("eight");
backwards();
delay(50);
halt();
irrecv.resume(); // Receive the next value
break;
case 2056: //if 8 is pressed
Serial3.write(12);
delay(5);
Serial3.print("eight");
backwards();
delay(50);
halt();
irrecv.resume(); // Receive the next value
break;
case 2054: //if 6 is pressed
Serial3.write(12);
delay(5);
Serial3.print("six");
turnRight();
delay(50);
halt();
irrecv.resume(); // Receive the next value
break;
case 6: //if 6 is pressed
Serial3.write(12);
delay(5);
Serial3.print("six");
turnRight();
delay(50);
halt();
irrecv.resume(); // Receive the next value
break;
case 2052: //if 4 is pressed
Serial3.write(12);
delay(5);
Serial3.print("four");
turnLeft();
delay(50);
halt();
irrecv.resume(); // Receive the next value
break;
case 4: //if 4 is pressed
Serial3.write(12);
delay(5);
Serial3.print("four");
turnLeft();
delay(50);
halt();
irrecv.resume(); // Receive the next value
break;
default: //if none of the above buttons are pressed
Serial3.write(12);
delay(5);
Serial3.print("nothing");
halt();
delay(100);
irrecv.resume(); // Receive the next value
}
}
}
}
Brian Swanson
I used a PIR sensor to first detect movement anad then take movement
commands from a remote.
Parts:
Arduino Mega
Sabertooth Speed Controller
2 Motors
1 Breadboard
1 LCD screen
1 PIR sensor
1 IR sensor with remote
Connections:
Both motors to the Sabertooth
Sabertooth to TX2
LCD to TX3
IR sensor to PIN 11
PIR to PIN 2
*/
#include <IRremote.h> //library needed for the ir sensor and remote
int PIR = 0; //sets PIR sensor to 0
byte clrscn = 12; //variable to clear the LCD screen
int RECV_PIN = 11; //sets RECV_PIN to pin 11
IRrecv irrecv(RECV_PIN); //sets the IR sensor to pin 11
decode_results results; //statement to decode the results from the IR sensor
void halt()
{ //function to stop the robot
Serial2.write(0);
delay(500);
}
void forward() //function to make the robot move forward
{
Serial2.write(100);
Serial2.write(228);
delay(500);
}
void backwards() //function to make the robot move backwards
{
Serial2.write(28);
Serial2.write(155);
delay(500);
}
void turnLeft() //function to turn left
{
Serial2.write(100);
Serial2.write(154);
delay(250);
}
void turnRight() //function to turn right
{
Serial2.write(28);
Serial2.write(228);
delay(250);
}
void setup()
{
pinMode(2, INPUT); //makes the PIR sensor an input
Serial.begin(9600); //makes the Serial monitor, Sabertooth, and the LCD screen on the same level
Serial2.begin(9600);
Serial3.begin(9600);
irrecv.enableIRIn(); //starts the IR receiver
}
void loop()
{
halt();
PIR = 0; //sets the PIR sensor to 0. should already be at 0 but to double-check
Serial3.write(clrscn);
delay(5);
Serial3.print("DO NOT MOVE");
delay(30000); //setup time for PIR sensor between 10-60s. mine is 30
Serial3.write(clrscn);
delay(5);
Serial3.print("Setup Ready");
delay(1000);
Serial3.write(clrscn);
delay(5);
Serial3.print("Looking");
while (PIR == LOW) //loops until the PIR sensor sees something
{
delay(100);
PIR = digitalRead(2); //sets PIR equal to whatever the PIR sensor reads
}
Serial3.write(clrscn);
delay(5);
Serial3.print("Movement");
delay(2000);
while(true) //loops forever to enter the remote phase
{
if (irrecv.decode(&results)) //enters this loop if the IR sensor detects a signal
{
long int code = results.value; //puts the signal received by the remote into an integer type long
Serial.println(results.value); //prints to the serial monitor to troubleshoot
switch(code) //switch case structure for the results that the IR sensor receives from the remote
{
case 2050: //if 2 is pressed
Serial3.write(12); //clears the screen
delay(5);
Serial3.print("two");
forward();
delay(50);
halt();
irrecv.resume(); // Receive the next value
break;
case 2: //if 2 is pressed
Serial3.write(12);
delay(5);
Serial3.print("two");
forward();
delay(50);
halt();
irrecv.resume(); // Receive the next value
break;
case 8: //if 8 is pressed
Serial3.write(12);
delay(5);
Serial3.print("eight");
backwards();
delay(50);
halt();
irrecv.resume(); // Receive the next value
break;
case 2056: //if 8 is pressed
Serial3.write(12);
delay(5);
Serial3.print("eight");
backwards();
delay(50);
halt();
irrecv.resume(); // Receive the next value
break;
case 2054: //if 6 is pressed
Serial3.write(12);
delay(5);
Serial3.print("six");
turnRight();
delay(50);
halt();
irrecv.resume(); // Receive the next value
break;
case 6: //if 6 is pressed
Serial3.write(12);
delay(5);
Serial3.print("six");
turnRight();
delay(50);
halt();
irrecv.resume(); // Receive the next value
break;
case 2052: //if 4 is pressed
Serial3.write(12);
delay(5);
Serial3.print("four");
turnLeft();
delay(50);
halt();
irrecv.resume(); // Receive the next value
break;
case 4: //if 4 is pressed
Serial3.write(12);
delay(5);
Serial3.print("four");
turnLeft();
delay(50);
halt();
irrecv.resume(); // Receive the next value
break;
default: //if none of the above buttons are pressed
Serial3.write(12);
delay(5);
Serial3.print("nothing");
halt();
delay(100);
irrecv.resume(); // Receive the next value
}
}
}
}
No comments:
Post a Comment