Elektriska kanot (3 / 8 steg)
Steg 3: elektronik
Här är arduino koden jag använde:
WiiChuck.h och ServoTimer1.h är båda tillgängliga on-line. Släpp ett meddelande om du vill ha dem och inte kan hitta dem.
******************
#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 / / minsta servo läge
#define MAXTILLPULSE 1900 / / minsta servo läge
#define MINTHROTTPULSE 1000 / / minsta servo läge
#define MAXTHROTTPULSE 1965 / / maximalt servo läge
ServoTimer1 rorkult = ServoTimer1();
WiiChuck chuck = WiiChuck();
int tillerPulse = 1500; Belopp till puls servo
int throttlePulse = 1500; Belopp till puls servo
int throttleDiffPulse = 1500; Belopp till puls servo
int lastTillerPulse = 1500;
int baseTillerPulse = 1500;
int flytta = 10;
lång lastPulse = 0; tid i millisekunder för den sista pulsen
int refreshTime = 20; den tid som behövs mellan pulser
int analogValue = 0; det värde som returneras från analog sensor
int analogPin = 0; den analoga stift som sensorn är på
booleska vakna = sant;
långa insomningstimern = 0;
booleska dothrottle = sant;
void setup() {
Serial.BEGIN(9600);
Chuck.BEGIN();
Chuck.Update();
tiller.setMaximumPulse(2500);
tiller.setMinimumPulse(500);
för (int jag = 8; jag < 13; i ++) {
pinMode (i, matas); Ställ in servo pin som en utgångsstiftet
}
}
int metar;
void loop() {
Chuck.Update();
lastTillerPulse = tillerPulse;
om (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;
}
annat {
om (abs(chuck.readJoyX()) > 10) {
tillerPulse = baseTillerPulse - chuck.readJoyX();
}
annat {
tillerPulse = baseTillerPulse;
}
}
om (chuck.buttonZ) {
throttlePulse = (int) (1500.0 + chuck.readJoyY() * 5);
}
om (throttlePulse < MINTHROTTPULSE) {
throttlePulse = MINTHROTTPULSE;
}
om (throttlePulse > MAXTHROTTPULSE) {
throttlePulse = MAXTHROTTPULSE;
}
om (throttleDiffPulse < MINTHROTTPULSE) {
throttleDiffPulse = MINTHROTTPULSE;
}
om (throttleDiffPulse > MAXTHROTTPULSE) {
throttleDiffPulse = MAXTHROTTPULSE;
}
om (tillerPulse < MINTILLPULSE) {
tillerPulse = MINTILLPULSE;
}
om (tillerPulse > MAXTILLPULSE) {
tillerPulse = MAXTILLPULSE;
}
Serial.Print(tillerPulse);
Serial.Print (",");
Serial.println(throttlePulse);
om (tillerPulse! = lastTillerPulse) {
vakna = sant;
insomningstimern = 0;
}
annat {
insomningstimern + = 1;
}
om (insomningstimern > 80)
vakna = false;
om (vakna) {
digitalWrite (TILL_POWER_PIN, hög);
}
annat {
digitalWrite(TILL_POWER_PIN,LOW);
}
updateServos();
}
void updateServos() {
analogValue = analogRead(analogPin); Läs den analog ingången
tillerPulse = (analogValue * 19) / 10 + MINPULSE; konvertera det analoga värdet
till ett intervall mellan MINPULSE
och MAXPULSE.
om (dothrottle) {
tillerPulse servo igen om rhe uppdatera tid (20 ms) har gått:
digitalWrite (THROTT_MAIN_PIN, hög);
delayMicroseconds(throttlePulse);
digitalWrite (THROTT_MAIN_PIN, låg); Aktivera motorn
digitalWrite (THROTT_DIFF_PIN, hög);
delayMicroseconds(throttleDiffPulse);
digitalWrite (THROTT_DIFF_PIN, låg); Stear Motors
delayMicroseconds (5000 - throttlePulse - throttleDiffPulse);
}
annat {
delayMicroseconds(5000);
}
dothrottle =! dothrottle;
digitalWrite (TILL_STBD_PIN, hög); Aktivera motorn
digitalWrite (TILL_PORT_PIN, hög); Aktivera motorn
delayMicroseconds(tillerPulse); Längden på pulsen anger motor
digitalWrite (TILL_STBD_PIN, låg); Aktivera motorn
digitalWrite (TILL_PORT_PIN, låg); Aktivera motorn
delayMicroseconds(5000-tillerPulse);
}