cR_17_lezione 02

by / sabato, 05 Novembre 2016 / Published in Archivio Robotica

 

 

LEZIONE n° 2 – SENSORI

 

 

 

 

La lezione è sui sensori e sulla loro gestione. I primi sensori che saranno gestiti sono i LED.

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 1:

 

/* Blink Turns on and off a light emitting diode(LED) connected to a digital pin. The circuit: * LED attached from pin 13 to ground. * Note: on most Arduinos, there is already an LED on the board that’s attached to pin 13, so no hardware is needed for this example. */

// set pin numbers: const int ledPin = 13;         // the number of the LED pin

// Variables will change: int ledState = LOW;           // ledState used to set the LED

void setup() { pinMode(ledPin, OUTPUT);             // set the digital pin as output: }

void loop()                            // here is where you’d put code that needs to be running all the time. {                                             // 1000 egual 1 seconds  unsigned long tempo = 250;

digitalWrite(ledPin, HIGH);  delay(tempo);

digitalWrite(ledPin, LOW);  delay(tempo); }

Modifiche proposte:

cambiare il tempo di accensione

 

 

 

listato programma esempio numero 2:

/* Blink Turns on and off a light emitting diode(LED) connected to a digital pin. The circuit: * LED attached from pin 13 to ground. * Note: on most Arduinos, there is already an LED on the board that’s attached to pin 13, so no hardware is needed for this example. */

// set pin numbers: const int buttonPin = 2;

     // the number of the pushbutton pin const int ledPin = 13;         // the number of the LED pin

// Variables will change: int ledState = LOW;           // ledState used to set the LED int buttonState = 0;             // variable for reading the pushbutton status

void setup()

{

 pinMode(ledPin, OUTPUT);           // set the digital pin as output:  

pinMode(buttonPin, INPUT);         // initialize the pushbutton pin as an input:

}

void loop()                            // here is where you’d put code that needs to be running all the time. {                                             // 1000 egual 1 seconds  // read the state of the pushbutton

value:  buttonState = digitalRead(buttonPin);

 if (buttonState == HIGH)

{             // check if the pushbutton is pressed.  // turn LED on:  digitalWrite(ledPin, HIGH);           // if it is, the buttonState is HIGH:  }  else {  // turn LED off:  digitalWrite(ledPin, LOW);

 }

}

 

 

 

Modifiche proposte:

cambiare il pin di controllo

controllare il led da DUE pin

 

 

 

 

di seguito le slide della lezione

 

 

(50)

TOP