mikdonalds
Published © GPL3+

WALL-E Revived

My first Arduino project: WALL-E brought to life!

BeginnerShowcase (no instructions)419
WALL-E Revived

Things used in this project

Hardware components

Arduino UNO
Arduino UNO
×1
SparkFun Full-Bridge Motor Driver Breakout - L298N
SparkFun Full-Bridge Motor Driver Breakout - L298N
×1
Geared DC Motor, 12 V
Geared DC Motor, 12 V
9V DC Motor + gear from old cd-rom/dvd
×4

Hand tools and fabrication machines

Soldering iron (generic)
Soldering iron (generic)
Hot glue gun (generic)
Hot glue gun (generic)

Story

Read more

Schematics

schematic

Code

WALL-E Code

Arduino
Code to read Nunchuk and power the motors according to movement of the joystick
//L293D
//Motor A
const int motorPin1  = 9;  // Pin 14 of L293
const int motorPin2  = 10;  // Pin 10 of L293
//Motor B
const int motorPin3  = 6; // Pin  7 of L293
const int motorPin4  = 5;  // Pin  2 of L293

//This will run only one time.
#include <Wire.h>
#include "Nunchuk.h"

void setup() {
    Serial.begin(9600);
    Wire.begin();
     // Change TWI speed for nuchuk, which uses Fast-TWI (400kHz)
    Wire.setClock(400000);
    // nunchuk_init_power(); // A1 and A2 is power supply
    nunchuk_init();
 
    //Set pins as outputs
    pinMode(motorPin1, OUTPUT);
    pinMode(motorPin2, OUTPUT);
    pinMode(motorPin3, OUTPUT);
    pinMode(motorPin4, OUTPUT);
}

void loop(){

  if (nunchuk_read()) {

int nunX =  nunchuk_joystickX();
int nunY =  nunchuk_joystickY();
if (nunX < -20) // IF X axis is a lower than 20 we go left
    {  
    int nunXneg = map(nunX, 0,  -100, 0, 240); //240 is the maximum speed for the motor so we map the reading to a number from 0 to 240
    Serial.println(nunXneg);
    analogWrite(motorPin1, 0);
    analogWrite(motorPin2, nunXneg);
    analogWrite(motorPin3, nunXneg);
    analogWrite(motorPin4, 0);
    }else if(nunX > 20) // IF X axis is higher than 20 we go right
    {  
    int nunXpos = map(nunX, 0,  100, 0, 240);
    Serial.println(nunXpos);
    analogWrite(motorPin1, nunXpos);
    analogWrite(motorPin2, 0);
    analogWrite(motorPin3, 0);
    analogWrite(motorPin4, nunXpos);
    }else if(nunY < -20) // IF Y axis is lower than 20 we go forward
    { 
    int nunYneg = map(nunY, 0,  -100, 0, 240);
    Serial.println(nunYneg);
    analogWrite(motorPin1, 0);
    analogWrite(motorPin2, nunYneg);
    analogWrite(motorPin3, 0);
    analogWrite(motorPin4, nunYneg);
    }else if(nunY > 20) // IF Y axis is higher than 20 we go backward
    {  
    int nunYpos = map(nunY, 0,  100, 0, 240);
    Serial.println(nunYpos);
    analogWrite(motorPin1, nunYpos);
    analogWrite(motorPin2, 0);
    analogWrite(motorPin3, nunYpos);
    analogWrite(motorPin4, 0);
    }
    else{ //if joystick is not moved we do nothing
    analogWrite(motorPin1, 0);
    analogWrite(motorPin2, 0);
    analogWrite(motorPin3, 0);
    analogWrite(motorPin4, 0);
    }
delay(10);
}
}

Credits

mikdonalds

mikdonalds

0 projects • 0 followers

Comments