Arduino Code??

I have created a code for the arduino breathalyzer and I wat to confirm that will it work properly so please someone who's good with this can do this for me.And yes the code compiles successfully but will it work as expexted.It should light up some of the 7 LEDs according to how much you drink.It also has a pot to calibrate the sensor and i will be using a MQ-3 alcohol sensor.
here is the code:

const int analogPin = 0;    // the pin that the potentiometer is attached to
const int ledCount = 7;    // the number of LEDs in the bar graph

int ledPins[] = {
7,6,5,4,3,2,1 // Here we have the number of LEDs to use in the BarGraph
  };  


void setup() {

  for (int thisLed = 0; thisLed < ledCount; thisLed++) {
    pinMode(ledPins[thisLed], OUTPUT);
  }}

void loop() {
  //This is the code to light up LED's
  int sensorReading = analogRead(analogPin);

  int ledLevel = map(sensorReading, 500, 1023, 0, ledCount);


  for (int thisLed = 0; thisLed < ledCount; thisLed++) {

    if (thisLed < ledLevel) {
      digitalWrite(ledPins[thisLed], HIGH);
    }

    else {
      digitalWrite(ledPins[thisLed], LOW);
    }  }}

sort by: active | newest | oldest
frollard5 years ago
Seems okay so far. You didn't explicitly define thisLed anywhere, but it's technically okay as a local variable when declared at the start of a loop.

The code seems fine.

I would add serial debugging
(in void setup, add Serial.begin(9600); )
(in void loop, add

Serial.print("Analog:");
Serial.println("sensorReading"); //show the input value
Serial.print("value:");
Serial.println("ledLevel"); //show the intended level
delay(1000); //so it doesn't spam the serial too often
Serial.print("output:");
and in the if
Serial.print("o"); // led should light, put a circle
and in the else
Serial.print("x"); //led should be off, put an x
after the end of the else
Serial.println();


Lastly, double check your wiring is connected to the correct pins!
Bot1398 (author)  frollard5 years ago
So you mean the code is OK and will work!!
it looks good, but you need to build it to find out.
Best way to test the code is to upload it to the Arduino and give it a test run. Blow into it and see what the reading is. Take a swig of beer and blow, then take a swig of liquor and blow. If all is working right the meter should show a progressively higher reading with each blow.
Bot1398 (author)  mpilchfamily5 years ago
I just wanted to tell you that I am 13 and how on the earth can I drink beer.I tested my sensor with rubbing alcohol
Then why are you making a breathalyzer?

Any time a company is producing a new product or app they go through a testing phase. They bring in people from within and out of the company to try the product out report there findings. You can do the same thing by enlisting the help of your parents.
Bot1398 (author)  mpilchfamily5 years ago
I am just making it as a fun project.Good idea i will take help of my parents
Another option would be to gargle with Listerine. It has an alcohol content and should register a little on your meter.
Bot1398 (author)  mpilchfamily5 years ago
Thanxx man never thought listerine has alcohol I use it everyday after brushing.