Created
March 21, 2014 00:59
-
-
Save VimKanzo/9677412 to your computer and use it in GitHub Desktop.
Breathalyzer using Alcohol Gas Sensor with Arduino
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#define qtyLED 6 //Declare the quantity of LED's for the bar graph | |
int LEDs[qtyLED]; //Array to store the states of the LEDs | |
int sensorPin = 0; //Variable that will hold the current value of the sensor | |
int time; //Variable that will hold the amount of time for the sensor to be read | |
int maxreading; //Variable to store the maximum value read during the last reading | |
//For loop counting variables | |
int i; | |
int j; | |
void setup() | |
{ | |
//Initialize the counting variables | |
//Serial.begin(9600); | |
i=0; | |
j=2; | |
while(i < qtyLED) //While i is less than the number of defined LED's | |
{ //...save the value of i+2 in the LEDs array. we add 2 so that it represents one of our digital pins | |
LEDs[i] = j; | |
i++; | |
j++; | |
} | |
for(i=0;i<qtyLED;i++) //Define the LED Pins as outputs. In this case 2->7 | |
{ | |
pinMode(LEDs[i], OUTPUT); | |
} | |
pinMode(13, OUTPUT); //Define pin 13 as an output so we can show when we are not taking a reading from the sensor(Blinkin | |
//...Or when we are taking a reading from the sensor (Solid) | |
} | |
void loop() | |
{ | |
PORTB = PORTB ^ 100000; //Invert pin 13 to give the LED a blinking effect | |
delay(100); //Delay so that the user can see the blinking happen | |
int sensor = analogRead(sensorPin); //Read the sensors value and store it in the sensor variable | |
if(sensor >= 40) //If the value is less that 40(value for the chemical being used in this example) | |
{ | |
digitalWrite(13, HIGH); //Turn on the blue LED indecating that the sensor detected a minimal amount of alcohol (sensor >= 40) | |
maxreading = 0; //Initiate the max reading to 0 | |
for(time = 0;time <= 5000;time++) //Read the sensor for 5 seconds | |
{ //...every millisecond update the value of the max reading of the sensor | |
int sensor = analogRead(sensorPin); | |
delay(1); | |
if(sensor > maxreading) | |
{ | |
maxreading = sensor; | |
} | |
} | |
digitalWrite(13, LOW); //As soon as the reading is complete turn off the blue LED | |
int level = map(maxreading, 0, 200, 0, qtyLED); //Map the value from 0 to 200(reading from sensor) to 0 and the number of LEDs which is 6 | |
//...0 to 6(LEDs) level determines how many of them turn on. | |
for(i=0;i<qtyLED;i++) //Compare all the leds current state with the value of the level that was read | |
{ //if for example the level read was 5, then LEDs 2->6 would turn on. | |
if (i < level) //If the current LED being checked is less that the level | |
{ | |
digitalWrite(LEDs[i], HIGH); //...Turn it on | |
} | |
else //If the current LED being checked is higher than the level | |
{ | |
digitalWrite(LEDs[i], LOW); //...Turn it off | |
} | |
} | |
delay(10000); //Wait 10 seconds for the user to get the reading from the led bar graph | |
for(i=0;i<qtyLED;i++) //Turn off all the LEDs | |
{ | |
digitalWrite(LEDs[i],LOW); | |
} | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment