Ovduino

Robottino comandato da arduino tramite telecomando infrarossi e Nunchuck Wii rotto

ozio_gallery_jgallery

Cosa serve:

  • Arduino Uno
  • Breadboard
  • Cavetti
  • Servomotore
  • Led ...
  • Telecomando IR
  • Nunchuck Wii

Downolad:

Codice sorgente:


/* 
Ovduino - ZappocoS - 3/03/2014
cavo giallo  pin  5 - occhi - led bicolore
cavo verde   pin  6 - occhi - led bicolore
cavo arancio pin  7 - bocca - led giallo
cavo blu     pin  9 - servomotore testa
cavo bianco  pin 11 - controllo infrarossi - segnale
*/
// Nunchuck
#include <Wire.h>
#include <ArduinoNunchuk.h>
ArduinoNunchuk nunchuk = ArduinoNunchuk();
#include "IRremote.h"
#include <Servo.h>
uint8_t buf[6];     // buffer for the six-bytes packet coming from Nunchuck
int cnt=0;          // count of bytes in the buffer
int readcnt=0;      // number of times we read a packet from Nunchuck
int ledPin=13;      // which pin will host the blinking LED
boolean StsDummy = false;
Servo myServo;
int receiver = 11;  // pin 1 of IR receiver to Arduino digital pin 11
int pinBocca = 7;   // Led Bocca
int pinForward = 5; // Avanti
int pinStop = 6;    // Indietro
int pinTesta = 9;   // Servocomando
boolean stsTelecomando = false;
boolean stspinBocca = false;
boolean stspinForward = false;
boolean stspinStop = false;
boolean stspinLeft = false;
boolean stspinRight = false;
int stepAngle = 10;
int AngTesta;
int AngMin = -90;
int AngMax = 90;
//int ServoMin = 0;
//int ServoMax = 179;
int ServoMin = 60;
int ServoMax = 119;
int Angle = (AngMin + AngMax)/2 ;
/*-----( Declare objects )-----*/
IRrecv irrecv(receiver);           // create instance of 'irrecv'
decode_results results;            // create instance of 'decode_results'
void setup()
{
  Serial.begin(19200);
  // Nunchuck  
  nunchuk.init();
  //nunchuck_init();
  Serial.println("Test per controllo Robot OvoDuino");
  Serial.println("marzo 2014");  
  irrecv.enableIRIn(); // Start the receiver
  myServo.attach(pinTesta);
  pinMode (pinBocca, OUTPUT);
  pinMode (pinForward, OUTPUT); 
  pinMode (pinStop, OUTPUT);
}
void loop()
{
  // Controllo infrarossi
  if (irrecv.decode(&results)) // have we received an IR signal?
  {
    // Serial.println(results.value, HEX);  //UN Comment to see raw values
    translateIR(); 
    irrecv.resume(); // receive the next value
  } 
  // Controllo Nunchuck
  else {
    //nunchuk.update();   // Utilizzo libreria
    nunchuck_update ();         
  }
  // esegue operazioni in funzione degli eventi
    if(stspinBocca == true) {
        digitalWrite (pinBocca, HIGH);
        }
    else
        {
        digitalWrite (pinBocca, LOW);
        }
    if(stspinForward == true) {
        digitalWrite (pinForward, HIGH);
        }
    else
        {
        digitalWrite (pinForward, LOW);
        }
    if(stspinStop == true) {
        digitalWrite (pinStop, HIGH);
        }
    else
        {
        digitalWrite (pinStop, LOW);
        }
    ControlloTesta ();        
}
/*-----( Declare User-written Functions )-----*/
//
// Reset dello stato delle variabili
//
void ResetOriginaValue() 
{
        stspinBocca = false;
        stspinForward = false;
        stspinStop = false;
        stspinLeft = false;
        stspinRight = false;
        Angle = (AngMin + AngMax)/2 ;
        stepAngle = 10;
}
//
// Controllo funzioni della testa OvDuino
//
void ControlloTesta() 
{
    if(stspinLeft == true) {
        Angle = Angle + stepAngle;
        stspinLeft = false;
        }
    if(stspinRight == true) {
        Angle = Angle - stepAngle;
        stspinRight = false;
        }
      if (Angle < AngMin) {
        Angle = AngMin;
      }
      if (Angle > AngMax) {
        Angle = AngMax;
      }
      Serial.print(" Angle ");    
      Serial.println( Angle);    
      AngTesta = map(Angle, AngMin, AngMax, ServoMin, ServoMax);
      myServo.write(AngTesta);
}
void translateIR() // takes action based on IR code received
// describing KEYES Remote IR codes 
{
  switch(results.value)
  {
  case 0xFF629D: Serial.println(" FORWARD"); 
       stspinForward = !stspinForward;
       break;
  case 0xFF22DD: Serial.println(" LEFT");
       stspinLeft = true;
       break;
  case 0xFF02FD: Serial.println(" -OK-");    
       stspinBocca = !stspinBocca;
       break;
  case 0xFFC23D: Serial.println(" RIGHT");
       stspinRight = true;
       break;
  case 0xFFA857: Serial.println(" REVERSE"); 
       stspinStop = !stspinStop;
       break;
  case 0xFF6897: Serial.println(" 1"); 
       stspinForward = !stspinForward;
       stspinLeft = true;
       break;
  case 0xFF9867: Serial.println(" 2");    break;
  case 0xFFB04F: Serial.println(" 3");    
       stspinForward = !stspinForward;
       stspinRight = true;
       break;
  case 0xFF30CF: Serial.println(" 4");    
       stspinLeft = true;
       break;
  case 0xFF18E7: Serial.println(" 5");    break;
  case 0xFF7A85: Serial.println(" 6");
       stspinRight = true;
       break;
  case 0xFF10EF: Serial.println(" 7");
       stspinStop = !stspinStop;
       stspinLeft = true;
       break;
  case 0xFF38C7: Serial.println(" 8");    break;
  case 0xFF5AA5: Serial.println(" 9");  
       stspinStop = !stspinStop;
       stspinRight = true;
       break;
  case 0xFF42BD: Serial.println(" *"); 
       stepAngle = stepAngle * 2;
       if (stepAngle > 45) {
         stepAngle = 45;
       }
       break;
  case 0xFF4AB5: Serial.println(" 0");
       ResetOriginaValue();
       break;
  case 0xFF52AD: Serial.println(" #"); 
       stepAngle = stepAngle / 2;
       if (stepAngle < 2) {
         stepAngle = 2;
       }
       break;
  case 0xFFFFFFFF: Serial.println(" REPEAT");break;  
  default: 
    Serial.println(" other button   ");
  }// End Case
  //delay(100); // Do not get immediate repeat
} //END translateIR
// ---------------------------------------------------
//
// Nunchuck
//
// ---------------------------------------------------
uint8_t nunchuk_decode(uint8_t x)   // decode nunchuck data
{
  return (x^0x17)+0x17;             // not that an hard encryption...
}
/*
void nunchuck_init()                // inizializzazione
{
  int n;
  for(n=0; n<6; n++) buf[n]=0;      // fill buffer with zero values
  Wire.begin();                     // TWI init
  Wire.beginTransmission(0x52);     // nunchuck init
  Wire.write(0x55);
  Wire.write(0xF0);
  Wire.endTransmission();
  Wire.beginTransmission(0x52);     // nunchuck init  
  Wire.write(0x00);
  Wire.write(0xFB);
  Wire.endTransmission();
}
*/
void nunchuck_update()                // inizializzazione
{
    Wire.requestFrom(0x52, 6);        // request data from nunchuck
    while(Wire.available())           // read data and light the LED
    {
      buf[cnt++] = nunchuk_decode(Wire.read());
    }
    //if(cnt>=6)                        // an entire Nunchuck packet was read?
    //{
      // nunchuckprintdata();            // yes, print it
      cnt=0;                          // clear buffer counter
      nunchuck_ack();                 // acknowledge received packet
      // tasto C
      StsDummy = buf[5]&2 ? false : true;    
      if ( StsDummy == true){
         stspinBocca = !stspinBocca;
      }
      // tasto z
      StsDummy = buf[5]&1 ? false : true;    
      if ( StsDummy == true){
         ResetOriginaValue();
      }
      // Avanti
      if (((int)buf[1]-131) > 20){
        stspinForward = !stspinForward;
        stspinStop = false;
      }
      // Indietro
      else if (((int)buf[1]-131) < -20){
        stspinForward = false;
        stspinStop = !stspinStop;
      }
      // Right
      if (((int)buf[0]-167) > 20){
        stspinRight = true;
      }
      // Left
      if (((int)buf[0]-167) < -20){
        stspinLeft = true;
      }
    //}
}
void nunchuck_ack()                 // acknowledge a Nunchuck packet
{
   Wire.beginTransmission(0x52);     // send a zero to device 0x52
   Wire.write(0);
   Wire.endTransmission();
}
void nunchuckprintdata()                    // print out the values
{
  int n;   // note: the 123,131,524,597... depend on my Nunchuck calibration
  Serial.print(++readcnt, DEC);
  Serial.print("\t");               // joystick x/y values
  Serial.print((int)buf[0]-123);
  Serial.print("\t");  
  Serial.print((int)buf[1]-131);
  n=(buf[2]<<2)+((buf[5]>>2)&3)-524;  // accel X
  Serial.print("\t");  
  Serial.print(n);
  n=(buf[3]<<2)+((buf[5]>>4)&3)-597;  // accel Y
  Serial.print("\t");  
  Serial.print(n);
  n=(buf[4]<<2)+((buf[5]>>6)&3)-668;  // accel Z
  Serial.print("\t");  
  Serial.print(n);
  Serial.print(buf[5]&1 ? "\t- " : "\t[z] ");
  Serial.print(buf[5]&2 ? "-" : "[c]");
  // Serial.print(buf[5]);
  Serial.print ("\r\n");
}
/* ( THE END ) */
 

ZappocoJ, marzo 2014