Stap 4: Bereiden de Arduino-Code
Omvatten de bibliotheek die hieronder zijn toegevoegd. De bibliotheek van de servo is de standaard servo-bibliotheek die wordt geleverd met de Arduino schets software.
Constante variabelen definiëren
U moet het instellen van deze waarden afhankelijk van het protocol en remote codes die u gebruikt. Deze zijn van mijn hobby RAS. Bekijk de bijgevoegde IRLib bibliotheek voor verwijzingen op het IRLib-protocol.
#define MY_PROTOCOL NEC#define RIGHT_ARROW 0xFF02FD // PLAY main loop (power)#define LEFT_ARROW 0xFF22DD // SERVO POSITION 2#define POWER 0xFFA25D // SERVO POSITION 2 POWER#define PLAY 0xFFC23D // PLAY AUTO LOOP#define RETURN 0xFFB04F // LEAVE AUTOMATIC LOOP#define MUTE 0xFFE21D // OFF const int SFX_4 = 4; const int SFX_2 = 5;const int SFX_RST = 6;const int SFX_vol = 7;const int sensor1_pwr = A4;const int sensor2_pwr = A5;const int sensor1 = A0;const int sensor2 = A1;const int LEDON = 10;
Define variabelen wijzigen
int _SFX_4 = HIGH; int _SFX_2 = HIGH;int _SFX_RST = HIGH;int _SFX_vol = HIGH;int pos1;int pos2;int Read1NEW = 400;int Read1OLD = 400;int Read2NEW = 400;int Read2OLD = 400;int state = 0int n = 0;int m = 0;int w = 0;int prevMillis = 0;long previousMillis = 0;
Servo's definiëren:
Servo servo1; // Define servoServo servo2; // Define servo
Gebruik deze codesegment voor de IR ontvanger:
IRrecv My_Receiver(11); //Receive on pin 11IRdecode My_Decoder;
Setup:
void setup(){ // begin setupSerial.begin(9600); // Begin serial communicationMy_Receiver.No_Output(); // Turn off any unused IR LED output circuitMy_Receiver.enableIRIn(); // Starts the receiver for IR remote
Define Pin modi
pinMode(SFX_2,OUTPUT);digitalWrite(SFX_2,_SFX_2);pinMode(SFX_4,OUTPUT);digitalWrite(SFX_4,_SFX_4);pinMode(SFX_vol,OUTPUT);digitalWrite(SFX_vol,_SFX_vol);pinMode(SFX_RST,OUTPUT);digitalWrite(SFX_RST,_SFX_RST);pinMode(sensor1_pwr,OUTPUT);digitalWrite(sensor1_pwr,HIGH);pinMode(sensor1_pwr,OUTPUT);digitalWrite(sensor1_pwr,HIGH);pinMode(sensor2_pwr,OUTPUT);digitalWrite(sensor2_pwr,HIGH); pinMode(LEDON,OUTPUT);digitalWrite(LEDON,LOW);
Servo's toevoegen
servo1.attach(8);servo2.attach(9);
} // end setup
Hoofdlus:
Dit segment van code is toegelicht, dus ik niet het doel van de codes herhalen zal.
void loop(){ // begin main loop bailout1:{ servo1.detach(); // Detach Servo, prevents servo noise servo2.detach(); // Detach Servo, prevents servo noise int V1 = 0; // Variable for while loop below while (V1 == 0){ if (My_Receiver.GetResults(&My_Decoder)) { My_Decoder.decode(); if(My_Decoder.decode_type==MY_PROTOCOL) { switch(My_Decoder.value) { case MUTE:{ digitalWrite(LEDON,LOW); Serial.println("POWER OFF"); servo1.attach(8); // Attaches servo 1 servo2.attach(9); // Attaches servo 2 servo1.write(95); // low position is 95 deg servo2.write(100); // low position is 100 deg delay(500); goto bailout1; } case POWER:{ Serial.println("POWER ON"); digitalWrite(LEDON,HIGH); int V2 = 0; while (V2 == 0) { // Detach all servos so that vibrations sensors do not pick up any // servo vibration or whine. //---------------------------------------------------------------------- servo1.detach(); servo2.detach(); // Nested Loop Wich Sets Initial Conditions for SFX board // In this case, it is used for adjusting volume. // Go to https://learn.adafruit.com/adafruit-audio-fx-soun... // for a detailed tutorial on how the Adafruit FX board works. // The segment of code below is essentially turning the volume // down by a crude method. //---------------------------------------------------------------------- while(n < 1){ n++; Serial.print("Resetting FX Sound Board..."); digitalWrite(SFX_RST,LOW); delay(40); digitalWrite(SFX_RST,HIGH); Serial.println("Complete"); } while(m < 26){ m++; digitalWrite(SFX_vol,LOW); delay(40); digitalWrite(SFX_vol,HIGH); delay(40); if(m == 26){ Serial.println("Complete"); } if(m == 1){ Serial.print("Checking Volume Level..."); }} //---------------------------------------------------------------------- bailout2:{ // Used to escape from nested loops below //---------------------------------------------------------------------- // Remote Control Interface //---------------------------------------------------------------------- // For info on how to use the IR remote control with the Arduino // visit https://github.com/cyborg5/IRLib // There are manuals and examples. //---------------------------------------------------------------------- if (My_Receiver.GetResults(&My_Decoder)) { My_Decoder.decode(); if(My_Decoder.decode_type==MY_PROTOCOL) { switch(My_Decoder.value) { //---------------------------------------------------------------------- // The next two case statements are used to alternate servo positions // with the push of the button (right or left arrow on IR remote) //---------------------------------------------------------------------- case MUTE:{ digitalWrite(LEDON,LOW); Serial.println("POWER OFF"); servo1.attach(8); // Attaches servo 1 servo2.attach(9); // Attaches servo 2 servo1.write(95); // low position is 95 deg servo2.write(100); // low position is 100 deg delay(500); goto bailout1; // exit ON loop } case LEFT_ARROW:{ servo1.attach(8); // Attaches servo 1 servo2.attach(9); // Attaches servo 2Serial.println("MANUAL PLAY 1"); digitalWrite(SFX_4,LOW); // enables sound delay(400); // delay for sound digitalWrite(SFX_4,HIGH); // enables sound servo1.write(95); // High position is 125 deg servo2.write(75); // low position is 100 deg delay(2000); // delay for timing state = 1; // state for determining which sensor was last break; // exit loop } case RIGHT_ARROW: { servo1.attach(8); // Attaches servo 1 servo2.attach(9); // Attaches servo 2 Serial.println("MANUAL PLAY 2"); digitalWrite(SFX_4,LOW); // enables sound delay(400); // delay for sound digitalWrite(SFX_4,HIGH); // enables sound servo1.write(125); // low position is 95 deg servo2.write(100); // high position is 75 deg delay(2000); // delay for timing state = 0; // state for determining which sensor was last break; // exit loop } //********************************************************************** // This segment enables the automated play mode //********************************************************************** case PLAY: { Serial.println("Entering Automated Play Mode"); int q = 0; while(q==0) { //---------------------------------------------------------------------- // This segment of code is used to break out of the automated play mode //---------------------------------------------------------------------- if (My_Receiver.GetResults(&My_Decoder)) { My_Decoder.decode(); if(My_Decoder.decode_type==MY_PROTOCOL) { switch(My_Decoder.value) { case RETURN: { Serial.println("Leaving Automated Play Mode"); delay(10); servo1.attach(8); // Attaches servo 1 servo2.attach(9); // Attaches servo 2 servo1.write(95); // low position is 95 deg servo2.write(100); // low position is 100 deg delay(500); w=0; goto bailout2; // exits play loop }}} My_Receiver.resume(); } //---------------------------------------------------------------------- // Initially positions servo at beginning of automated play mode //---------------------------------------------------------------------- while(w < 1) { w++; // variable for loop servo1.attach(8); // Attaches servo 1 servo2.attach(9); // Attaches servo 2 delay(1); // delay for stability servo1.write(95); // positions servo High is 120 deg servo2.write(75); // positions servo low is 100 deg delay(20); // delay for timing servo1.detach(); // Detach Servo, prevents servo noise servo2.detach(); // Detach Servo, prevents servo noise //---------------------------------------------------------------------- //This segment Plays song to indicate you have entered automatic play mode //---------------------------------------------------------------------- digitalWrite(SFX_2,LOW); delay(400); digitalWrite(SFX_2,HIGH); } //---------------------------------------------------------------------- // Stores readings for sensor 1 & 2 in a variable //---------------------------------------------------------------------- Read1NEW = analogRead(sensor1); // stores sensor value delay(1); // delay for stability Read2NEW = analogRead(sensor2); // stores sensor value delay(1); // delay for stability //---------------------------------------------------------------------- // Changes servo positions when a sensor is disturbed //---------------------------------------------------------------------- //---------------------------------------------------------------------- // SERVO / SENSOR 1 CONDITIONAL STATEMENT //---------------------------------------------------------------------- if((Read1NEW > 750) && (state == 0)) { Serial.println("Disturbance Detected in Sensor 1"); //---------------------------------------------------------------------- // This segment Plays sound indicating ball has been disturbed //---------------------------------------------------------------------- digitalWrite(SFX_4,LOW); delay(360); digitalWrite(SFX_4,HIGH); //---------------------------------------------------------------------- Read1NEW = 0; // stores sensor reading for sensor 1 Read2NEW = 0; // stores sensor reading for sensor 2 digitalWrite(sensor1_pwr,LOW); // temp disables sensor digitalWrite(sensor2_pwr,LOW); // temp disables sensor servo1.attach(8); // Attaches servo 1 servo2.attach(9); // Attaches servo 2 delay(1500); servo1.write(95); // Up position is 125 servo2.write(75); // Down position is 100 servo1.detach(); // Detach Servo, prevents servo noise servo2.detach(); // Detach Servo, prevents servo noise delay(1000); // delay for timing state = 1; // state for determining which sensor was last } //---------------------------------------------------------------------- // SERVO / SENSOR 2 CONDITIONAL STATEMENT //---------------------------------------------------------------------- if((Read2NEW > 750) && (state == 1)) { Serial.println("Disturbance Detected in Sensor 2"); //---------------------------------------------------------------------- // This segment Plays sound indicating ball has been disturbed //---------------------------------------------------------------------- digitalWrite(SFX_4,LOW); delay(360); digitalWrite(SFX_4,HIGH); //---------------------------------------------------------------------- Read1NEW = 0; // stores sensor reading for sensor 1 Read2NEW = 0; // stores sensor reading for sensor 2 digitalWrite(sensor1_pwr,LOW); // temp disables sensor digitalWrite(sensor2_pwr,LOW); // temp disables sensor servo1.attach(8); // Attaches servo 1 servo2.attach(9); // Attaches servo 2 delay(1500); // delay for timing servo1.write(125); // low is 95 servo2.write(100); // high is 75 servo1.detach(); // Detach Servo, prevents servo noise servo2.detach(); // Detach Servo, prevents servo noise delay(1000); // delay for timing state = 0; // state for determining which sensor was last } else { digitalWrite(SFX_4,HIGH); // Keeps sound effect pin high (OFF) if ((analogRead(sensor2) < 350) && (analogRead(sensor1) < 350)) { digitalWrite(sensor1_pwr,HIGH); // enables sensor digitalWrite(sensor2_pwr,HIGH); // enables sensor }}}}}} My_Receiver.resume(); }}}}}} My_Receiver.resume(); }}}} //********************************************************************** //**********************************************************************