Go Down

Topic: Arduino smart car (Read 2017 times) previous topic - next topic

May 31, 2014, 07:15 pm Last Edit: Jun 06, 2014, 11:28 am by marco_arduino Reason: 1
My Arduino smart car,
control is via WII nunchuk and nrf24l01+

http://youtu.be/3_f8sF2zc5s
http://www.youtube.com/watch?v=3_f8sF2zc5s
code:
Code: [Select]

/*

*/
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
/*-----( Declare Constants and Pin Numbers )-----*/
#define CE_PIN   9
#define CSN_PIN 10


const int AIA = 30;  // (pwm) pin 9 connected to pin A-IA
const int AIB = 31;  // (pwm) pin 5 connected to pin A-IB
const int BIA = 33; // (pwm) pin 10 connected to pin B-IA  
const int BIB = 34;  // (pwm) pin 6 connected to pin B-IB

const int CIA = 35;  // (pwm) pin 9 connected to pin c-Ic
const int CIB = 36;  // (pwm) pin 5 connected to pin c-Ic
const int DIA = 37; // (pwm) pin 10 connected to pin d-Id  
const int DIB = 38;  // (pwm) pin 6 connected to pin d-Id

byte speed = 255;  // change this (0-255) to control the speed of the motors

const uint64_t pipe = 0xE8E8F0F0E1LL; // Define the transmit pipe
int led = 13;
int mydata[2];
   
   RF24 radio(CE_PIN, CSN_PIN); // Create a Radio
 


void setup() {
 
    mydata[0]=0;
    pinMode(led, OUTPUT);
   Serial.begin(9600);
     radio.begin();
 radio.openReadingPipe(1,pipe);
 radio.startListening();;

   
 
 pinMode(AIA, OUTPUT); // set pins to output
 pinMode(AIB, OUTPUT);
 pinMode(BIA, OUTPUT);
 pinMode(BIB, OUTPUT);
 
  pinMode(CIA, OUTPUT); // set pins to output
 pinMode(CIB, OUTPUT);
 pinMode(DIA, OUTPUT);
 pinMode(DIB, OUTPUT);
 stopnow();
}

void loop() {
 
 mydata[0]=0;
  if ( radio.available() )
 {
   // Read the data payload until we've received everything
   bool done = false;
   while (!done)
   {
     // Fetch the data payload
  done = radio.read( mydata, sizeof(mydata) );
 
     Serial.print(" mydata: ");

     Serial.println(mydata[0]);
     
     
//     Serial.print(" Y = ");      
  //   Serial.println(joystick[1]);
      digitalWrite(led, HIGH);  
  //  delay(100);
   }
   
   
 }
 
 
 else
 {    
 //    Serial.println("No radio available");
      digitalWrite(led, LOW);
  //    delay(100);
 }

if (mydata[0]==1) forward();
if (mydata[0]==2) backward();
if (mydata[0]==3) left();
if (mydata[0]==4) right();
if (mydata[0]==0) stopnow();

delay(40);


}

void stopnow()
{
 analogWrite(AIA, 0);
 analogWrite(AIB, 0);
 analogWrite(BIA, 0);
 analogWrite(BIB, 0);
 
   analogWrite(CIA, 0);
 analogWrite(CIB, 0);
 analogWrite(DIA, 0);
 analogWrite(DIB, 0);
}



void backward()
{
 analogWrite(AIA, speed);//left top
 analogWrite(AIB, 0);
analogWrite(BIA, speed);//right top
analogWrite(BIB, 0);
 
 analogWrite(CIA, 0);
analogWrite(CIB, speed);
analogWrite(DIA, 0);
 analogWrite(DIB, speed);
 
 
}

void forward()
{
analogWrite(AIA, 0);//left top
 analogWrite(AIB, speed);
analogWrite(BIA, 0);//right top
analogWrite(BIB, speed);
 
 analogWrite(CIA, speed);
analogWrite(CIB, 0);
analogWrite(DIA, speed);
 analogWrite(DIB, 0);
 
}

void left()
{
 analogWrite(AIA, speed);
 analogWrite(AIB, 0);
 analogWrite(BIA, 0);
 analogWrite(BIB, speed);
 
 
 analogWrite(CIA, 0);
analogWrite(CIB, speed);
analogWrite(DIA, speed);
 analogWrite(DIB, 0);
 
}

void right()
{
analogWrite(AIA, 0);
 analogWrite(AIB, speed);
 analogWrite(BIA, speed);
 analogWrite(BIB, 0);
 
 
 analogWrite(CIA, speed);
analogWrite(CIB, 0);
analogWrite(DIA, 0);
 analogWrite(DIB, speed);
}


robtillaart

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

spycatcher2k

Drew.
http://www.uk-pcb.co.uk - My UK Based PCB Fab & Assembly Company
Design work undertaken
SMD & Thru-Hole assembly


luisilva

marco_arduino, did you had seen this thread?

zaxarias

z.p

Made out well . you can upgrade it.

Go Up