Step 3Electronics
Here is the arduino code I used:
WiiChuck.h and ServoTimer1.h are both available on-line. Drop a message if you want them and cant find them.
******************
#include "Wire.h"
#include "WiiChuck.h"
#include "ServoTimer1.h"
#define TILL_POWER_PIN 8
#define TILL_STBD_PIN 10
#define TILL_PORT_PIN 9
#define THROTT_DIFF_PIN 12
#define THROTT_MAIN_PIN 11
#define MINTILLPULSE 1100 // Minimum servo position
#define MAXTILLPULSE 1900 // Minimum servo position
#define MINTHROTTPULSE 1000 // Minimum servo position
#define MAXTHROTTPULSE 1965 // Maximum servo position
//ServoTimer1 tiller = ServoTimer1();
WiiChuck chuck = WiiChuck();
int tillerPulse = 1500; // Amount to pulse the servo
int throttlePulse = 1500; // Amount to pulse the servo
int throttleDiffPulse = 1500; // Amount to pulse the servo
int lastTillerPulse = 1500;
int baseTillerPulse = 1500;
int move = 10;
long lastPulse = 0; // the time in milliseconds of the last pulse
int refreshTime = 20; // the time needed in between pulses
int analogValue = 0; // the value returned from the analog sensor
int analogPin = 0; // the analog pin that the sensor's on
boolean wake = true;
long sleeptimer = 0;
boolean dothrottle = true;
void setup() {
Serial.begin(9600);
chuck.begin();
chuck.update();
//tiller.setMaximumPulse(2500);
//tiller.setMinimumPulse(500);
for (int i =8; i<13;i++) {
pinMode(i, OUTPUT); // Set servo pin as an output pin
}
}
int angle;
void loop() {
chuck.update();
lastTillerPulse = tillerPulse;
if (chuck.buttonC) {
//tiller.attach(9);
//tiller.write(angle);
//tiller.attach(10);
//tiller.write(angle);
baseTillerPulse = (int)(1500.0 - chuck.readRoll() * 3);
throttleDiffPulse = (int)(1500.0 + chuck.readJoyX() * 5);
tillerPulse = baseTillerPulse;
}
else {
if (abs(chuck.readJoyX()) > 10) {
tillerPulse = baseTillerPulse - chuck.readJoyX();
}
else {
tillerPulse = baseTillerPulse;
}
}
if (chuck.buttonZ) {
throttlePulse = (int)(1500.0 + chuck.readJoyY() * 5);
}
if (throttlePulse < MINTHROTTPULSE) {
throttlePulse = MINTHROTTPULSE;
}
if (throttlePulse > MAXTHROTTPULSE) {
throttlePulse = MAXTHROTTPULSE;
}
if (throttleDiffPulse < MINTHROTTPULSE) {
throttleDiffPulse = MINTHROTTPULSE;
}
if (throttleDiffPulse > MAXTHROTTPULSE) {
throttleDiffPulse = MAXTHROTTPULSE;
}
if (tillerPulse < MINTILLPULSE) {
tillerPulse = MINTILLPULSE;
}
if (tillerPulse > MAXTILLPULSE) {
tillerPulse = MAXTILLPULSE;
}
Serial.print(tillerPulse);
Serial.print(", ");
Serial.println(throttlePulse);
if (tillerPulse != lastTillerPulse) {
wake = true;
sleeptimer = 0;
}
else {
sleeptimer += 1;
}
if (sleeptimer > 80)
wake = false;
if (wake) {
digitalWrite(TILL_POWER_PIN, HIGH);
}
else {
digitalWrite(TILL_POWER_PIN,LOW);
}
updateServos();
}
void updateServos() {
//analogValue = analogRead(analogPin); // read the analog input
//tillerPulse = (analogValue * 19) / 10 + MINPULSE; // convert the analog value
// to a range between MINPULSE
// and MAXPULSE.
if (dothrottle) {
// tillerPulse the servo again if rhe refresh time (20 ms) have passed:
digitalWrite(THROTT_MAIN_PIN, HIGH);
delayMicroseconds(throttlePulse);
digitalWrite(THROTT_MAIN_PIN, LOW); // Turn the motor on
digitalWrite(THROTT_DIFF_PIN, HIGH);
delayMicroseconds(throttleDiffPulse);
digitalWrite(THROTT_DIFF_PIN, LOW); // Stear Motors
delayMicroseconds(5000 - throttlePulse - throttleDiffPulse);
}
else {
delayMicroseconds(5000);
}
dothrottle = !dothrottle;
digitalWrite(TILL_STBD_PIN, HIGH); // Turn the motor on
digitalWrite(TILL_PORT_PIN, HIGH); // Turn the motor on
delayMicroseconds(tillerPulse); // Length of the pulse sets the motor position
digitalWrite(TILL_STBD_PIN, LOW); // Turn the motor on
digitalWrite(TILL_PORT_PIN, LOW); // Turn the motor on
delayMicroseconds(5000-tillerPulse);
}
| « Previous Step | Download PDFView All Steps | Next Step » |
1
comment
|
Add Comment
|
![]() |
Add Comment
|













































