Advertisement
Guest User

Breathalyzer

a guest
Mar 28th, 2015
181
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.95 KB | None | 0 0
  1. /*
  2. Analog input, analog output, serial output
  3.  
  4. Reads an analog input pin, maps the result to a range from 0 to 255
  5. and uses the result to set the pulsewidth modulation (PWM) of an output pin.
  6. Also prints the results to the serial monitor.
  7.  
  8. The circuit:
  9. * potentiometer connected to analog pin 0.
  10. Center pin of the potentiometer goes to the analog pin.
  11. side pins of the potentiometer go to +5V and ground
  12. * LED connected from digital pin 9 to ground
  13.  
  14. created 29 Dec. 2008
  15. modified 9 Apr 2012
  16. by Tom Igoe
  17.  
  18. This example code is in the public domain.
  19.  
  20. */
  21.  
  22. #include <Wire.h> // Comes with Arduino IDE
  23. #include <LiquidCrystal_I2C.h>
  24.  
  25. LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE); // Set the LCD I2C address
  26.  
  27. // These constants won't change. They're used to give names
  28. // to the pins used:
  29. const int analogInPin = A0; // Analog input pin that the potentiometer is attached to
  30. const int analogOutPin = 9; // Analog output pin that the LED is attached to
  31.  
  32. int s = 0; // value read from the pot
  33. int outputValue = 0; // value output to the PWM (analog out)
  34.  
  35. void setup() {
  36. // initialize serial communications at 9600 bps:
  37. Serial.begin(9600);
  38. pinMode(8,OUTPUT);
  39. pinMode(9,OUTPUT);
  40. pinMode(10,OUTPUT);
  41. pinMode(11,OUTPUT);
  42. digitalWrite(8,LOW);
  43. digitalWrite(9,LOW);
  44. digitalWrite(10,LOW);
  45. digitalWrite(11,LOW);
  46.  
  47. lcd.begin(16,2); // initialize the lcd for 16 chars 2 lines, turn on backlight
  48. lcd.backlight(); // finish with backlight on
  49.  
  50. lcd.clear();
  51. lcd.setCursor(0,0); //Start at character 0 on line 0
  52. lcd.print("XXXXXXXX");
  53. lcd.setCursor(0,1);
  54. lcd.print("Alchohol level");
  55. }
  56.  
  57. void loop() {
  58. // read the analog in value:
  59. s = analogRead(analogInPin);
  60. // map it to the range of the analog out:
  61. //outputValue = map(sensorValue, 0, 1023, 0, 255);
  62. // change the analog out value:
  63. analogWrite(analogOutPin, outputValue);
  64.  
  65. if(s<400) {
  66. digitalWrite(9,LOW);
  67. digitalWrite(10,HIGH);
  68. digitalWrite(11,LOW);
  69. lcd.clear();
  70. lcd.setCursor(0,0); //Start at character 0 on line 0
  71. lcd.print("Low");
  72. lcd.setCursor(0,1);
  73. lcd.print("Alchohol level");
  74. }
  75. else if ((s>=400)&&(s<600)){
  76. digitalWrite(9,HIGH);
  77. digitalWrite(10,LOW);
  78. digitalWrite(11,LOW);
  79. lcd.clear();
  80. lcd.setCursor(0,0); //Start at character 0 on line 0
  81. lcd.print("Medium");
  82. lcd.setCursor(0,1);
  83. lcd.print("Alchohol level");
  84. }
  85. else if (s>=600){
  86. digitalWrite(9,LOW);
  87. digitalWrite(10,LOW);
  88. digitalWrite(11,HIGH);
  89. lcd.clear();
  90. lcd.setCursor(0,0); //Start at character 0 on line 0
  91. lcd.print("High");
  92. lcd.setCursor(0,1);
  93. lcd.print("Alchohol level");
  94.  
  95. }
  96.  
  97. // print the results to the serial monitor:
  98. Serial.print("sensor = " );
  99. Serial.print(s);
  100. Serial.print("\t output = ");
  101. Serial.println(outputValue);
  102.  
  103. // wait 2 milliseconds before the next loop
  104. // for the analog-to-digital converter to settle
  105. // after the last reading:
  106. delay(2);
  107. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement