Canbot
- Войдите на сайт для отправки комментариев
Чт, 27/08/2015 - 10:41
//------------------------------------------------------------------------------
//
#include <Servo.h>
#include <IRremote.h>
//------------------------------------------------------------------------------
//
IRrecv irrecv(11);
Servo motorL, motorR;
long prM = 0;
boolean updw =1;
boolean mode =1;
boolean _LR =0;
byte _move = 5;
byte echoP = 4;
byte trigP = 5;
int distans;
//+=============================================================================
//
void setup ( )
{
pinMode(13, OUTPUT);
pinMode(trigP, OUTPUT);
pinMode(echoP, INPUT);
Serial.begin(9600); // Status message will be sent to PC at 9600 baud
irrecv.enableIRIn(); // Start the receiver
// motorL.attach(6);
// motorR.attach(7);
delay(100);
}
void _distR(){
digitalWrite(trigP, LOW);
delayMicroseconds(2);
digitalWrite(trigP, HIGH);
delayMicroseconds(10);
digitalWrite(trigP, LOW);
distans=pulseIn(echoP, HIGH);
delayMicroseconds(10);
}
void moveMotors(int speedL, int speedR){
motorL.attach(6);
motorR.attach(7);
motorL.write(speedL);
motorR.write(180-speedR);
}
void dumpCode (decode_results *results)
{
if(results->value == 0xE0E006F9){
// Serial.print(" up");
moveMotors(10, 10);
_move = 1;
updw=1;
prM = millis();
}
if(results->value == 0xE0E08679){
// Serial.print("down");
moveMotors(160, 160);
_move = 2;
updw=0;
prM = millis();
}
if(results->value == 0xE0E0A659){
// Serial.print(" left");
moveMotors(10, 160);
_move = 3;
prM = millis();
}
if(results->value == 0xE0E046B9){
//Serial.print(" right");
moveMotors(160, 10);
_move = 4;
prM = millis();
}
if(results->value == 0xE0E016E9){
// Serial.print(" stop");
motorL.detach();
motorR.detach();
_move = 0;
}
if(results->value == 0xE0E0807F){ //source
mode = 1;
}
if(results->value == 0xE0E0F00F){ //mute
mode = 0;
}
if(results->value == 0xE0E058A7){ //menu
digitalWrite(13, HIGH);
}
if(results->value == 0xE0E040BF){ //menu
digitalWrite(13, LOW);
}
if(results->value == 0xE0E0D827){ //tv
_move = 5;
}
Serial.print(results->value, HEX);
Serial.println(";");
}
//+=============================================================================
//
void loop ( )
{
decode_results results; // Somewhere to store the results
if (irrecv.decode(&results)) { // Grab an IR code
dumpCode(&results); // Output the results as source code
//Serial.println(""); // Blank line between entries
irrecv.resume(); // Prepare for the next value
}
switch (_move){
case 1:
// if(_move == 1){
if(mode == 1){
moveMotors(10, 10);
}else if(millis()>prM+200){
motorL.detach();
motorR.detach();
}
break;
case 2:
//}else if(_move == 2){
if(mode == 1){
moveMotors(160, 160);
}else if(millis()>prM+200){
motorL.detach();
motorR.detach();
}
break;
case 3:
//}else if(_move == 3){
if(millis()<prM+100){
moveMotors(10, 160);
}else if(updw == 1){
_move = 1;
}else{_move = 2;
}
break;
case 4:
//}else if(_move == 4){
if(millis()<prM+100){
moveMotors(160, 10);
}else if(updw == 1){
_move = 1;
}else{_move = 2;
}
break;
case 5:
//}else if(_move == 5){
if(millis()<prM+10){
motorL.detach();
motorR.detach();
digitalWrite(13, HIGH);
_distR();
Serial.print(distans);
Serial.print(" ");
Serial.print(distans/5.8);
Serial.print("mm");
Serial.println(";");
digitalWrite(13, LOW);
}else if(millis()>prM+10&&millis()<prM+500){
if(distans>700){
digitalWrite(13, LOW);
_LR=!_LR;
moveMotors(10, 10);
}else if(distans<500){
moveMotors(160, 160);
}else if(distans<=700){
digitalWrite(13, HIGH);
if(_LR==0){
moveMotors(160, 10);
}else{
moveMotors(10, 160);
}
}
}else if(millis()>prM+500){
prM=millis();
}
}
}
Canbot, код под arduino pro mini, в оригинале http://www.thingiverse.com/thing:965449 для Attiny. Управление по ик, с любого пульта от телевозоров samsung.