diff options
-rw-r--r-- | 5-microfono-relay.ino | 27 | ||||
-rw-r--r-- | 6-allarme.ino | 64 | ||||
-rw-r--r-- | 7-robot-linefollowing.ino | 119 |
3 files changed, 210 insertions, 0 deletions
diff --git a/5-microfono-relay.ino b/5-microfono-relay.ino new file mode 100644 index 0000000..83fe599 --- /dev/null +++ b/5-microfono-relay.ino @@ -0,0 +1,27 @@ +int sensorPin = A0; //KY-038 microphone +int relay = 13; +int sensorValue = 0; +bool on = false; + +void setup () +{ + pinMode (relay, OUTPUT); + digitalWrite (relay, LOW); + Serial.begin (9600); +} + +void loop () +{ + sensorValue = analogRead (sensorPin); + Serial.println (sensorValue, DEC); + if (sensorValue > 500) { + /*if (on == true) { + on = false; + } else { + on = true; + }*/ + on = !on; + } + digitalWrite (relay, on); + delay(100); +}
\ No newline at end of file diff --git a/6-allarme.ino b/6-allarme.ino new file mode 100644 index 0000000..211ca30 --- /dev/null +++ b/6-allarme.ino @@ -0,0 +1,64 @@ +// nota, frequenza (Hz). +#define Do4 261 +#define Re4 294 +#define Mi4 329 +#define Fa4 349 +#define Sol4 392 +#define La4 440 +#define Si4 493 +#define Do5 523 + +// Una pausa è una nota con frequenza zero +#define P 0 + + +const int buttonPin = 2; +// Il buzzer deve essere collegato a un pin PWM +int speakerOut = 9; + +void setup() { + pinMode(buttonPin, INPUT); + pinMode(speakerOut, OUTPUT); +} + +// melody[] è l'array con le varie note da suonare +// beats[] è l'array con la durata di ciascuna nota da suonare in frazioni di semibreve +int melody[] = { La4, La4, Si4, Do5, Do5, Si4, La4, Sol4, Fa4, Fa4, Sol4, La4, La4, Sol4, Sol4, P }; +int beats[] = { 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 3, 8, 2, 1 }; +int MAX_COUNT = sizeof(melody) / 2; // Calcoliamo la dimensione degli array + +//Tempo della nota semibreve in millesimi di secondo +long tempo = 4000; +// Pausa tra le note in milionesimi di secondo +int pause = 1000; + + +int tone_ = 0; +int beat = 0; +long duration = 0; +int volume = 1023; + + +void loop() { + if (digitalRead(buttonPin) == LOW) { + // Scorriamo tutte le note e le rispettive durate + for (int i=0; i<MAX_COUNT; i++) { + tone_ = melody[i]; + beat = beats[i]; + + + duration = (tempo)/beat; // Calcoliamo la durata in millisecondi della nota attuale + if (tone_ > 0) { + if (digitalRead(buttonPin) == HIGH) break; + noTone(speakerOut); + tone(speakerOut,tone_,duration); + delay(duration); + // Piccola pausa dopo la nota + delayMicroseconds(pause); + + } else { + delay(duration); + } + } + } +} diff --git a/7-robot-linefollowing.ino b/7-robot-linefollowing.ino new file mode 100644 index 0000000..b655bf6 --- /dev/null +++ b/7-robot-linefollowing.ino @@ -0,0 +1,119 @@ +/* +basato sul codice di esempio di Arvind Sanjeev per il set di sensori QTR-8A + + */ +#include <Servo.h> + +Servo left; +Servo right; + +int mid = 0; +int mn = 1023; +int mx = 0; + +void setup() +{ + +left.attach(9, 800, 2200); //left servo motor +right.attach(10, 800, 2200); //right servo motor + +Serial.begin(9600); + +digitalWrite(13, LOW); + +right.write(90);//stop signal +left.write(90);//stop signal + +for(int i=0; i<5000; i++) +{ +digitalWrite(13, HIGH); + +for(int j=0; j<=5; j++)//Calibrating the sensor, finding max and +{ //min reflectance values. +int val = analogRead(j); +if(val >= mx) +mx = val; +if(val <= mn) +mn = val; +} +delay(1); +} + +mid = ((mx + mn)/2); +digitalWrite(13, LOW); + +right.write(90); +left.write(90); +} + +void loop() +{ + +int s0 = analogRead(0);//Signal pin 1 on the board +int s1 = analogRead(1);//Signal pin 2 on the board +int s2 = analogRead(2);//Signal pin 3 on the board +int s3 = analogRead(3);//Signal pin 4 on the board +int s4 = analogRead(4);//Signal pin 5 on the board +int s5 = analogRead(5);//Signal pin 6 on the board + + +Serial.print("Mid: "); +Serial.print(mid); +Serial.print(" "); +Serial.print(s0); +Serial.print(" "); +Serial.print(s1); +Serial.print(" "); +Serial.print(s2); +Serial.print(" "); +Serial.print(s3); +Serial.print(" "); +Serial.print(s4); +Serial.print(" "); +Serial.print(s5); +Serial.print(" "); +Serial.println(); + +right.write(180);//Move forward +left.write(0);//Move forward + +delay(1); + +int averageLeft = (s0+s1+s2)/3; +int averageRight = (s3+s4+s5)/3; + + +if(((averageLeft)>((averageRight)+240)))//Move right +{ +right.write(130);//180 +left.write(90);//90 +Serial.print(" RIGHT"); +delay(abs((averageRight-(averageLeft))/2)); +} + +if(((averageLeft)<((averageRight)-240)))//Move left +{ +right.write(90);//90 +left.write(40);//0 +Serial.print(" LEFT"); +delay(abs(((averageRight)-(averageLeft))/2)); +} + +if((s0 > mid)&&(s5 > mid))//Stop if all the sensors give low +{ //reflectance values +right.write(90); +left.write(90); +Serial.print(" STOP"); + +for(int k=0; k<50; k++) +{ +digitalWrite(13, HIGH); +delay(100); +digitalWrite(13, LOW); +delay(100); +} +delay(5000); +} + + +} |