Telecomanda IR nu este la fel de precisa ca o telecomanda RC dar iti poti face treaba cu ea.
Am folosit 7 butoane de pe telecomanda, dintre care 2 butoane actioneaza un LED iar 5 butoane actioneaza motoarele robotului.
Am folosit:
- IR reciver (TSOP48)
- telecomanda IR
- LCD Serial
- 2 x motoare DC
- Arduino Duemilanove
- LED
- L293D (punte H pentru motoare)
Program:
#include
#include
#define rxPin 9
#define txPin 10
int RECV_PIN = 12;
IRrecv irrecv(RECV_PIN);
decode_results results;
NewSoftSerial serial = NewSoftSerial(rxPin, txPin);
const int motor1a = 2;
const int motor2a = 4;
const int motor1b = 5;
const int motor2b = 7;
const int enable1 = 3;
const int ledPin = 13;
boolean val = HIGH;
int led = 8;
void setup()
{
pinMode(rxPin, INPUT);
pinMode(txPin, OUTPUT);
pinMode(motor1a, OUTPUT);
pinMode(motor2a, OUTPUT);
pinMode(motor1b, OUTPUT);
pinMode(motor2b, OUTPUT);
pinMode(ledPin, OUTPUT);
pinMode(led, OUTPUT);
digitalWrite(enable1, HIGH);
serial.begin(9600);
irrecv.enableIRIn();
// clipeste ledPin de 3 ori. Asta trebuie sa se intample doar 1 data.
// daca vezi ca ledul clipeste de mai multe ori inseamna ca s-a
// resetat; probabil din cauza ca motorasele sunt arse
// sau este un scurt circuit.
blink(ledPin, 3, 100);
}
void Reverse()
{
digitalWrite(enable1, HIGH);
digitalWrite(motor1a, HIGH);
digitalWrite(motor2a, LOW);
digitalWrite(motor1b, HIGH);
digitalWrite(motor2b, LOW);
}
void Forward()
{
digitalWrite(enable1, HIGH);
digitalWrite(motor1a, LOW);
digitalWrite(motor2a, HIGH);
digitalWrite(motor1b, LOW);
digitalWrite(motor2b, HIGH);
}
void Left()
{
digitalWrite(enable1, HIGH);
digitalWrite(motor1a, LOW);
digitalWrite(motor2a, HIGH);
digitalWrite(motor1b, HIGH);
digitalWrite(motor2b, LOW);
}
void Right()
{
digitalWrite(enable1, HIGH);
digitalWrite(motor1a, HIGH);
digitalWrite(motor2a, LOW);
digitalWrite(motor1b, LOW);
digitalWrite(motor2b, HIGH);
}
void Stop()
{
digitalWrite(enable1, LOW);
digitalWrite(motor1a, LOW);
digitalWrite(motor2a, LOW);
digitalWrite(motor1b, LOW);
digitalWrite(motor2b, LOW);
}
void blink(int whatPin, int howManyTimes, int milliSecs)
{
int i = 0;
for(i=0; i<=3; i++)
{
digitalWrite(ledPin, HIGH);
delay(100/2);
digitalWrite(ledPin, LOW);
delay(100/2);
}
serial.print("$CLEAR\n\r");
serial.print("$GO 1 1\n\r");
serial.print("$PRINT Strat asteapta \n\r");
serial.print("\n\r");
}
void rectsop()
{
if (irrecv.decode(&results))
irrecv.resume();
}
void loop()
{
rectsop();
if(results.value==16718055) //scrie 2
{
Forward();
serial.print("$CLEAR\n\r");
serial.print("$GO 1 1\n\r");
serial.print("$PRINT Inainte (2) \n\r");
serial.print("\n\r");
delay(100);
}
if(results.value==16726215) // scrie 5
{
Stop();
serial.print("$CLEAR\n\r");
serial.print("$GO 1 1\n\r");
serial.print("$PRINT Stop (5) \n\r");
serial.print("\n\r");
delay(100);
}
if(results.value==16734885) //scrie 6
{
Right();
serial.print("$CLEAR\n\r");
serial.print("$GO 1 1\n\r");
serial.print("$PRINT Dreapta (6) \n\r");
serial.print("\n\r");
delay(100);
}
if(results.value==16716015) // scrie 4
{
Left();
serial.print("$CLEAR\n\r");
serial.print("$GO 1 1\n\r");
serial.print("$PRINT Stanga (4) \n\r");
serial.print("\n\r");
delay(100);
}
if(results.value==16730805) // scrie 8
{
Reverse();
serial.print("$CLEAR\n\r");
serial.print("$GO 1 1\n\r");
serial.print("$PRINT Inapoi (8) \n\r");
serial.print("\n\r");
delay(100);
}
if(results.value==16750695) //scrie+100
{
digitalWrite(led, HIGH);
serial.print("$CLEAR\n\r");
serial.print("$GO 1 1\n\r");
serial.print("$PRINT Aprinde LED \n\r");
serial.print("\n\r");
delay(100);
}
if(results.value==16754775)
{
digitalWrite(led, LOW);
serial.print("$CLEAR\n\r");
serial.print("$GO 1 1\n\r");
serial.print("$PRINT Stinge LED \n\r");
serial.print("\n\r");
delay(100);
}
}
*In locul librariei SoftwareSerial.h care intra in conflict cu IRremote.h am folosit NewSoftSerial.h
Niciun comentariu:
Trimiteți un comentariu