Components and supplies
SparkFun Full-Bridge Motor Driver Breakout - L298N
Geared DC Motor, 12 V
Arduino UNO
Tools and machines
Soldering iron (generic)
Hot glue gun (generic)
Project description
Code
WALL-E Code
arduino
Code to read Nunchuk and power the motors according to movement of the joystick
1//L293D 2//Motor A 3const int motorPin1 = 9; // Pin 14 of L293 4const int motorPin2 = 10; // Pin 10 of L293 5//Motor B 6const int motorPin3 = 6; // Pin 7 of L293 7const int motorPin4 = 5; // Pin 2 of L293 8 9//This will run only one time. 10#include <Wire.h> 11#include "Nunchuk.h" 12 13void setup() { 14 Serial.begin(9600); 15 Wire.begin(); 16 // Change TWI speed for nuchuk, which uses Fast-TWI (400kHz) 17 Wire.setClock(400000); 18 // nunchuk_init_power(); // A1 and A2 is power supply 19 nunchuk_init(); 20 21 //Set pins as outputs 22 pinMode(motorPin1, OUTPUT); 23 pinMode(motorPin2, OUTPUT); 24 pinMode(motorPin3, OUTPUT); 25 pinMode(motorPin4, OUTPUT); 26} 27 28void loop(){ 29 30 if (nunchuk_read()) { 31 32int nunX = nunchuk_joystickX(); 33int nunY = nunchuk_joystickY(); 34if (nunX < -20) // IF X axis is a lower than 20 we go left 35 { 36 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 37 Serial.println(nunXneg); 38 analogWrite(motorPin1, 0); 39 analogWrite(motorPin2, nunXneg); 40 analogWrite(motorPin3, nunXneg); 41 analogWrite(motorPin4, 0); 42 }else if(nunX > 20) // IF X axis is higher than 20 we go right 43 { 44 int nunXpos = map(nunX, 0, 100, 0, 240); 45 Serial.println(nunXpos); 46 analogWrite(motorPin1, nunXpos); 47 analogWrite(motorPin2, 0); 48 analogWrite(motorPin3, 0); 49 analogWrite(motorPin4, nunXpos); 50 }else if(nunY < -20) // IF Y axis is lower than 20 we go forward 51 { 52 int nunYneg = map(nunY, 0, -100, 0, 240); 53 Serial.println(nunYneg); 54 analogWrite(motorPin1, 0); 55 analogWrite(motorPin2, nunYneg); 56 analogWrite(motorPin3, 0); 57 analogWrite(motorPin4, nunYneg); 58 }else if(nunY > 20) // IF Y axis is higher than 20 we go backward 59 { 60 int nunYpos = map(nunY, 0, 100, 0, 240); 61 Serial.println(nunYpos); 62 analogWrite(motorPin1, nunYpos); 63 analogWrite(motorPin2, 0); 64 analogWrite(motorPin3, nunYpos); 65 analogWrite(motorPin4, 0); 66 } 67 else{ //if joystick is not moved we do nothing 68 analogWrite(motorPin1, 0); 69 analogWrite(motorPin2, 0); 70 analogWrite(motorPin3, 0); 71 analogWrite(motorPin4, 0); 72 } 73delay(10); 74} 75}
WALL-E Code
arduino
Code to read Nunchuk and power the motors according to movement of the joystick
1//L293D 2//Motor A 3const int motorPin1 = 9; // Pin 14 of L293 4const int motorPin2 = 10; // Pin 10 of L293 5//Motor B 6const int motorPin3 = 6; // Pin 7 of L293 7const int motorPin4 = 5; // Pin 2 of L293 8 9//This will run only one time. 10#include <Wire.h> 11#include "Nunchuk.h" 12 13void setup() { 14 Serial.begin(9600); 15 Wire.begin(); 16 // Change TWI speed for nuchuk, which uses Fast-TWI (400kHz) 17 Wire.setClock(400000); 18 // nunchuk_init_power(); // A1 and A2 is power supply 19 nunchuk_init(); 20 21 //Set pins as outputs 22 pinMode(motorPin1, OUTPUT); 23 pinMode(motorPin2, OUTPUT); 24 pinMode(motorPin3, OUTPUT); 25 pinMode(motorPin4, OUTPUT); 26} 27 28void loop(){ 29 30 if (nunchuk_read()) { 31 32int nunX = nunchuk_joystickX(); 33int nunY = nunchuk_joystickY(); 34if (nunX < -20) // IF X axis is a lower than 20 we go left 35 { 36 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 37 Serial.println(nunXneg); 38 analogWrite(motorPin1, 0); 39 analogWrite(motorPin2, nunXneg); 40 analogWrite(motorPin3, nunXneg); 41 analogWrite(motorPin4, 0); 42 }else if(nunX > 20) // IF X axis is higher than 20 we go right 43 { 44 int nunXpos = map(nunX, 0, 100, 0, 240); 45 Serial.println(nunXpos); 46 analogWrite(motorPin1, nunXpos); 47 analogWrite(motorPin2, 0); 48 analogWrite(motorPin3, 0); 49 analogWrite(motorPin4, nunXpos); 50 }else if(nunY < -20) // IF Y axis is lower than 20 we go forward 51 { 52 int nunYneg = map(nunY, 0, -100, 0, 240); 53 Serial.println(nunYneg); 54 analogWrite(motorPin1, 0); 55 analogWrite(motorPin2, nunYneg); 56 analogWrite(motorPin3, 0); 57 analogWrite(motorPin4, nunYneg); 58 }else if(nunY > 20) // IF Y axis is higher than 20 we go backward 59 { 60 int nunYpos = map(nunY, 0, 100, 0, 240); 61 Serial.println(nunYpos); 62 analogWrite(motorPin1, nunYpos); 63 analogWrite(motorPin2, 0); 64 analogWrite(motorPin3, nunYpos); 65 analogWrite(motorPin4, 0); 66 } 67 else{ //if joystick is not moved we do nothing 68 analogWrite(motorPin1, 0); 69 analogWrite(motorPin2, 0); 70 analogWrite(motorPin3, 0); 71 analogWrite(motorPin4, 0); 72 } 73delay(10); 74} 75}
Downloadable files
schematic
schematic
Comments
Only logged in users can leave comments