Advertisement
epl70

Analog On/Off Switch for ATtiny84

Feb 12th, 2016
325
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.18 KB | None | 0 0
  1. /*
  2.   Analog Input
  3.   Demonstrates analog input by reading an analog sensor on analog pin 0 and
  4.   turning on and off a light emitting diode(LED)  connected to digital pin 0.
  5.   Whether the LED if on for readings below 2.5.V.
  6.  
  7.   The circuit:
  8.    Potentiometer attached to analog input A1
  9.    center pin of the potentiometer to the analog pin
  10.    one side pin (either one) to ground
  11.    the other side pin to +5V
  12.    LED anode (long leg) attached to digital output 0
  13.    LED cathode (short leg) attached to ground
  14.  
  15.  
  16.   Created by David Cuartielles
  17.   modified 12 Feb 2016
  18.   By Ernst Pluess
  19.  
  20.   This example code is in the public domain.
  21.  
  22. */
  23.  
  24. int sensorPin = A1;    // select the input pin for the potentiometer
  25. int ledPin = 0;      // select the pin for the LED
  26. int sensorValue = 0;  // variable to store the value coming from the sensor
  27.  
  28. void setup() {
  29.   // declare the ledPin as an OUTPUT:
  30.   pinMode(ledPin, OUTPUT);
  31. }
  32.  
  33. void loop() {
  34.   // read the value from the sensor:
  35.   sensorValue = analogRead(sensorPin);
  36.   if (sensorValue < 512) {
  37.     // turn the ledPin on
  38.     digitalWrite(ledPin, HIGH);
  39.   }
  40.   else {
  41.     // turn the ledPin off:
  42.     digitalWrite(ledPin, LOW);
  43.   }
  44.  
  45. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement