Stap 3: Tweaks & testen
De kloon Ardunio hoefde opstartlader niet dus ik moest de oude trouwe GQ4-X-programmeur en de kloon maar de ICSP poort flitste.
De Arduino micro kloon had ik heb niet de functionaliteit van de usb-functie dan een emulatie van seriële poort te wijzigen dus ik moest schrijven van twee programma's in plaats van één.
De Arduino-programma in principe vangt de knoppen en de analoge stok en stuurt de gegevens via seriële
Analoge X -> A0
Analogie -> A1
Andere knoppen D0 <> - D8
Hier is de code voor de Micro
// These constants won't change. They're used to give names<br>// to the pins used: const int analogInPinY = A0; // Analog input pin that the potentiometer is attached to const int analogInPinX = A1; // Analog input pin that the potentiometer is attached to int outputValueY = 0; // value output to the PWM (analog out) int outputValueX = 0; // value output to the PWM (analog out) int centreY = 0; //Callibrate int centreX = 0; //Callibrate int Ypos = 0; int TempY = 0; int NegY = 0; int Xpos = 0; int TempX = 0; int NegX = 0; // constants won't change. They're used here to // set pin numbers: const int buttonup = 2; // the number of the pushbutton pin const int buttonright = 3; // the number of the pushbutton pin const int buttonleft = 4; // the number of the pushbutton pin const int buttondown = 5; // the number of the pushbutton pin const int buttonDiamond = 9; // the number of the pushbutton pin const int buttonTriangle = 8; // the number of the pushbutton pin const int buttonRed = 7; // the number of the pushbutton pin const int buttonYellow = 6; const int buttonAnalog = 10; const int buttonMode = 11; const int ledPin = 13; // the number of the LED pin const int led2Pin = A2; // variables will change: int dirState = 0; // variable for reading the pushbutton status int buttonState = 0; // variable for reading the pushbutton status int CTX; int AnalogNegs = 0; int PadMode; int Fader = 0; int FaderDir = 0; void setup() { Serial.begin(19200); // initialize the LED pin as an output: pinMode(ledPin, OUTPUT); pinMode(led2Pin, OUTPUT); // initialize the pushbutton pin as an input: pinMode(buttonup, INPUT_PULLUP); pinMode(buttondown, INPUT_PULLUP); pinMode(buttonleft, INPUT_PULLUP); pinMode(buttonright, INPUT_PULLUP); pinMode(buttonTriangle, INPUT_PULLUP); pinMode(buttonDiamond, INPUT_PULLUP); pinMode(buttonYellow, INPUT_PULLUP); pinMode(buttonRed, INPUT_PULLUP); pinMode(buttonAnalog, INPUT_PULLUP); pinMode(buttonMode, INPUT_PULLUP); CTX = analogRead(analogInPinX); centreY = map(analogRead(analogInPinY), 0, 1023, 0, 254); centreX = map(analogRead(analogInPinX), 0, 1023, 0, 254); Callib;} void Callib() { //TRY!! Callibrate Centre centreY = map(analogRead(analogInPinY), 0, 1023, 0, 254); centreX = map(analogRead(analogInPinX), 0, 1023, 0, 254); } void loop(){ // read the state of the pushbutton value: outputValueY = map(analogRead(analogInPinY), 0, 1023, 0, 254); outputValueX = map(analogRead(analogInPinX), 0, 1023, 0, 254); Ypos = 0; NegY = 0; TempY = (outputValueY - centreY); if (TempY > 5 || TempY < -5) { digitalWrite(ledPin, HIGH); NegY = TempY < -5 ; Ypos = TempY & 254; }; Xpos = 0; NegX = 0; TempX = (outputValueX - centreX); if (TempX > 5 || TempX < -5) { digitalWrite(ledPin, HIGH); NegX = TempX < -5 ; // if (TempX < -83){TempX = -84;} // if (TempX > 83){TempX = 84;} Xpos = TempX & 254; }; // Serial.println(TempX); //Serial.println(Xpos); dirState = 0; dirState |= (digitalRead(buttonup) ^ 1) << 0; dirState |= (digitalRead(buttondown) ^ 1) << 1; dirState |= (digitalRead(buttonleft) ^ 1) << 2; dirState |= (digitalRead(buttonright) ^ 1) << 3; buttonState = 0; buttonState |= (digitalRead(buttonTriangle) ^ 1) << 0; buttonState |= (digitalRead(buttonDiamond) ^ 1) << 1; buttonState |= (digitalRead(buttonYellow) ^ 1) << 2; buttonState |= (digitalRead(buttonRed) ^ 1) << 3; buttonState |= (digitalRead(buttonAnalog) ^ 1) << 4; int d = (digitalRead(buttonMode) ^ 1); if (PadMode > 0) { //delay(30); digitalWrite(led2Pin, HIGH);} else{ digitalWrite(led2Pin, LOW); } //if (PadMode = 0) {analogWrite(A2, 0);} if (d > 0 ){ PadMode = (PadMode ^ 1); // // PadMode = 1; // digitalWrite(A2, HIGH);} //return;} while ((digitalRead(buttonMode) ^ 1) > 0) { } delay(3);} buttonState |= PadMode << 5; AnalogNegs = NegX << 0; AnalogNegs |= NegY << 2; Serial.write(0xFF); //sync Serial.write(dirState); Serial.write(buttonState); Serial.write(AnalogNegs); Serial.write(Xpos); Serial.write(Ypos); // check if the pushbutton is pressed. // if it is, the buttonState is HIGH: if (dirState > 0 || buttonState > 0) { // turn LED on: digitalWrite(ledPin, HIGH); //analogWrite(A2, 20); } else { // turn LED off: digitalWrite(ledPin, LOW); Callib; // delay(3); } delay(2); }
Ik heb een synchronisatie-byte gebruikt om de seriële gegevens synchroniseren met het FreeBasic programma. Ik voegde ook een LED die zit in de centrale knop die wordt gebruikt als een 'Mode' knop om een andere laag van controle geven.
Het FreeBasic programma omgezet in de seriële data van de Arduino in toetsenbord toetsaanslagen, het draait bij het opstarten van windows als een achtergrondtaak.
Dankzij Ben Heck voor de inspiratie!!