#include //including the libraries #include #define BAUDRATE 19200 //define the transimission rate int motorPin1 = 3; //declare the variables int motorPin2 = 4; int motorPin3 = 5; int motorPin4 = 6; int delayTime; int led = 11; ArduinoNunchuk nunchuk = ArduinoNunchuk(); void setup() { pinMode(led, OUTPUT); //set the led Serial.begin(BAUDRATE); //initialize the serial transmission nunchuk.init(); //initialize the nunchuck pinMode(motorPin1, OUTPUT); //initialize the 4 motor pins pinMode(motorPin2, OUTPUT); pinMode(motorPin3, OUTPUT); pinMode(motorPin4, OUTPUT); } void loop() { nunchuk.update(); //get data from the nunchuck Serial.print(nunchuk.analogX, DEC); //print them on the serial interface in decimal Serial.print(' '); Serial.print(nunchuk.analogY, DEC); Serial.print(' '); Serial.print(nunchuk.accelX, DEC); Serial.print(' '); Serial.print(nunchuk.accelY, DEC); Serial.print(' '); Serial.print(nunchuk.accelZ, DEC); Serial.print(' '); Serial.print(nunchuk.zButton, DEC); Serial.print(' '); Serial.println(nunchuk.cButton, DEC); byte brighteness = 6^abs(nunchuk.analogX - 124); //adjust the led brightness proportionally to the value of nunchuk.analogX analogWrite(led, brighteness); if(nunchuk.analogX == 124){ //set the motor pins to 0 if joystick is in rest position digitalWrite(motorPin4, LOW); digitalWrite(motorPin3, LOW); digitalWrite(motorPin2, LOW); digitalWrite(motorPin1, LOW); } if(nunchuk.analogX > 124){ //if the joystick moves upward if(nunchuk.analogX < 134){ //Create the ranges to set the delay Time (and consequently the velocity) based on the value of nunchuk.analogX delayTime = 100; } if(nunchuk.analogX > 134 && nunchuk.analogX < 144){ delayTime = 90; } if(nunchuk.analogX > 144 && nunchuk.analogX < 154){ delayTime = 80; } if(nunchuk.analogX > 154 && nunchuk.analogX < 164){ delayTime = 70; } if(nunchuk.analogX > 164 && nunchuk.analogX < 174){ delayTime = 60; } if(nunchuk.analogX > 174 && nunchuk.analogX < 184){ delayTime = 50; } if(nunchuk.analogX > 184 && nunchuk.analogX < 194){ delayTime = 40; } if(nunchuk.analogX > 194 && nunchuk.analogX < 204){ delayTime = 30; } if(nunchuk.analogX > 204 && nunchuk.analogX < 214){ delayTime = 20; } if(nunchuk.analogX > 214 && nunchuk.analogX < 224){ delayTime = 10; } if(nunchuk.analogX > 224 && nunchuk.analogX < 234){ delayTime = 7; } if(nunchuk.analogX > 234 && nunchuk.analogX < 244){ delayTime = 5; } if(nunchuk.analogX > 244){ delayTime = 3; } digitalWrite(motorPin4, HIGH); //let the motor move clockwise with velocity based on the dealyTime determined before digitalWrite(motorPin3, LOW); digitalWrite(motorPin2, LOW); digitalWrite(motorPin1, LOW); delay(delayTime); digitalWrite(motorPin4, LOW); digitalWrite(motorPin3, LOW); digitalWrite(motorPin2, HIGH); digitalWrite(motorPin1, LOW); delay(delayTime); digitalWrite(motorPin4, LOW); digitalWrite(motorPin3, HIGH); digitalWrite(motorPin2, LOW); digitalWrite(motorPin1, LOW); delay(delayTime); digitalWrite(motorPin4, LOW); digitalWrite(motorPin3, LOW); digitalWrite(motorPin2, LOW); digitalWrite(motorPin1, HIGH); delay(delayTime);} if(nunchuk.analogX < 124){ //if the joystick moves downward if(nunchuk.analogX > 114 && nunchuk.analogX < 124){ //set the delay Time (and consequently the velocity) based on the value of nunchuk.analogX delayTime = 100; } if(nunchuk.analogX < 114 && nunchuk.analogX > 104){ delayTime = 90; } if(nunchuk.analogX > 94 && nunchuk.analogX < 104){ delayTime = 80; } if(nunchuk.analogX > 84 && nunchuk.analogX < 94){ delayTime = 70; } if(nunchuk.analogX > 74 && nunchuk.analogX < 84){ delayTime = 60; } if(nunchuk.analogX > 64 && nunchuk.analogX < 74){ delayTime = 50; } if(nunchuk.analogX < 64 && nunchuk.analogX > 54){ delayTime = 40; } if(nunchuk.analogX > 44 && nunchuk.analogX < 54){ delayTime = 30; } if(nunchuk.analogX > 34 && nunchuk.analogX < 44){ delayTime = 20; } if(nunchuk.analogX > 24 && nunchuk.analogX < 34){ delayTime = 10; } if(nunchuk.analogX > 14 && nunchuk.analogX < 24){ delayTime = 7; } if(nunchuk.analogX > 4 && nunchuk.analogX < 14){ delayTime = 5; } if(nunchuk.analogX >= 0 && nunchuk.analogX < 4){ delayTime = 3; } digitalWrite(motorPin4, LOW); //move the motor counterclockwise with the velocity depending on the delayTime determined before digitalWrite(motorPin3, HIGH); digitalWrite(motorPin2, LOW); digitalWrite(motorPin1, LOW); delay(delayTime); digitalWrite(motorPin4, LOW); digitalWrite(motorPin3, LOW); digitalWrite(motorPin2, HIGH); digitalWrite(motorPin1, LOW); delay(delayTime); digitalWrite(motorPin4, HIGH); digitalWrite(motorPin3, LOW); digitalWrite(motorPin2, LOW); digitalWrite(motorPin1, LOW); delay(delayTime); digitalWrite(motorPin4, LOW); digitalWrite(motorPin3, LOW); digitalWrite(motorPin2, LOW); digitalWrite(motorPin1, HIGH); delay(delayTime); } }