From 54a478908b8865add69e24463a1c87aee2184b6c Mon Sep 17 00:00:00 2001 From: Andrea Manni Date: Thu, 7 May 2015 18:16:31 +0200 Subject: [PATCH] servo --- .../input_interrupt_0/input_interrupt_0.ino | 31 ++++++++ motors/servo/Knob_2/Knob_2.ino | 22 ++++++ motors/servo/Sweep_1/Sweep_1.ino | 31 ++++++++ .../BlinkWithoutDelay_6_1_interrupt.ino | 70 +++++++++++++++++++ 4 files changed, 154 insertions(+) create mode 100644 interrupts/input_interrupt_0/input_interrupt_0.ino create mode 100644 motors/servo/Knob_2/Knob_2.ino create mode 100644 motors/servo/Sweep_1/Sweep_1.ino create mode 100644 multitasking/BlinkWithoutDelay_6_1_interrupt/BlinkWithoutDelay_6_1_interrupt.ino diff --git a/interrupts/input_interrupt_0/input_interrupt_0.ino b/interrupts/input_interrupt_0/input_interrupt_0.ino new file mode 100644 index 0000000..80d1d17 --- /dev/null +++ b/interrupts/input_interrupt_0/input_interrupt_0.ino @@ -0,0 +1,31 @@ +/* + Interrupts : input + + Utilizzare un interrupt per intercettare + l'input di un interruttore momentaneo + + */ + +int ledPin = 13; +volatile int state = LOW; + +void setup() +{ + pinMode(ledPin, OUTPUT); + pinMode(2, INPUT_PULLUP); + attachInterrupt(0, blink, FALLING); +} + +void loop() +{ + //digitalWrite(ledPin, state); + delay(10000); // Mette in pausa Arduino per 10sec +} + +// Funzioni +void blink() +// Modifica dello stato del LED +{ + state = !state; + digitalWrite(ledPin, state); +} diff --git a/motors/servo/Knob_2/Knob_2.ino b/motors/servo/Knob_2/Knob_2.ino new file mode 100644 index 0000000..886e107 --- /dev/null +++ b/motors/servo/Knob_2/Knob_2.ino @@ -0,0 +1,22 @@ +// Controlling a servo position using a potentiometer (variable resistor) +// by Michal Rinott + +#include + +Servo myservo; // create servo object to control a servo + +int potpin = 0; // analog pin used to connect the potentiometer +int val; // variable to read the value from the analog pin + +void setup() +{ + myservo.attach(9); // attaches the servo on pin 9 to the servo object +} + +void loop() +{ + val = analogRead(potpin); // reads the value of the potentiometer (value between 0 and 1023) + val = map(val, 0, 1023, 0, 179); // scale it to use it with the servo (value between 0 and 180) + myservo.write(val); // sets the servo position according to the scaled value + delay(15); // waits for the servo to get there +} diff --git a/motors/servo/Sweep_1/Sweep_1.ino b/motors/servo/Sweep_1/Sweep_1.ino new file mode 100644 index 0000000..fb326e7 --- /dev/null +++ b/motors/servo/Sweep_1/Sweep_1.ino @@ -0,0 +1,31 @@ +// Sweep +// by BARRAGAN +// This example code is in the public domain. + + +#include + +Servo myservo; // create servo object to control a servo + // a maximum of eight servo objects can be created + +int pos = 0; // variable to store the servo position + +void setup() +{ + myservo.attach(9); // attaches the servo on pin 9 to the servo object +} + + +void loop() +{ + for(pos = 0; pos < 180; pos += 1) // goes from 0 degrees to 180 degrees + { // in steps of 1 degree + myservo.write(pos); // tell servo to go to position in variable 'pos' + delay(15); // waits 15ms for the servo to reach the position + } + for(pos = 180; pos>=1; pos-=1) // goes from 180 degrees to 0 degrees + { + myservo.write(pos); // tell servo to go to position in variable 'pos' + delay(15); // waits 15ms for the servo to reach the position + } +} diff --git a/multitasking/BlinkWithoutDelay_6_1_interrupt/BlinkWithoutDelay_6_1_interrupt.ino b/multitasking/BlinkWithoutDelay_6_1_interrupt/BlinkWithoutDelay_6_1_interrupt.ino new file mode 100644 index 0000000..6018195 --- /dev/null +++ b/multitasking/BlinkWithoutDelay_6_1_interrupt/BlinkWithoutDelay_6_1_interrupt.ino @@ -0,0 +1,70 @@ +/* Blink without Delay + + Utilizzo di un interrupt per richiamare update() + una volta ogni millesimo di secondo + */ + +// Oggetti: +class Lampeggiatore { + // Lampeggia un LED utilizzando millis() + // Variabili + int ledPin ; // il numero del LED pin + int ledState ; // stato attuale del LED + long interval ; // milliseconds di intervallo nel lampeggiare + long previousMillis ; //precedente cambio di stato + + // Constructor: come viene instanziato un oggetto facente parte della classe +public: + Lampeggiatore(int pin, long time) + { + ledPin = pin; + pinMode(ledPin, OUTPUT); + ledState = LOW; + previousMillis = 0; + interval = time; + } + +// Una funzione facente parte di una classe prende il nome di "metodo" della stessa: + void Update () { + // Illumina il ledB secondo un intervallo passato come argomento + + if(millis() - previousMillis > interval) { + // save the last time you blinked the LED + previousMillis = millis(); + + // if the LED is off turn it on and vice-versa: + ledState = !ledState ; // Inverti il LED + } + // set the LED with the ledState of the variable: + digitalWrite(ledPin, ledState); + } + +}; + +// Instanziamo i due led dalla classe +Lampeggiatore ledA(13, 1000); +Lampeggiatore ledB(12, 500); + +void setup() { + // Interrupt ogni millesimo di secondo + // Timer0 is already used for millis() - we'll just interrupt somewhere + // in the middle and call the "Compare A" function below + OCR0A = 0xAF; + TIMSK0 |= _BV(OCIE0A); +} +// A ogni millisecondo l'interrupt attiva questa funzione +SIGNAL(TIMER0_COMPA_vect) +{ + ledA.Update(); + ledB.Update(); +} +void loop() +{ + // Gli aggiornamenti dei LED ora vengono richiamati dalla funzione + // associata agli interrupt +// ledA.Update(); +// ledB.Update(); +} + + + -- 2.39.2