Stap 3: elektronica
Hier is de code van de arduino die ik gebruikt:
WiiChuck.h en ServoTimer1.h zijn beide beschikbaar on-line. Een berichtje als je wilt dat ze en kan ze niet vinden.
******************
#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 positie
#define MAXTILLPULSE 1900 / / Minimum servo positie
#define MINTHROTTPULSE 1000 / / Minimum servo positie
#define MAXTHROTTPULSE 1965 / / Maximum servo positie
ServoTimer1 helmstok = ServoTimer1();
Chuck WiiChuck = WiiChuck();
int tillerPulse = 1500; Bedrag aan de servo pulse
int throttlePulse = 1500; Bedrag aan de servo pulse
int throttleDiffPulse = 1500; Bedrag aan de servo pulse
int lastTillerPulse = 1500;
int baseTillerPulse = 1500;
int verplaatsen = 10;
lange lastPulse = 0; de tijd in milliseconden van de laatste puls
int refreshTime = 20; de tijd die nodig is tussen pulsen
int analogValue = 0; de resultaatwaarde van de analoge sensor
int analogPin = 0; de analoge pin thats de sensor op
Booleaanse wake = true;
lange sleeptimer = 0;
Boole dothrottle = waar;
VOID Setup {}
Serial.begin(9600);
Chuck.begin();
Chuck.update();
tiller.setMaximumPulse(2500);
tiller.setMinimumPulse(500);
for (int i = 8; ik < 13; i ++) {}
pinMode (ik, OUTPUT); Set servo pin als een uitgang pins
}
}
int hoek;
void loop {}
Chuck.update();
lastTillerPulse = tillerPulse;
Als (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 {}
Als (abs(chuck.readJoyX()) > 10) {}
tillerPulse = baseTillerPulse - chuck.readJoyX();
}
else {}
tillerPulse = baseTillerPulse;
}
}
Als (chuck.buttonZ) {}
throttlePulse = (int) (1500.0 + chuck.readJoyY() * 5);
}
Als (throttlePulse < MINTHROTTPULSE) {}
throttlePulse = MINTHROTTPULSE;
}
Als (throttlePulse > MAXTHROTTPULSE) {}
throttlePulse = MAXTHROTTPULSE;
}
Als (throttleDiffPulse < MINTHROTTPULSE) {}
throttleDiffPulse = MINTHROTTPULSE;
}
Als (throttleDiffPulse > MAXTHROTTPULSE) {}
throttleDiffPulse = MAXTHROTTPULSE;
}
Als (tillerPulse < MINTILLPULSE) {}
tillerPulse = MINTILLPULSE;
}
Als (tillerPulse > MAXTILLPULSE) {}
tillerPulse = MAXTILLPULSE;
}
Serial.Print(tillerPulse);
Serial.Print (",");
Serial.println(throttlePulse);
Als (tillerPulse! = lastTillerPulse) {}
wakker = true;
Sleeptimer = 0;
}
else {}
Sleeptimer += 1;
}
Als (sleeptimer > 80)
wakker = false;
Als (wake) {}
digitalWrite (TILL_POWER_PIN, hoge);
}
else {}
digitalWrite(TILL_POWER_PIN,LOW);
}
updateServos();
}
ongeldig updateServos() {}
analogValue = analogRead(analogPin); Lees de analoge ingang
tillerPulse = (analogValue * 19) / 10 + MINPULSE; de analoge waarde converteren
naar een bereik tussen MINPULSE
en MAXPULSE.
Als (dothrottle) {}
tillerPulse de servo opnieuw als rhe vernieuwen tijd (20 ms) hebben doorgegeven:
digitalWrite (THROTT_MAIN_PIN, hoge);
delayMicroseconds(throttlePulse);
digitalWrite (THROTT_MAIN_PIN, laag); Zet de motor
digitalWrite (THROTT_DIFF_PIN, hoge);
delayMicroseconds(throttleDiffPulse);
digitalWrite (THROTT_DIFF_PIN, laag); Stear Motors
delayMicroseconds (5000 - throttlePulse - throttleDiffPulse);
}
else {}
delayMicroseconds(5000);
}
dothrottle =! dothrottle;
digitalWrite (TILL_STBD_PIN, hoge); Zet de motor
digitalWrite (TILL_PORT_PIN, hoge); Zet de motor
delayMicroseconds(tillerPulse); Lengte van de pols wordt de motor positie ingesteld
digitalWrite (TILL_STBD_PIN, laag); Zet de motor
digitalWrite (TILL_PORT_PIN, laag); Zet de motor
delayMicroseconds(5000-tillerPulse);
}