Étape 28 : Programmer le Arduinos
Télécharger ce code à l’Arduino contrôlant l’écran tactile :
Caméra Rig contrôleur écran Touch #include < Wire.h > #include < Adafruit_GFX.h > / / Core bibliothèque graphique #include < Adafruit_TFTLCD.h > / / spécifiques au matériel bibliothèque #include < TouchScreen.h > #if defined(__SAM3X8E__) #undef __FlashStringHelper::F(string_literal) #define F(string_literal) string_literal #endif #ifndef USE_ADAFRUIT_SHIELD_PINOUT #error "cette esquisse est destinée pour être utilisé avec le bouclier de LCD TFT. Assurez-vous que USE_ADAFRUIT_SHIELD_PINOUT est #defined dans le fichier de bibliothèque de Adafruit_TFTLCD.h."#endif / / Voici les broches pour le bouclier ! #define YP A1 / / doit être une broche analogique, utilisez la « Une » notation ! #define XM A2 / / doit être une broche analogique, utilisez la « Une » notation ! #define YM 7 / / peut être une broche numérique #define XP 6 / / peut être un __SAM3X8E__ de la broche numérique #ifdef #define TS_MINX 125 #define TS_MINY 170 #define TS_MAXX 880 #define TS_MAXY 940 #else #define TS_MINX 150 #define TS_MINY 120 #define TS_MAXX 920 #define TS_MAXY 940 #endif / / pour une meilleure précision de la pression, nous avons besoin de connaître la résistance / / entre X + et X - utilisation tout multimètre pour le lire / / pour celui que nous utilisons , ses 300 ohms à travers le X plaque tactile ts = écran tactile (XP, YP, XM, YM, 300) ; #define LCD_CS A3 #define LCD_CD A2 #define LCD_WR A1 #define LCD_RD A0 / / assigner des noms lisibles par l’homme à des valeurs communes de couleur 16-bit : #define noir 0 x 0000 #define BLUE 0x001F #define rouge 0xF800 #define vert 0x07E0 #define CYAN 0x07FF #define MAGENTA 0xF81F #define jaune 0xFFE0 #define blanc 0xFFFF #define écouteur 0x65FF Adafruit_TFTLCD tft ; int oldcolor, currentcolor ; bool pan = 1 ; int panPosition1 = 20 ; int panPosition2 = 20 ; bool inclinaison = 0 ; int tiltPosition1 = 20 ; int tiltPosition2 = 20 ; bool lecteur = 0 ; int drivePosition1 = 20 ; int drivePosition2 = 20 ; bool temps = 0 ; int timePosition1 = 20 ; int timePosition2 = 20 ; int startSend = 0 ; int x ; void setup(void) {Wire.begin(); / / join bus i2c (adresse facultatif pour maître) Serial.begin(9600) ; tft.reset() ; uint16_t identificateur = tft.readID() ; if(identifier == 0x9325) {Serial.println (F ("driver trouvé ILI9325 LCD"));} d’autre if(identifier == 0x9328) {Serial.println (F ("driver trouvé ILI9328 LCD"));} d’autre if(identifier == 0x7575) {Serial.println (F ("driver trouvé HX8347G LCD"));} else {Serial.print (F ("puce pilote LCD inconnu:")) ; Serial.println (identificateur, HEX) ; retour ; } tft.begin(identifier) ; tft.setRotation(1) ; tft.fillScreen(BLACK) ; drawMenu() ; tft.drawRect (0, 0, 58, 40, blanc) ; réglage initial curseur États tft.drawRect (19, 272, 89, 6, blanc) ; tft.fillRect (20, 90, 270, 4, jaune) ; tft.drawRect (panPosition1, 71, 17, 42, blanc) ; tft.fillRect ((panPosition1 + 1), 72, 15, 40, MAGENTA) ; tft.drawRect (19, 272, 169, 6, blanc) ; tft.fillRect (20, 170, 270, 4, jaune) ; tft.drawRect (panPosition2, 151, 17, 42, blanc) ; tft.fillRect ((panPosition2 + 1), 152, 15, 40, MAGENTA) ; currentColor = rouge ; pinMode (13, sortie) ; } #define MINPRESSURE 10 #define MAXPRESSURE 1000 void loop() {digitalWrite (13, HIGH) ; Point p = ts.getPoint() ; digitalWrite (13, faible) ; logé dans la pins, vous aurez besoin de fixer l’orientation de l’écran tactile broches pinMode (XM, sortie) ; pinMode (YP, sortie) ; Nous avons quelques pression minimale que nous considérons comme « valables » / / pression de 0 ne signifie aucune pression ! Si (p.z > MINPRESSURE & & p.z < MAXPRESSURE) {/ / Echelle de 0 -> 1023 à p.x tft.width = carte (p.x, TS_MINX, TS_MAXX, tft.width(), 0); p.y = carte (p.y, TS_MINY, TS_MAXY, tft.height(), 0); / / *** / / TOP MENU sélection CODE!!! / / *** si (p.x > 300) {oldcolor = currentcolor ; if (p.y < 58) {currentcolor = rouge ; tft.drawRect (0, 0, 58, 40, blanc); pan = 1; inclinaison = 0; lecteur = 0; temps = 0; drawSliders();} ElseIf (p.y < 140) {currentcolor = bleu clair ; tft.drawRect (58 0, 82, 40, BLANC) ; Pan = 0 ; inclinaison = 1 ; voiture = 0 ; temps = 0 ; drawSliders() ; } ElseIf (p.y < 180) {currentcolor = bleu ; tft.drawRect (140, 0, 96, 40, blanc); pan = 0; inclinaison = 0; disque = 1; temps = 0; drawSliders();} ElseIf (p.y < 240) {currentcolor = vert ; tft.drawRect (236 0, 82, 40, blanc); pan = 0; incliner = 0; lecteur = 0; temps = 1; drawSliders();}} //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! AI-JE BESOIN DE ÇA DU TOUT??? IL PEUT IL SUFFIT DE LIRE MENU DESSIN??? Si (oldcolor! = currentcolor) {si (oldcolor == rouge) drawMenu() ; if (oldcolor == écouteur) drawMenu() ; if (oldcolor == vert) drawMenu() ; if (oldcolor == bleu) drawMenu();} / / *** / / START sélection CODE!!! Si (p.x < 40) {si (p.y > 180) {tft.drawRect (220, 200, 96, 40, blanc); startSend = 1;}} / / *** //SLIDER CODE!!! //Top curseur si (p.x > 180 & & p.x < 260) {si (p.y < 350) {si (p.y > = 20) {/ / ou alors... pour le premier noir carré... //need bas remap et remappage des albums!!! p.y = carte (p.y, 0, 0, 240, 300) ; tft.fillRect (10, 71, (p.y - 10), 50, noir) ; } tft.drawRect ((p.y-1), 71, 17, 42, blanc) ; tft.fillRect (p.y, 72, 15, 40, MAGENTA) ; tft.fillRect ((p.y + 16), 71, (282 - p.y), 50, noir) ; tft.drawRect ((p.y + 15), 89, (272 - p.y), 6, blanc) ; tft.fillRect ((p.y + 16), 90, (270 - p.y), 4, jaune) ; tft.drawRect (19, 89, (p.y - 19), blanc, 6) ; tft.fillRect (20, 90, (p.y - 20), 4, jaune) ; Si (pan == 1) {panPosition1 = p.y; / / Serial.println(panPosition1);} else if(tilt == 1) {tiltPosition1 = p.y;} else if(drive == 1) {drivePosition1 = p.y;} d’autre if(time == 1) {timePosition1 = p.y;}} } //BOTTOM curseur si (p.x > 80 & & p.x < 160) {si (p.y < 350) {si (p.y > = 20) {/ / ou alors... pour le premier noir carré... //need bas remap et remappage des albums!!! p.y = carte (p.y, 0, 0, 240, 300); tft.fillRect (10, 151, (p.y - 10), 50, BLACK);} tft.drawRect ((p.y-1), 151, 17, 42, blanc); tft.fillRect (p.y, 152, 15, 40, MAGENTA); tft.fillRect ((p.y + 16) 151, (282 - p.y), 50, noir) ; tft.drawRect ((p.y + 15), 169, (272 - p.y), 6, blanc) ; tft.fillRect ((p.y + 16), 170, (270 - p.y), 4, jaune) ; tft.drawRect (19, 169, (p.y - 19), blanc, 6) ; tft.fillRect (20, 170, (p.y - 20), 4, jaune) ; Si (pan == 1) {panPosition2 = p.y ; Serial.println(panPosition2) ; } else if(tilt == 1) {tiltPosition2 = p.y;} else if(drive == 1) {drivePosition2 = p.y;} else if(time == 1) {timePosition2 = p.y;}} } / / *** //TRANSMITS à autre ARDUINO / / *** if(startSend == 1) {transmitValues() ; startSend = 0; delay(2000) ; drawMenu() ; if (pan == 1) tft.drawRect (0, 0, 58, 40, blanc); if (incliner == 1) tft.drawRect (0, 82, 58, 40, blanc); if (lecteur == 1) tft.drawRect (140, 0, 96, 40, blanc); if (temps == 1) tft.drawRect (236 0, 82, 40, blanc);}} } void transmitValues() {Wire.beginTransmission(2); / / transmettre à un périphérique #2 x = panPosition1 ; Wire.Write("a") ; envoie cinq octets Wire.write(x) ; Wire.endTransmission() ; arrêter la transmission delay(100) ; Wire.beginTransmission(2) ; transmettre à un périphérique #2 x = panPosition2 ; Wire.Write("b") ; envoie cinq octets Wire.write(x) ; Wire.endTransmission() ; arrêter la transmission delay(100) ; Wire.beginTransmission(2) ; transmettre à un périphérique #2 x = tiltPosition1 ; Wire.Write("c") ; Wire.Write(x) ; Wire.endTransmission() ; arrêter la transmission delay(100) ; Wire.beginTransmission(2) ; transmettre à un périphérique #2 x = tiltPosition2 ; Wire.Write("d") ; Wire.Write(x) ; Wire.endTransmission() ; arrêter la transmission delay(100) ; Wire.beginTransmission(2) ; transmettre à un périphérique #2 x = drivePosition1 ; Wire.Write("e") ; envoie cinq octets Wire.write(x) ; Wire.endTransmission() ; arrêter la transmission delay(100) ; Wire.beginTransmission(2) ; transmettre à un périphérique #2 x = drivePosition2 ; Wire.Write("f") ; envoie cinq octets Wire.write(x) ; Wire.endTransmission() ; arrêter la transmission delay(100) ; Wire.beginTransmission(2) ; transmettre à un périphérique #2 x = timePosition1 ; Wire.Write("g") ; Wire.Write(x) ; Wire.endTransmission() ; arrêter la transmission delay(100) ; Wire.beginTransmission(2) ; transmettre à un périphérique #2 x = timePosition2 ; Wire.Write("h") ; Wire.Write(x) ; Wire.endTransmission() ; arrêter la transmission delay(100) ; Wire.beginTransmission(2) ; transmettre à un périphérique #2 x = startSend ; Wire.Write("i") ; Wire.Write(x) ; Wire.endTransmission() ; arrêt de transmission} void drawSliders() {si (oldcolor ! = currentcolor) {tft.fillRect (10, 60, 300, 200, BLACK);} if(pan == 1) {tft.drawRect (19, 272, 89, 6, blanc); tft.fillRect (20, 90, 270, 4, jaune); tft.drawRect (panPosition1, 71, 17, 42, blanc); tft.fillRect ((panPosition1 + 1), 72, 15, 40, MAGENTA); tft.drawRect (19, 272, 169, 6, blanc); tft.fillRect (20, 170, 270, 4, jaune); tft.drawRect (panPosition2, 151, 17, 42, blanc); tft.fillRect ((panPosition2 + 1), 152, 15, 40, MAGENTA);} else if(tilt == 1) {tft.drawRect (19 89, 272, 6, BLANC) ; tft.fillRect (20, 90, 270, 4, jaune) ; tft.drawRect (tiltPosition1, 71, 17, 42, blanc) ; tft.fillRect ((tiltPosition1 + 1), 72, 15, 40, MAGENTA) ; tft.drawRect (19, 272, 169, 6, blanc) ; tft.fillRect (20, 170, 270, 4, jaune) ; tft.drawRect (tiltPosition2, 151, 17, 42, blanc) ; tft.fillRect ((tiltPosition2 + 1), 152, 15, 40, MAGENTA) ; } else if(drive == 1) {tft.drawRect (19, 272, 89, 6, blanc); tft.fillRect (20, 90, 270, 4, jaune); tft.drawRect (drivePosition1, 71, 17, 42, blanc); tft.fillRect ((drivePosition1 + 1), 72, 15, 40, MAGENTA); tft.drawRect (19, 272, 169, 6, blanc); tft.fillRect (20, 170, 270, 4, jaune); tft.drawRect (drivePosition2, 151, 17, 42, blanc); tft.fillRect ((drivePosition2 + 1), 152, 15, 40, MAGENTA);} d’autre if(time == 1) {tft.drawRect (19, 89 272, 6, BLANC) ; tft.fillRect (20, 90, 270, 4, jaune) ; tft.drawRect (timePosition1, 71, 17, 42, blanc) ; tft.fillRect ((timePosition1 + 1), 72, 15, 40, MAGENTA) ; tft.drawRect (19, 272, 169, 6, blanc) ; tft.fillRect (20, 170, 270, 4, jaune) ; tft.drawRect (timePosition2, 151, 17, 42, blanc) ; tft.fillRect ((timePosition2 + 1), 152, 15, 40, MAGENTA) ; drawMenu() Sub}} {tft.fillRect (236 0, 82, 40, vert); tft.fillRect (140, 0, 96, 40, bleu); tft.fillRect (0, 82, 58, 40, bleu clair); tft.fillRect (0, 0, 58, 40, rouge); tft.drawChar (2, 10, 'P', blanc, rouge, 3); tft.drawChar (20, 10, 'A', blanc, rouge, 3); tft.drawChar (38, 10, ' n ', blanc, rouge, 3); tft.drawChar (64, 10, ' t ', blanc, bleu clair, 3); tft.drawChar (82, 10, 'I', blanc, bleu clair, 3); tft.drawChar (100 10, 'L', BLANC, BLEU CLAIR, 3) ; tft.drawChar (118, 10, ' t ', blanc, bleu clair, 3) ; tft.drawChar (144, 10, serait ", blanc, bleu, 3) ; tft.drawChar (162, 10, « R », blanc, bleu, 3) ; tft.drawChar (180, 10, « I », blanc, bleu, 3) ; tft.drawChar (198, 10, « V », blanc, bleu, 3) ; tft.drawChar (216, 10, « E », blanc, bleu, 3) ; tft.drawChar (242, 10, ' t ', blanc, vert, 3) ; tft.drawChar (260, 10, « I », blanc, vert, 3) ; tft.drawChar (278, 10, suis ', blanc, vert, 3) ; tft.drawChar (296, 10, « E », blanc, vert, 3) ; tft.fillRect (220, 200, 96, 40, bleu) ; tft.drawChar (224, 210, les de ', blanc, bleu, 3) ; tft.drawChar (242, 210, ' t ', blanc, bleu, 3) ; tft.drawChar (260, 210, « A », blanc, bleu, 3) ; tft.drawChar (278, 210, « R », blanc, bleu, 3) ; tft.drawChar (296, 210, ' t ', blanc, bleu, 3) ; } < br >
Télécharger ce code à l’Arduino qui allez commander le moteur :
Caméra contrôleur de moteur Rig #include < Servo.h > #define ENC_A 14 #define ENC_B 15 #define ENC_PORT PINC Servo panServo ; Servo tiltServo ; distance de l’int = 0 ; StateChange {bool = 0 ; int motorPin = 3 ; LED connectée à la broche numérique 9 flotteur driveX = 0 ; float tiltX = 0 ; flotteur panX = 0 ; flotteur timeX = 0 ; flotteur driveZ = 0 ; float tiltZ = 0 ; flotteur panZ = 0 ; flotteur picZ = 0 ; int distanceToGo = 0 ; pour le programme int progStart = 0 ; test //int progStart = 1 ; motorSpeed int ; float totalTilt ; float totalPan ; float totalDrive ; float panIncrement ; float tiltIncrement ; float driveIncrement ; float picsPerPanIncrement = 1 ; float picsPerTiltIncrement = 1 ; float picsPerDriveIncrement = 1 ; int panSteps = 0 ; int tiltSteps = 0 ; int driveSteps = 0 ; char dataRx [20] ; #include < Wire.h > void setup() {Wire.begin(2); / / join bus i2c avec adresse #2 Wire.onReceive(receiveEvent); / / enregistrer événement Serial.begin(9600); / / start de série pour sortie panServo.attach(4) ; tiltServo.attach(5) ; tiltServo.writeMicroseconds(1100) ; panServo.writeMicroseconds(1500) ; delay(10000) ; pinMode (sortie 8); pinMode (entrée, ENC_A); digitalWrite (ENC_A, élevé); pinMode (entrée, ENC_B); digitalWrite (ENC_B, élevé);} void loop() {if(progStart == 1) {tiltX = carte (tiltX 0, 255, 750-1450) ; tiltZ = carte (tiltZ, 0, 255, 750-1450) ; panX = carte (panX, 2200, 600, 255, 0) ; panZ = carte (panZ, 2200, 600, 255, 0) ; Serial.println(panX) ; Serial.println(panZ) ; if(timeX < 5) {motorSpeed = 150 ; driveX = (driveX, 0, 255, 0, 180); timeX = 0;} else {motorSpeed = 75 ; driveX = (driveX, 255, 0, 0, 360);} totalPan = (panX - panZ) ; totalPan = abs(totalPan) ; totalTilt = tiltX - tiltZ ; totalTilt = abs(totalTilt) ; totalDrive = driveX ; Serial.println(totalPan) ; //figure comment loin d’avancer avant de prendre des photos / / alternativement, comment beaucoup de photos à prendre avant d’avancer / / *** / /!!!!!!!!!!!!!!!!!!!! CEUX-CI DOIVENT ÊTRE DES FLOTTEURS DE PUTAIN ET MERDE ! //! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! panIncrement = totalPan / picZ ; if(totalPan < picZ) {picsPerPanIncrement = 1 / panIncrement ; panIncrement = 1;} tiltIncrement = totalTilt / picZ ; if(totalTilt < picZ) {picsPerTiltIncrement = 1 / tiltIncrement ; tiltIncrement = 1;} driveIncrement = totalDrive / picZ ; if(totalDrive < picZ) {picsPerDriveIncrement = 1 / driveIncrement ; driveIncrement = 1;} / / *** //move servos dans les positions initiales / / *** tiltServo.writeMicroseconds(tiltX) ; panServo.writeMicroseconds(panX) ; Delay(10000) ; //Take quelques photos et déplacer des choses / / *** pour (int takingPictures = 0; takingPictures < picZ ; takingPictures ++) {si (panSteps > = picsPerPanIncrement) {if(panX > panZ) {panServo.writeMicroseconds (panX - panIncrement); panX = panX - panIncrement;} d’autre if(panX < panZ) {panServo.writeMicroseconds (panX + panIncrement); panX = panX + panIncrement;} else {panServo.writeMicroseconds(panX);} panSteps = 0 ; } Si (tiltSteps > = picsPerTiltIncrement) {if(tiltX > tiltZ) {tiltServo.writeMicroseconds (tiltX - tiltIncrement); tiltX = tiltX - tiltIncrement;} d’autre if(tiltX < tiltZ) {tiltServo.writeMicroseconds (tiltX + tiltIncrement); tiltX = tiltX + tiltIncrement;} else {tiltServo.writeMicroseconds(tiltX);} tiltSteps = 0 ; } Si (driveSteps > = picsPerDriveIncrement) {analogWrite (motorPin, motorSpeed); while(distance < distanceToGo) {callEncoder();} analogWrite (motorPin, 0); driveSteps = 0;} distanceToGo = distanceToGo + driveIncrement ; Delay(timeX * 500) ; Serial.println ("SHOOT!!!"); prendre photo digitalWrite (8, HIGH) ; Connectez la caméra déclencheur delay(500) ; Attendez une seconde digitalWrite (8, faible) ; Débrancher la caméra déclencheur delay(500) ; panSteps = panSteps + 1 ; tiltSteps = tiltSteps + 1 ; driveSteps = driveSteps + 1 ; progStart = 0 ; }}} / / fonction qui s’exécute chaque fois que les données sont demandées par le maître / / cette fonction est enregistrée comme un événement, voir setup() void receiveEvent (int titulaire) {int index = 0; while (1 < Wire.available()) / / effectuer une boucle sur tous sauf le dernier {char c = Wire.read(); / / recevoir des octets pour un caractère / / Serial.print(c); / / imprimer le caractère dataRx [index ++] = c; dataRx [index] = '\0';} int x = Wire.read(); / / recevoir octet sous forme d’entier / / Serial.println(x); / / print l’entier si (* dataRx == 'a') {panX = x ; Serial.Print ("OMG l’analyse PANX:") ; Serial.println(panX) ; } ElseIf (* dataRx == « b ») {panZ = x ; Serial.Print ("OMG l’analyse PANZ:") ; Serial.println(panZ) ; } ElseIf (* dataRx == « c ») {tiltX = x ; Serial.Print ("OMG l’analyse TILTX:") ; Serial.println(tiltX) ; } ElseIf (* dataRx == a ') {tiltZ = x ; Serial.Print ("OMG l’analyse TILTZ:") ; Serial.println(tiltZ) ; } ElseIf (* dataRx == « e ») {driveX = x ; Serial.Print ("OMG l’analyse DRIVEX:") ; Serial.println(driveX) ; } ElseIf (* dataRx == « f ») {driveZ = x ; Serial.Print ("OMG l’analyse DRIVEZ:") ; Serial.println(driveZ) ; } ElseIf (* dataRx == « g ») {timeX = x ; Serial.Print ("OMG l’analyse TIMEX:") ; Serial.println(timeX) ; } ElseIf (* dataRx == « h ») {picZ = x ; Serial.Print ("OMG l’analyse PICZ:") ; Serial.println(picZ) ; } ElseIf (* dataRx == « i ») {progStart = x ; Serial.Print ("démarrer l’analyse de OMG:") ; Serial.println(progStart) ; }} / * retourne change dans l’état de l’encodeur (-1.0.1) * / int8_t read_encoder() {public static int8_t enc_states [] = {0, - 1,1,0,1,0,0, -1, - 1,0,0,1,0,1,-1,0}; public static uint8_t old_AB = 0; / ** / old_AB << = 2; //remember précédent état old_AB | = (ENC_PORT & 0 x 03); //add état actuel retour (enc_states [(old_AB & 0x0f)]);} void callEncoder() {/ / analogWrite (motorPin, 0); / / delay(1000); / / / / //30Dst @ 150Sp / / //60Dst @ 75Sp / / //60Dst @ 50Sp / / / / while (distance < 240) {/ / analogWrite (motorPin 75) ; } public static uint8_t compteur = 0 ; Cette variable sera changée par l’encodeur d’entrée int8_t tmpdata ; / ** / tmpdata = read_encoder() ; Si (tmpdata) {/ / Serial.print ("valeur du compteur:") ; Serial.println (comptoir, DEC) ; Counter += tmpdata ; if(Counter == 255) {stateChange = 1;} if(counter == 0) {if(stateChange == 1) {stateChange = 0; distance += 1 ; Serial.Print ("incréments:") ; Serial.println (distance, DEC) ; }}}} < br >