Need a unique gift idea?
A Pastebin account makes a great Christmas gift
SHARE
TWEET

Wiichuck controlled RC Car

a guest Nov 9th, 2010 125 Never
Upgrade to PRO!
ENDING IN00days00hours00mins00secs
 
  1. // Created by Elie Rosen with code from Adam Stambler and Tod E. Kurt
  2.  
  3. #define motor1Dir 7
  4. #define motor2Dir 8
  5. #define motor1PWM 9
  6. #define motor2PWM 10
  7. #define motor1Enable 11
  8. #define motor2Enable 12
  9.  
  10. #include <avr/interrupt.h>
  11. #include <Wire.h>
  12. #include "nunchuck_funcs.h"
  13.  
  14.  
  15. byte joyx,joyy,accx,accy,zbut,cbut;
  16.  
  17. void setMotorVel(int dirPin, int pwmPin, int velocity){
  18.   if (velocity >= 255) velocity = 255;
  19.   if (velocity <= -255) velocity = -255;
  20.  
  21.   if (velocity == 0)
  22.   {
  23.     digitalWrite(dirPin, HIGH);
  24.     digitalWrite(pwmPin, HIGH);
  25.   }
  26.   else if(velocity <0){  // Reverse
  27.     digitalWrite(dirPin, HIGH);
  28.     analogWrite(pwmPin, 255+velocity);
  29.   }
  30.   else if(velocity >0){ // Forward
  31.     digitalWrite(dirPin,LOW);
  32.     analogWrite(pwmPin, velocity);
  33.   }
  34. }
  35.  
  36.  
  37. void setLeftMotorSpeed(int velocity)
  38. {
  39.   setMotorVel(motor1Dir, motor1PWM, -velocity);
  40.  
  41. }
  42.  
  43. void setRightMotorSpeed(int velocity){
  44.   setMotorVel(motor2Dir, motor2PWM, -velocity);
  45. }
  46.  
  47. void initMotorDriver()
  48. {
  49.   pinMode(motor1Dir, OUTPUT);
  50.   pinMode(motor2Dir, OUTPUT);
  51.  
  52.   pinMode(motor1Enable, OUTPUT);
  53.   pinMode(motor2Enable, OUTPUT);
  54.   digitalWrite(motor1Enable,HIGH);
  55.   digitalWrite(motor2Enable,HIGH);
  56.   setLeftMotorSpeed(0); // make sure the motors are stopped
  57.   setRightMotorSpeed(0);
  58. }
  59.  
  60. #include <stdio.h>
  61.  
  62. void Stop()
  63. {
  64.   setLeftMotorSpeed(0);
  65.   setRightMotorSpeed(0);
  66. }
  67.  
  68.  
  69. void goForward()
  70. {
  71.   setLeftMotorSpeed(255);
  72.   setRightMotorSpeed(255);
  73. }
  74.  
  75. void goRight()
  76. {
  77.   setLeftMotorSpeed(255);
  78.   setRightMotorSpeed(-255);
  79. }
  80.  
  81. void goLeft()
  82. {
  83.   setLeftMotorSpeed(-255);
  84.   setRightMotorSpeed(255);
  85. }
  86. void goReverse()
  87. {
  88.   setLeftMotorSpeed(-255);
  89.   setRightMotorSpeed(-255);
  90. }
  91.  
  92. void setup()
  93. {
  94.     Serial.begin(19200);
  95.     nunchuck_setpowerpins();
  96.     nunchuck_init(); // send the initilization handshake
  97.     initMotorDriver();
  98.     setLeftMotorSpeed(0);
  99.     setRightMotorSpeed(0);
  100.     delay(500);
  101.     Serial.println("Begin your drive");
  102. }
  103.  
  104. void loop()
  105. {
  106.         nunchuck_get_data();
  107.         joyx = nunchuck_joyx();
  108.         joyy = nunchuck_joyy();
  109.         accx  = nunchuck_accelx(); // ranges from approx 70 - 182
  110.         accy  = nunchuck_accely(); // ranges from approx 65 - 173
  111.         zbut = nunchuck_zbutton();
  112.         cbut = nunchuck_cbutton();
  113.        
  114.        
  115.       // Serial.print("Joyx: "); Serial.print((byte)joyx,DEC);
  116.       // Serial.print("\tJoyy: "); Serial.println((byte)joyy,DEC);
  117.      
  118.    
  119.   if (joyx>125 && joyx<135 && joyy>128 && joyy<138)
  120.   {
  121.     Stop();
  122.     Serial.println("Stop");
  123.   }
  124.   if (joyy>=200)
  125.   {
  126.     goForward();
  127.      Serial.println("Foward");
  128.   }
  129.   if (joyx<=45)
  130.   {
  131.     goLeft();
  132.      Serial.println("Left");
  133.   }
  134.   if (joyx>=200)
  135.   {
  136.     goRight();
  137.      Serial.println("Right");
  138.   }
  139.   if (joyy<=40)
  140.   {
  141.     goReverse();
  142.      Serial.println("Reverse");
  143.   }
  144.    delay(100);
  145. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top