Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <Otto.h>
- Otto Otto;
- #include <IRremote.h>
- IRrecv ir_rx(2);
- decode_results ir_rx_results;
- unsigned long fnc_ir_rx_decode()
- {
- bool decoded=false;
- if( ir_rx.decode(&ir_rx_results))
- {
- decoded=true;
- ir_rx.resume();
- }
- if(decoded)
- return ir_rx_results.value;
- else
- return 0;
- }
- #define LeftLeg 2 // left leg pin, servo[0]
- #define RightLeg 3 // right leg pin, servo[1]
- #define LeftFoot 4 // left foot pin, servo[2]
- #define RightFoot 5 // right foot pin, servo[3]
- #define Buzzer 13 //buzzer pin
- #include <EnableInterrupt.h>
- #define Timeout 500
- #define Shift 8
- unsigned long time, dtime,timeout_mark;
- unsigned long res = 0;
- unsigned long Button_ID = 0;
- unsigned int buf = 0;
- byte impuls = 1;
- byte Channl_ID = 1;
- byte Button_en = 0;
- byte state = 0;
- byte Channl_buf = 0;
- // Return if the key was pressed
- bool RC(long BT_ID)
- {
- if (Timeout < millis() - timeout_mark)
- Button_ID = 0x733;
- if (BT_ID == Button_ID)
- return 1;
- return 0;
- }
- //Configuration Remote Control
- void IRRC_setup(int pin, uint8_t Channl)
- {
- pinMode(pin, INPUT); //set the pin to input
- digitalWrite(pin, HIGH); //use the internal pullup resistor
- time = micros();
- enableInterrupt(pin, change, CHANGE);
- switch ( Channl )
- {
- case 1:
- Channl_ID = 0xFC;
- return;
- case 2:
- Channl_ID = 0x3C;
- return;
- case 3:
- Channl_ID = 0xCC;
- return;
- case 4:
- Channl_ID = 0x0C;
- return;
- case 5:
- Channl_ID = 0xF0;
- return;
- case 6:
- Channl_ID = 0x30;
- return;
- case 7:
- Channl_ID = 0xC0;
- return;
- case 8:
- Channl_ID = 0x00;
- return;
- //default:
- }
- }
- //Interrupt function
- void change()
- {
- dtime = micros();
- buf = dtime - time ;
- time = dtime;
- //procesar la duración del pulso (200 redondeando hacia arriba con buf% 200> 101 y (o) si el pulso es <101 asignación de una sola longitud)
- if (buf % 200 > 101)
- {
- buf = (buf / 200) + 1;
- }
- else
- {
- buf = buf / 200;
- }
- if (buf == 0) buf = 1;
- if (state == 0) // Initial state
- {
- if (!impuls) //Checking the first bits (000)
- {
- state++;
- res = 0;
- res = res << buf;
- //Serial.println("Start");
- }
- } else {
- if (impuls)
- {
- res = res << buf;
- for (int i = 0; i < buf ; i++)
- {
- res |= 1 << i ;
- }
- }else{
- res = res << buf;
- if ((byte)(res & 0x7F) == 0x38)
- {
- buf = 1;
- for (int i = 1; i < Shift; i ++)
- buf = (buf << 1) + 1;
- Channl_buf = (res >> 6) & buf ;
- if (Channl_buf == Channl_ID)
- {
- Button_ID = res >> (6+Shift);
- while(!(Button_ID & 1)) Button_ID = Button_ID >> 1;
- timeout_mark = millis();
- //Serial.println (Button_ID, HEX);
- }
- state = 0;
- res = 0;
- }
- }
- }
- impuls = !impuls;
- }
- void setup() {
- Otto.init(LeftLeg, RightLeg, LeftFoot, RightFoot, true, Buzzer);
- Otto.home();
- ir_rx.enableIRIn();
- IRRC_setup(2,1);
- }
- void loop() {
- if ((unsigned long)fnc_ir_rx_decode() & (0x00FF629D)) {
- Otto.walk(1,1000,1); // FORWARD
- }
- if ((unsigned long)fnc_ir_rx_decode() & (0x00FFA857)) {
- Otto.walk(1,1000,-1); // BACKWARD
- }
- if ((unsigned long)fnc_ir_rx_decode() & (0x00FF22DD)) {
- Otto.turn(1,1000,1); // LEFT
- }
- if ((unsigned long)fnc_ir_rx_decode() & (0x00FFC23D)) {
- Otto.turn(1,1000,-1); // RIGHT
- }
- if ((unsigned long)fnc_ir_rx_decode() & (0x00FFC23D)) {
- Otto.turn(1,1000,-1); // RIGHT
- }
- }
Add Comment
Please, Sign In to add comment