Stap 4: Programing
Download Makeblock bibliotheek en zet het onder Arduino bibliotheken. De broncode van de Arduino voor infrarood vechten tank is als volgt:
#include "MeOrion.h"#include <SoftwareSerial.h> #include "MeIR.h"
MeIR irsend; MeDCMotor MotorL(M1); MeDCMotor MotorR(M2); MeRGBLed ledBottom(PORT_3); Me7SegmentDisplay disp(PORT_4); MeInfraredReceiver infraredReceiverDecode(PORT_5); MeUSBHost joypad(PORT_6); // Corresponding buffer data of keys on the Joystick // Default:128-127-128-127-15-0-0-128 // L1:128-127-128-127-15-1-0-128 // R1:128-127-128-127-15-2-0-128 // L2:128-127-128-127-15-4-0-128 // R2:128-127-128-127-15-8-0-128 // Triangle key:128-127-128-127-31-0-0-128 (0001 1111) // Square key:128-127-128-127-143-0-0-128 (1000 1111) // X key:128-127-128-127-79-0-0-128 (0100 1111) // Circle key:128-127-128-127-47-0-0-128 (0010 1111) // Up:128-127-128-127-0-0-0-128 (0000 0000) // Down:128-127-128-127-4-0-0-128 (0000 0100) // Left:128-127-128-127-6-0-0-128 (0000 0110) // Right:128-127-128-127-2-0-0-128 (0000 0010) // Select:128-127-128-127-15-16-0-128 // Start:128-127-128-127-15-32-0-128 // Joystick:Right X-Right Y-Left X-Left Y-15-0-0-128 MeRGBLed ledTop(PORT_7, SLOT2, 15);
int moveSpeed = 200; uint8_t ReceiverCode; int bulletNumber = 16; int khz = 38; // 38kHz carrier frequency for the NEC protocol float lifeValue = 8; boolean gotShootIndex = 1;
void setup() { infraredReceiverDecode.begin(); Serial.println("InfraredReceiverDecode Start!"); Serial.begin(9600); joypad.init(USB1_0);
disp.display(bulletNumber); displayLifeValue();
int delayTime = 360; for (uint8_t t = 0; t < 16; t++) // blink led { ledBottom.setColor(200,0,0); // led on ledBottom.show(); delay(delayTime); ledBottom.setColor(0,0,0); // led off ledBottom.show(); delay(delayTime); delayTime -= 20; } }
void shoot() { // shoot, bullet number -1 bulletNumber -= 1;
if(bulletNumber>=0) { unsigned int irSignal[] = {9000, 4500, 560, 560, 560, 560, 560, 1690, 560, 560, 560, 560, 560, 560, 560, 560, 560, 560, 560, 1690, 560, 1690, 560, 560, 560, 1690, 560, 1690, 560, 1690, 560, 1690, 560, 1690, 560, 560, 560, 560, 560, 560, 560, 1690, 560, 560, 560, 560, 560, 560, 560, 560, 560, 1690, 560, 1690, 560, 1690, 560, 560, 560, 1690, 560, 1690, 560, 1690, 560, 1690, 560, 39416, 9000, 2210, 560}; //AnalysIR Batch Export (IRremote) - RAW irsend.sendRaw(irSignal, sizeof(irSignal) / sizeof(irSignal[0]), khz); // send ir signal
ledBottom.setColor(0,0,200); ledBottom.show(); Backward(); buzzerOn(); delay(100); Stop();
Forward(); delay(80); ledBottom.setColor(0,0,0); ledBottom.show(); Stop(); buzzerOff(); } else { bulletNumber=0; } disp.display(bulletNumber); }
void gotShoot() { Serial.println(ReceiverCode); if(ReceiverCode == 0x08) { Serial.println("got shoot"); lifeValue -= 0.5; if(lifeValue<0) { lifeValue = 0; } ledTop.setColorAt(lifeValue, 0, 0, 0); ledTop.show();
if(gotShootIndex){ ledBottom.setColor(200,0,0); // backward and flash red led ledBottom.show(); Backward(); delay(100); ledBottom.setColor(0,0,0); ledBottom.show(); Stop(); } gotShootIndex = !gotShootIndex; } }
void displayLifeValue() { lifeValue = 8; for(int8_t t=0; t<lifeValue; t++) { // light led ledTop.setColorAt(t, 200,0,0); ledTop.show(); delay(150); } }
void loop() {
if(infraredReceiverDecode.available()) { ReceiverCode = infraredReceiverDecode.read(); gotShoot(); }
if(!joypad.device_online) { joypad.probeDevice(); delay(1000); } else { int len = joypad.host_recv(); parseJoystick(joypad.RECV_BUFFER); delay(5); } }
void parseJoystick(unsigned char *buf) //Analytic function, print 8 bytes from USB Host { // print buffer data to debug // int i = 0; // for(i = 0; i < 7; i++) // { // Serial.print(buf[i]); // Serial.print('-'); // } // Serial.println(buf[7]); // delay(10);
switch (buf[5]) { case 1: // L1 reset life value displayLifeValue(); break; case 2: // R1 reset bullet number bulletNumber = 16; disp.display(bulletNumber); break; case 4: // L2 break; case 8: // R2 break; default: break; }
if ((128 != buf[0]) || (127 != buf[1]) || (128 != buf[2]) || (127 != buf[3])) { // buf[0]-buf[3] corresponding to the left and right joystick
} else { switch (buf[4]) { case 79: // X Stop(); break; case 0: // up Forward(); break; case 4: // down Backward(); break; case 6: // left TurnLeft(); break; case 2: // right TurnRight(); break; case 143: // square key
break; case 31: // triangle key shoot(); break; case 47: // circle key break; default: Stop(); break; } } }
void Forward() { MotorL.run(-moveSpeed); MotorR.run(moveSpeed); } void Backward() { MotorL.run(moveSpeed); MotorR.run(-moveSpeed); } void TurnLeft() { MotorL.run(moveSpeed); MotorR.run(moveSpeed); } void TurnRight() { MotorL.run(-moveSpeed); MotorR.run(-moveSpeed); } void Stop() { MotorL.run(0); MotorR.run(0); }