Exemplo com o uso de entradas e saídas analógicas. Usando o Arduino para fazer um LED piscar de maneira suave de acordo com a leitura de um potenciômetro.
Código:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
int sensorPin = A0; // select the input pin for the potentiometer int ledPin = 9; // select the pin for the LED int brightness = 0; // how bright the LED is int fadeAmount = 5; // how many points to fade the LED by int sensorValue = 0; // variable to store the value coming from the sensor void setup() { // declare the ledPin as an OUTPUT: pinMode(ledPin, OUTPUT); } void loop() { // read the value from the sensor: sensorValue = analogRead(sensorPin); // turn the ledPin on // set the brightness of pin 9: analogWrite(ledPin, brightness); // change the brightness for next time through the loop: brightness = brightness + fadeAmount; // reverse the direction of the fading at the ends of the fade: if (brightness == 0 || brightness == 255) { fadeAmount = -fadeAmount ; } delay(map(sensorValue, 0, 1023, 5, 100)); } |