Galera, montei um codigo para controlar um robo via IR, abaixo tem o codigo, só que não funicnou, alguem sabe por que ?!
#include <IRremote.h>
#include <AFMotor.h>
#define FNV_PRIME_32 16777619
#define FNV_BASIS_32 2166136261
AF_DCMotor motor1(1, MOTOR12_64KHZ);
AF_DCMotor motor2(2, MOTOR12_64KHZ);
int RECV_PIN = 8; //Onde será ligado o Receptor IR
IRrecv irrecv(RECV_PIN);
decode_results results;
void setup()
{
irrecv.enableIRIn(); //Iniciando a recepção do sinal
Serial.begin(9600);
motor1.setSpeed(200);
motor2.setSpeed(200);
}
int compare(unsigned int oldval, unsigned int newval) {
if (newval < oldval * .8) {
return 0;
}
else if (oldval < newval * .8) {
return 2;
}
else {
return 1;
}
}
unsigned long decodeHash(decode_results *results) {
unsigned long hash = FNV_BASIS_32;
for (int i = 1; i+2 < results->rawlen; i++) {
int value = compare(results->rawbuf[i], results->rawbuf[i+2]);
hash = (hash * FNV_PRIME_32) ^ value;
}
return hash;
}
void loop() {
if (irrecv.decode(&results)) {
Serial.print("'real' decode: "); //Mostra o valor recebido
Serial.print(results.value, HEX); //!!!!!!!PARA IDENTIFICAR AS TEClAS
Serial.print(", hash decode: ");
unsigned long hash = decodeHash(&results);
Serial.println (hash); //Mostra o valor já decodificado!
irrecv.resume(); // Obrigatório a reinicialização da recepção!
///////////////////////////////////////////////////
if (hash == 1938763546 ){
Serial.println (" Pra Cima!");
motor1.run(FORWARD);
motor2.run(FORWARD);
delay(150);
motor1.run(RELEASE);
motor2.run(RELEASE);
}
////////////////////////////////////////////////////
if (hash == 617239776 ){
Serial.println (" Pra Baixo!");
motor1.run(BACKWARD);
motor2.run(BACKWARD);
delay(150);
motor1.run(RELEASE);
motor2.run(RELEASE);
}
/////////////////////////////////////////////////////
if (hash == 2893538684 ){
Serial.println (" Pra Direita!");
motor1.run(FORWARD);
motor2.run(BACKWARD);
delay(150);
motor1.run(RELEASE);
motor2.run(RELEASE);
}
///////////////////////////////////////////////////
if (hash == 414219326 ){
Serial.println (" Pra Esquerda!");
motor1.run(BACKWARD);
motor2.run(FORWARD);
delay(150);
motor1.run(RELEASE);
motor2.run(RELEASE);
}
///////////////////////////////////////////////////
}
}
Tags:
Bem-vindo a
Laboratorio de Garagem (arduino, eletrônica, robotica, hacking)
© 2024 Criado por Marcelo Rodrigues. Ativado por