Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Conditionals - If statement
- This example demonstrates the use of if() statements.
- It reads the state of a potentiometer (an analog input) and turns on an LED
- only if the LED goes above a certain threshold level. It prints the analog value
- regardless of the level.
- The circuit:
- * potentiometer connected to analog pin 0.
- Center pin of the potentiometer goes to the analog pin.
- side pins of the potentiometer go to +5V and ground
- * LED connected from digital pin 13 to ground
- * Note: On most Arduino boards, there is already an LED on the board
- connected to pin 13, so you don't need any extra components for this example.
- created 17 Jan 2009
- modified 9 Apr 2012
- by Tom Igoe
- This example code is in the public domain.
- http://arduino.cc/en/Tutorial/IfStatement
- */
- // These constants won't change:
- const int analogPinA = A0; // pin that the sensor is attached to
- const int ledPinUP = 6; // pin that the Shift Up Relay is attached to
- const int analogPinB = A1; // pin that the sensor is attached to
- const int ledPinDOWN = 5; // pin that the Shift Down Relay is attached to
- const int analogPinC = A2; // pin that the sensor is attached to
- const int ledPinBUZZER = 3; // pin that the Shift Buzzer is attached to
- const int ledPinON = 7; // pin that activates initial changeover relay
- const int threshold = 400; // an arbitrary threshold level that's in the range of the analog input
- void setup() {
- // initialize the LED pin as an output:
- pinMode(ledPinUP, OUTPUT);
- // initialize the LED pin as an output:
- pinMode(ledPinDOWN, OUTPUT);
- // initialize the LED pin as an output:
- pinMode(ledPinBUZZER, OUTPUT);
- // initialise LED Pin as output:
- pinMode (ledPinON, OUTPUT);
- // initialize serial communications:
- Serial.begin(9600);
- }
- void loop() {
- digitalWrite(ledPinON, LOW);
- // read the value of the potentiometer:
- int analogValueA = analogRead(analogPinA);
- // if the analog value is high enough, turn on the LED:
- if (analogValueA >750 && analogValueA <920) {
- digitalWrite(ledPinUP, LOW);
- }
- else {
- digitalWrite(ledPinUP,HIGH);
- }
- // read the value of the potentiometer:
- int analogValueB = analogRead(analogPinB);
- // if the analog value is high enough, turn on the LED:
- if (analogValueB >320 && analogValueB <420) {
- digitalWrite(ledPinDOWN, LOW);
- }
- else {
- digitalWrite(ledPinDOWN,HIGH);
- }
- // read the value of the potentiometer:
- int analogValueC = analogRead(analogPinC);
- // if the analog value is high enough, turn on the LED:
- if (analogValueC >320 && analogValueC <920) {
- digitalWrite(ledPinBUZZER, LOW);
- }
- else {
- digitalWrite(ledPinBUZZER,HIGH);
- }
- // print the analog value:
- Serial.println(analogValueA);
- delay(1); // delay in between reads for stability
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement