Advertisement
Guest User

Untitled

a guest
Feb 23rd, 2014
171
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.85 KB | None | 0 0
  1. /*
  2. Conditionals - If statement
  3.  
  4. This example demonstrates the use of if() statements.
  5. It reads the state of a potentiometer (an analog input) and turns on an LED
  6. only if the LED goes above a certain threshold level. It prints the analog value
  7. regardless of the level.
  8.  
  9. The circuit:
  10. * potentiometer connected to analog pin 0.
  11. Center pin of the potentiometer goes to the analog pin.
  12. side pins of the potentiometer go to +5V and ground
  13. * LED connected from digital pin 13 to ground
  14.  
  15. * Note: On most Arduino boards, there is already an LED on the board
  16. connected to pin 13, so you don't need any extra components for this example.
  17.  
  18. created 17 Jan 2009
  19. modified 9 Apr 2012
  20. by Tom Igoe
  21.  
  22. This example code is in the public domain.
  23.  
  24. http://arduino.cc/en/Tutorial/IfStatement
  25.  
  26. */
  27.  
  28. // These constants won't change:
  29. const int analogPinA = A0; // pin that the sensor is attached to
  30. const int ledPinUP = 6; // pin that the Shift Up Relay is attached to
  31. const int analogPinB = A1; // pin that the sensor is attached to
  32. const int ledPinDOWN = 5; // pin that the Shift Down Relay is attached to
  33. const int analogPinC = A2; // pin that the sensor is attached to
  34. const int ledPinBUZZER = 3; // pin that the Shift Buzzer is attached to
  35. const int ledPinON = 7; // pin that activates initial changeover relay
  36. const int threshold = 400; // an arbitrary threshold level that's in the range of the analog input
  37.  
  38. void setup() {
  39. // initialize the LED pin as an output:
  40. pinMode(ledPinUP, OUTPUT);
  41. // initialize the LED pin as an output:
  42. pinMode(ledPinDOWN, OUTPUT);
  43. // initialize the LED pin as an output:
  44. pinMode(ledPinBUZZER, OUTPUT);
  45. // initialise LED Pin as output:
  46. pinMode (ledPinON, OUTPUT);
  47. // initialize serial communications:
  48. Serial.begin(9600);
  49. }
  50.  
  51. void loop() {
  52. digitalWrite(ledPinON, LOW);
  53. // read the value of the potentiometer:
  54. int analogValueA = analogRead(analogPinA);
  55.  
  56. // if the analog value is high enough, turn on the LED:
  57. if (analogValueA >750 && analogValueA <920) {
  58. digitalWrite(ledPinUP, LOW);
  59. }
  60. else {
  61. digitalWrite(ledPinUP,HIGH);
  62. }
  63. // read the value of the potentiometer:
  64. int analogValueB = analogRead(analogPinB);
  65.  
  66. // if the analog value is high enough, turn on the LED:
  67. if (analogValueB >320 && analogValueB <420) {
  68. digitalWrite(ledPinDOWN, LOW);
  69. }
  70. else {
  71. digitalWrite(ledPinDOWN,HIGH);
  72. }
  73. // read the value of the potentiometer:
  74. int analogValueC = analogRead(analogPinC);
  75.  
  76. // if the analog value is high enough, turn on the LED:
  77. if (analogValueC >320 && analogValueC <920) {
  78. digitalWrite(ledPinBUZZER, LOW);
  79. }
  80. else {
  81. digitalWrite(ledPinBUZZER,HIGH);
  82. }
  83.  
  84. // print the analog value:
  85. Serial.println(analogValueA);
  86. delay(1); // delay in between reads for stability
  87. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement