Stap 9: Programma Arduino
Programma
Tijd voor onze Arduino program, hebt u het verbinden met een USB-poort van uw pc, dan openen de EDI en uploaden van de schets hieronder.
Over deze code die ik heb gemaakt kan ik zeggen:
- het maakt gebruik van Millis en geen vertragingen zijn werk te doen
- Het heeft 3 modi:
- "Scannen": het is wanneer de machine is zoals het scannen van het milieu langzaam haar hoofd links en rechts.
- "AfraidSx": het is wanneer een geluid wordt ontdekt aan de linkerkant. De machine draait naar links, en beweegt willekeurig een it is hoofd uitzoeken als er iets om te schieten is op.
- "AfraidDx": hetzelfde als hierboven, maar aan de rechterkant.
Geluid detectie zal leiden tot een van de bovenstaande modi
#include <br>
Servo servo;
int center = 115; int limitsx = 85; int limitdx = 145; int Behaviour = 1; //setta behaviuour iniziale int pos = 85; int Direction=1; byte endingAfraid = 0;
//knock const int knockSensor = 5; // the piezo is connected to analog pin 0 //const int threshold = 100; // threshold value to decide when the detected sound is a knock or not int sensorReading = 0; unsigned long millisAtKnockDx = 0;
//knock sx const int knockSensor2 = 3; // the piezo is connected to analog pin 0 //const int threshold = 100; // threshold value to decide when the detected sound is a knock or not int sensorReading2 = 0; unsigned long millisAtKnockSx = 0;
//verboselog const int VelocityCiclo1 = 1000; int CountCiclo1 = 0; unsigned long previousMillis1;
//afraiddx const int StartVelocityCiclo2 = 100; const int EndVelocityCiclo2 = 500; int CountCiclo2 = 0; unsigned long previousMillis2; byte servoSweep2 = 0;
//afraidsx const int StartVelocityCiclo4 = 100; const int EndVelocityCiclo4 = 500; int CountCiclo4 = 0; unsigned long previousMillis4; byte servoSweep4 = 0;
//ledloop const int VelocityCiclo5 = 200; int CountCiclo5 = 0; unsigned long previousMillis5; byte servoSweep5 = 0;
//scan const int VelocityCiclo3 = 130; int CountCiclo3 = 0; unsigned long previousMillis3; int pos3 = 0;
//DetectnOise int KnockDetected = 0; int PreviousKnockDetected = 0; const int VelocityCiclo6 = 500; //lentezza del ciclo di detect rumore int CountCiclo6 = 0; unsigned long previousMillis6; int NoiseTresh = 2; //soglia. abbassare per rendere meno sensibile al casino. se ho almeno 3 knock in 1000 msec allora non è un tirgeer.... int NoiseDetected = 0;
//DDelayUscita da noise const int VelocityCiclo7 = 1000; //lentezza dolp la quale tornare alla normalità... int CountCiclo7 = 0; unsigned long previousMillis7;
void setup(){ servo.attach(6); Serial.begin(9600); // initialize the serial communications 4 debug //pinMode(ControlSwitch, INPUT); // //pinMode(Led5, OUTPUT); pos3 = limitsx;
//IntLed---------------------- //led pinMode(12, OUTPUT); pinMode(11, OUTPUT); pinMode(10, OUTPUT); pinMode(9, OUTPUT); pinMode(8, OUTPUT); pinMode(7, OUTPUT); //IntLed---------------------- }
void loop(){
switch (Behaviour){ case 1: Scan(); //AfraidFr(); break;
case 2: if ((millis() <= millisAtKnockDx)) { // cicli dopo i quali tornare in scan AfraidDx(); } else { Direction=0; Behaviour = 1; endingAfraid = 1; } break;
case 3: if ((millis() <= millisAtKnockSx)) { // cicli dopo i quali tornare in scan AfraidSx(); } else { Direction=1; Behaviour = 1; endingAfraid = 1; } break; }
KnockTriggerDx(); KnockTriggerSx(); VerboseLog(); DetectNoise(); digitalWrite(12, HIGH); digitalWrite(11, HIGH); digitalWrite(10, HIGH); digitalWrite(9, HIGH); digitalWrite(8, HIGH); digitalWrite(7, HIGH); }
void KnockTriggerDx() { // read the sensor and store it in the variable sensorReading: sensorReading = digitalRead(knockSensor); // if the sensor reading is greater than the threshold: if (sensorReading == 1) { // toggle the status of the ledPin: Serial.println("Knock DX !!!!!!!!!!!"); KnockDetected++; // setto per riconoscere parlato. millisAtKnockDx = millis() + random(3000,10000); // azzero counter afraid dx if (NoiseDetected == 0){ Behaviour = 2; //vado in afraid dx } delay(200);
} }
VOID KnockTriggerSx() {/ / Lees de sensor en sla het in de variabele sensorReading: sensorReading2 = digitalRead(knockSensor2); / / als de sensorgegevens groter dan de drempel is: als (sensorReading2 == 1) {/ / de status van de ledPin in-/ uitschakelen: Serial.println ("Knock SX!!!"); KnockDetected ++; Setto per riconoscere parlato. millisAtKnockSx = millis() + random(3000,10000); azzero counter bang dx als (NoiseDetected == 0) {gedrag = 3; //vado in bang sx void KnockTriggerSx() { // read the sensor and store it in the variable sensorReading: sensorReading2 = digitalRead(knockSensor2); // if the sensor reading is greater than the threshold: if (sensorReading2 == 1) { // toggle the status of the ledPin: Serial.println("Knock SX !!!!!!!!!!!"); KnockDetected++; // setto per riconoscere parlato. millisAtKnockSx = millis() + random(3000,10000); // azzero counter afraid dx if (NoiseDetected == 0){ Behaviour = 3; //vado in afraid sx } delay(200); } } delay(200); void AfraidDx(){ if (millis() >= previousMillis2){ CountCiclo2++; previousMillis2 = previousMillis2+random(StartVelocityCiclo2,EndVelocityCiclo2); // each case gets 100ms servoSweep2 = random(1,4); //seleziona caso random tra 1 e 3 /*if (servoSweep == 7){ //cicla tutti i casi servoSweep = 1; }*/ } switch (servoSweep2){ case 1: pos = limitdx-3; servo.write(pos); break; case 2: pos = limitdx-6; servo.write(pos); break; case 3: pos = limitdx; servo.write(pos); break; } // end of sweep }
void AfraidSx(){ if (millis() >= previousMillis4){ CountCiclo4++; previousMillis4 = previousMillis4+random(StartVelocityCiclo4,EndVelocityCiclo4); // each case gets 100ms servoSweep4 = random(1,4); //seleziona caso random tra 1 e 3 /*if (servoSweep == 7){ //cicla tutti i casi servoSweep = 1; }*/ } switch (servoSweep4){ case 1: pos = limitsx+3; servo.write(pos); break; case 2: pos = limitsx+6; servo.write(pos); break; case 3: pos = limitsx; servo.write(pos); break; } // end of sweep }
void LedLoop(){
}
void Scan(){
if (millis() >= previousMillis3){ if (endingAfraid == 1) { //se to uscendao da afraid resetto la pos per fluidità endingAfraid = 0; if (Direction == 0) { previousMillis3 = previousMillis2; pos3 = limitdx - 6 ; } else{ previousMillis3 = previousMillis4; pos3 = limitsx + 6 ; } } CountCiclo3++; //debug// Serial.println(pos3); previousMillis3 = previousMillis3+VelocityCiclo3 ;
Als (millis() > = previousMillis3) {als (endingAfraid == 1) {//se te uscendao da bang resetto la pos per fluidità endingAfraid = 0; als (richting == 0) {previousMillis3 = previousMillis2; pos3 = limitdx - 6; if (Direction % 2) { //se dispari (da basso a alto) (da limit sx a limit dx) if(pos3 < limitdx) { pos3++; servo.write(pos3); } else { Direction++; // se ho raggiunto il limite aumenta di 1 direzione, invertendola } } else //se pari if(pos3 > limitsx) { pos3--; servo.write(pos3); } else { Direction++; // se ho raggiunto il limite aumenta di 1 direzione, invertendola } else {previousMillis3 = previousMillis4; pos3 = limitsx + 6; } }
Als (richting % 2) {//se dispari (da basso een alto) (da limiet sx een limiet dx) if(pos3 < limitdx) {pos3 ++; servo.write(pos3); void DetectNoise() { if (millis() >= previousMillis6){ CountCiclo6++; //se dopo un po di tempo/VelocityCiclo6)... previousMillis6 = previousMillis6+VelocityCiclo6 ; // ho accumulato troppi knokkeddx ed sx.... if (PreviousKnockDetected <= KnockDetected){ // per capirlo sottracco quelli di ora - quelli previus ciclo (lento) se > x sono in noise... PreviousKnockDetected = KnockDetected+NoiseTresh; // Azzer.... NoiseTresh = soglia che vado a aggiugnere... Serial.println(); Serial.println("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!noise detected!!! chi parla?????????????????"); //triggero log Serial.println(); Behaviour = 1; //e vado in modalità idonea... NoiseDetected = 1; //qs variabile impedisce ad altri knoc di cambiare behaviour x un po'.... previousMillis7 = previousMillis6+VelocityCiclo7 ; //setto dalay per rientro varaibile NoiseDetected else {richting ++; / / se ho raggiunto il limite aumenta di 1 direzione, invertendola }else{ if (millis() >= previousMillis7){ //dopo xx ulteirori millisecondi.. CountCiclo7++; NoiseDetected = 0; //vado via dastato di noisedetected... } } PreviousKnockDetected = KnockDetected+NoiseTresh; //debug// Serial.print("trigger rilevati in unità di tempo= "); //debug// Serial.println(PreviousKnockDetected-KnockDetected); } } anders //se pari if(pos3 > limitsx) {pos3--; servo.write(pos3); void VerboseLog() { if (millis() >= previousMillis1){ CountCiclo1++; previousMillis1 = previousMillis1+VelocityCiclo1 ; // each case gets 100ms else {richting ++; / / se ho raggiunto il limite aumenta di 1 direzione, invertendola //fun Verboselog Serial.print("| CountCiclo1= "); Serial.print(CountCiclo1);
Serial.print("| knockDX= "); Serial.print(millisAtKnockDx);
Serial.print("PreviousKnocKDetected= "); Serial.print(PreviousKnockDetected);
} else {als (millis() > = previousMillis7) {//dopo xx ulteirori millisecondi... CountCiclo7 ++; NoiseDetected = 0; Vado via dastato di noisedetected... Serial.print("KnocKDetected= "); Serial.print(KnockDetected); Serial.print("NoiseDetectedStatus= "); Serial.print(NoiseDetected); PreviousKnockDetected = KnockDetected + NoiseTresh; debug / / Serial.print ("leiden tot rilevati in unità di tempo ="); debug / / Serial.println(PreviousKnockDetected-KnockDetected); Serial.println("end");
}
}