The final step before the skateboard can be assembled and tested is writing and uploading the control software to the Arduino. Before you connect your Arduino to your laptop, make sure that it is not powered by the skateboard’s batteries via the BEC. Connecting the Arduino to your computer’s USB port while it is powered by the BEC can damage either your computer’s USB port, the Arduino, or both!
You can find the Arduino control software that I wrote in the github repository here! Right now, it is a pretty simple sketch that translates the nunchuck input into a motor speed that is communicated to the ESC. In the near future, I hope to write better, more advanced versions of this control software to improve the skateboard’s performance. Tweak and improve the code to your liking and share your improvements with me!
Skateboard Control Arduino Sketch:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 |
/* * Rahul Iyer */ #include <Servo.h> #include <Wire.h> #include <ArduinoNunchuk.h> #define BAUDRATE 19200 #define CHUCK_ZERO 133 #define CHUCK_MAX 255 #define ESC_ZERO 90 #define SPEED_MAX 130 ArduinoNunchuk nunchuk = ArduinoNunchuk(); int escOutputValue = ESC_ZERO; float currentOutputValue = ESC_ZERO; int resetCounter = 0; Servo ESC; void setup() { ESC.attach(9); Serial.begin(BAUDRATE); nunchuk.init(); delay(100); } void loop() { resetCounter++; if (resetCounter==40) { resetCounter=0; nunchuk.init(); delay(100); } nunchuk.update(); // Serial.print(nunchuk.analogX, DEC); // 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.print(nunchuk.cButton, DEC); // Serial.print(' '); int yValue = nunchuk.analogY; if (yValue<CHUCK_ZERO) { yValue = CHUCK_ZERO; // no backwards } // escOutputValue = map(yValue, CHUCK_ZERO, CHUCK_MAX, ESC_ZERO, SPEED_MAX); //linear relationship between joystick value and ESC output value--deemed not good enough :P if (yValue == CHUCK_ZERO) { escOutputValue = ESC_ZERO; //if joystick is at zero, no power to motor (coast) currentOutputValue = ESC_ZERO; //reset } else { int maxPossibleSpeed = map (yValue, CHUCK_ZERO, CHUCK_MAX, ESC_ZERO, SPEED_MAX); if (escOutputValue > maxPossibleSpeed) { escOutputValue = maxPossibleSpeed; } else { float increment = (1.0 * (yValue - CHUCK_ZERO)) / (CHUCK_MAX - CHUCK_ZERO); currentOutputValue += increment; escOutputValue = (int) currentOutputValue; if (escOutputValue > maxPossibleSpeed) { escOutputValue = maxPossibleSpeed; } } } ESC.write(escOutputValue); Serial.println(escOutputValue); //for debugging } |