Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Created by Elie Rosen with code from Adam Stambler and Tod E. Kurt
- #define motor1Dir 7
- #define motor2Dir 8
- #define motor1PWM 9
- #define motor2PWM 10
- #define motor1Enable 11
- #define motor2Enable 12
- #include <avr/interrupt.h>
- #include <Wire.h>
- #include "nunchuck_funcs.h"
- byte joyx,joyy,accx,accy,zbut,cbut;
- void setMotorVel(int dirPin, int pwmPin, int velocity){
- if (velocity >= 255) velocity = 255;
- if (velocity <= -255) velocity = -255;
- if (velocity == 0)
- {
- digitalWrite(dirPin, HIGH);
- digitalWrite(pwmPin, HIGH);
- }
- else if(velocity <0){ // Reverse
- digitalWrite(dirPin, HIGH);
- analogWrite(pwmPin, 255+velocity);
- }
- else if(velocity >0){ // Forward
- digitalWrite(dirPin,LOW);
- analogWrite(pwmPin, velocity);
- }
- }
- void setLeftMotorSpeed(int velocity)
- {
- setMotorVel(motor1Dir, motor1PWM, -velocity);
- }
- void setRightMotorSpeed(int velocity){
- setMotorVel(motor2Dir, motor2PWM, -velocity);
- }
- void initMotorDriver()
- {
- pinMode(motor1Dir, OUTPUT);
- pinMode(motor2Dir, OUTPUT);
- pinMode(motor1Enable, OUTPUT);
- pinMode(motor2Enable, OUTPUT);
- digitalWrite(motor1Enable,HIGH);
- digitalWrite(motor2Enable,HIGH);
- setLeftMotorSpeed(0); // make sure the motors are stopped
- setRightMotorSpeed(0);
- }
- #include <stdio.h>
- void Stop()
- {
- setLeftMotorSpeed(0);
- setRightMotorSpeed(0);
- }
- void goForward()
- {
- setLeftMotorSpeed(255);
- setRightMotorSpeed(255);
- }
- void goRight()
- {
- setLeftMotorSpeed(255);
- setRightMotorSpeed(-255);
- }
- void goLeft()
- {
- setLeftMotorSpeed(-255);
- setRightMotorSpeed(255);
- }
- void goReverse()
- {
- setLeftMotorSpeed(-255);
- setRightMotorSpeed(-255);
- }
- void setup()
- {
- Serial.begin(19200);
- nunchuck_setpowerpins();
- nunchuck_init(); // send the initilization handshake
- initMotorDriver();
- setLeftMotorSpeed(0);
- setRightMotorSpeed(0);
- delay(500);
- Serial.println("Begin your drive");
- }
- void loop()
- {
- nunchuck_get_data();
- joyx = nunchuck_joyx();
- joyy = nunchuck_joyy();
- accx = nunchuck_accelx(); // ranges from approx 70 - 182
- accy = nunchuck_accely(); // ranges from approx 65 - 173
- zbut = nunchuck_zbutton();
- cbut = nunchuck_cbutton();
- // Serial.print("Joyx: "); Serial.print((byte)joyx,DEC);
- // Serial.print("\tJoyy: "); Serial.println((byte)joyy,DEC);
- if (joyx>125 && joyx<135 && joyy>128 && joyy<138)
- {
- Stop();
- Serial.println("Stop");
- }
- if (joyy>=200)
- {
- goForward();
- Serial.println("Foward");
- }
- if (joyx<=45)
- {
- goLeft();
- Serial.println("Left");
- }
- if (joyx>=200)
- {
- goRight();
- Serial.println("Right");
- }
- if (joyy<=40)
- {
- goReverse();
- Serial.println("Reverse");
- }
- delay(100);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement