Lezione 3

by / giovedì, 03 Dicembre 2015 / Published in Archivio Robotica

LEZIONE n° 3 – SENSORI

La lezione è sui sensori e sulla loro gestione.

Abbiamo 2 ingressi connessi a due interruttori e 3 uscite connesse a tre LED.

Quando schiacciamo il primo interruttore si accende il LED rosso, quando schiacciamo il secondo interruttore si accende il LED giallo, quando li schiacciamo insieme si accende il LED verde.

Per provare a fare le modifiche proposte copiare il listato (colorato in rosso) ed incollarlo in un editor di solo testo [Notepad ad esempio]

listato programma esempio numero 3:


/*
Button

Turns on and off a light emitting diode(LED) connected to digital
pin 13, when pressing a pushbutton attached to pin 2.

The circuit:
* LED attached from pin 13 to ground
* pushbutton attached to pin 2 from +5V
* 10K resistor attached to pin 2 from ground
*/

// set pin numbers:
const int buttonPin1 = 7; // the number of the pushbutton pin
const int buttonPin2 = 2; // the number of the pushbutton pin
const int ledPin1 = 6; // the number of the LED pin
const int ledPin2 = 5; // the number of the LED pin

// variables will change:
int buttonState1 = 0; // variable for reading the pushbutton status
int buttonState2 = 0; // variable for reading the pushbutton status

void setup() {
// initialize the LED pin as an output:
pinMode(ledPin1, OUTPUT);
pinMode(ledPin2, OUTPUT);
// initialize the pushbutton pin as an input:
pinMode(buttonPin1, INPUT);
pinMode(buttonPin2, INPUT);
}

void loop(){
// read the state of the pushbutton value:
buttonState1 = digitalRead(buttonPin1);
buttonState2 = digitalRead(buttonPin2);

// check if the pushbutton is pressed.
// if it is, the buttonState is HIGH:
if (buttonState1 == LOW || buttonState2 == LOW ) {
// turn LED on:
digitalWrite(ledPin1, LOW);
digitalWrite(ledPin2, LOW);
}
if (buttonState1 == HIGH ) {
// turn LED on:
digitalWrite(ledPin1, HIGH);
digitalWrite(ledPin2, LOW);
}
if (buttonState2 == HIGH ) {
// turn LED on:
digitalWrite(ledPin1, LOW);
digitalWrite(ledPin2, HIGH);
}
if ((buttonState1 == HIGH)&&(buttonState2 == HIGH )) {
// turn LED on:
digitalWrite(ledPin1, HIGH);
digitalWrite(ledPin2, HIGH);
}
}

LE MODIFICHE PROPOSTE SONO:

invece di accendere il LED FARLO LAMPEGGIARE PER DUE VOLTE CONSECUTIVE

listato programma esempio numero 4:

/*

Button

Turns on and off a light emitting diode(LED) connected to digital

pin 13, when pressing a pushbutton attached to pin 2.

The circuit:

* LED attached from pin 13 to ground

* pushbutton attached to pin 2 from +5V

* 10K resistor attached to pin 2 from ground

*/

// set pin numbers:

int sensorPin = A0;

int sensorValue = 0; // variable to store the value coming from the sensor

const int ledPin1 = 5; // the number of the LED pin

const int ledPin2 = 6; // the number of the LED pin

const int ledPin3 = 8; // the number of the LED pin

// variables will change:

void setup() {

Serial.begin(9600);

// initialize the LED pin as an output:

pinMode(ledPin1, OUTPUT);

pinMode(ledPin2, OUTPUT);

pinMode(ledPin3, OUTPUT);

// initialize the pushbutton pin as an input:

}

void loop(){

// read the state of the pushbutton value:

sensorValue = analogRead(sensorPin);

// print the results to the serial monitor:

Serial.print(“sensor = ” );

Serial.print(sensorValue);

Serial.print(“\n “);

// Serial.println(outputValue);

// wait 10 milliseconds before the next loop

// for the analog-to-digital converter to settle

// after the last reading:

delay(10);

// check if the pushbutton is pressed.

// if it is, the buttonState is HIGH:

if ((100 < sensorValue)&&( sensorValue < 300) ) {

// turn LED on:

digitalWrite(ledPin1, LOW);

digitalWrite(ledPin2, LOW);

digitalWrite(ledPin3, LOW);

}

if ((300 < sensorValue)&&( sensorValue < 550) ) {

// turn LED on:

digitalWrite(ledPin1, HIGH);

digitalWrite(ledPin2, LOW);

digitalWrite(ledPin3, LOW);

}

if ((550 < sensorValue)&&( sensorValue < 650) ) {

// turn LED on:

digitalWrite(ledPin1, LOW);

digitalWrite(ledPin2, HIGH);

digitalWrite(ledPin3, LOW);

}

if ((650 < sensorValue)&&( sensorValue < 850) ) {

// turn LED on:

digitalWrite(ledPin1, LOW);

digitalWrite(ledPin2, LOW);

digitalWrite(ledPin3, HIGH);

}

if ((850 < sensorValue)&&( sensorValue < 1050) ) {

// turn LED on:

digitalWrite(ledPin1, HIGH);

digitalWrite(ledPin2, HIGH);

digitalWrite(ledPin3, HIGH);

}

}

LE MODIFICHE PROPOSTE SONO:

AUMENTARE i valori da segnalare in cinque condizioni diverse

di seguito le slide della lezione

(102)

Tagged under: , ,
TOP