Absolute noob starting a college project, a breathalyzer

I'm a first year applied physics and instrumentation student and we started messing about with the arduino about 3 weeks ago , so far I have been fairly competent with the basic such as control led's with potentiometers and programming a binary thermometer. I know serial read and have been messing about with a speaker for the last couple of days, have got it to change pitch with varying potentiometer readings and give a single note between readings

Ive been given as part of a group task to design and program a breathalyzer, for the time being my task is get an lcd screen (was thinking a basic 16x2) to show readings of BAC and to have a speaker make a tone indicating when the pressure sensor is reading within range

thing is i want the speaker to give a continuous tone when the pressure sensor reads the right right range but give a double high pitched beep when it goes over, as ive said ive got he continuous tone but the double beep is a continuous which is a tad annoying

also ive been trying to get this mario tune to play only once a button is pushed but seem to be coming up with issues in the code

any advice? ive been scouring the internet and theres loads out there but alot which is not applicable or im just too inexperience to take anything from

play_note_C_when_in_range.ino (692 Bytes)

button_mario.ino (2.8 KB)

ive attached the 2 bits of code im on about as attachments not too sure how to get in a scroll box as i have seen in other posts

also was thinking is there any way i could have the mario code in a different tab so that when the button reads low it will play the fuction from that tab

evancleary:
ive attached the 2 bits of code im on about as attachments not too sure how to get in a scroll box as i have seen in other posts

Try reading the how to use this forum sticky post that you missed at the start of every section.

yer sorry bout, my bad. This is the mario code (i sourced this from youtube)

i get an expected primary function before "for" error

int speakerPin = 13;
int button = 12;

int length = 295; // the number of notes
char notes[] = "EE E CE G  g  C  g e  a b ia gEGA FG E CDb C  g e  a b ia gEGA FG E CDb  GNFR E uaC aCD GNFR E 1 11   GNFR E uaC aCD L  D C   CC C CD EC ag  CC C CDE  CC C CD EC ag  EE E CE G  g  C  g e  a b ia gEGA FG E CDb C  g e  a b ia gEGA FG E CDb EC g u aF Fa  bAAAGFEC ag  EC g u aF Fa  bF FFEDCe ec  "; // a space represents a rest
float beats[] = { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 2, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, //Page 1
                2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 2, 1, 1, 2, 4, //Page 2
                1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 4, 4, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 2, //Page4
                1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, //Page 5
                1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 2, 1.3, 1.3, 1.3, 1.3, 1.3, 1.3, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1.3, 1.3, 1.3, 1, 1, 1, 1, 1, 1, 2 }; //Page 6
                
int tempo = 95;

void playTone(int ton1, int duration) {
  for (long i = 0; i < duration * 1000L; i += ton1) {
    tone(speakerPin, ton1);
    delayMicroseconds(ton1);
  }
  noTone(speakerPin);
}

void playNote(char note, int duration) {
//                        c    c#   d    d#   e    f    f#   g    g#   a    a#   b
  char names[] = { ' ',  '!', '2', '3', '4', '5', '6', '7', '8', '9', '0', '-', '=', 'c', 'd', 'e', 'f', 'g', 'a', 'b', 'C', 'D', 'E', 'F', 'G', 'A', 'B', 'i', 'N', 'R', 'u',  '1', 'L', 'k'}; // [i = b flat] [N = G flat] [R = D#] [u = g#] [1 = C oct. 5] [L = E flat]
  int tones[] =  {   0, 1046, 138, 146, 155, 164, 174, 184, 195, 207, 220, 233, 246, 261, 293, 329, 349, 391, 440, 493, 523, 587, 659, 698, 783, 880, 987, 466, 740, 622, 415, 1046, 622u, 227};
  
  // play the tone corresponding to the note name
  for (int i = 0; i < 34; i++) {
    if (names[i] == note) {
      playTone(tones[i], duration);
    }
  }
}

void setup() {
  pinMode(speakerPin, OUTPUT);
  pinMode(button, INPUT);
  digitalWrite(button, HIGH);
}

void loop() 
{
  if (digitalRead(button) == LOW)
  (
  for(int i = 0; i < length; i++)
  {
    if (notes[i] == ' ')
    {
      delay(beats[i] * tempo); // rest
    } 
    else 
    {
      playNote(notes[i], beats[i] * tempo);
    }
    
    // pause between notes
    delay(tempo / 2); 
  }
  else if(digitalRead(button) == HIGH)
  {
    delay(100)
  }
}

and here's the speaker code

#include "pitches.h"
#define Num_Notes 3
int pot = A0;
int speaker = 13;
int sensorValue = 0;


const int notes[Num_Notes] = 
{
  NOTE_E7, NOTE_E7, 0 };

const int beat[Num_Notes] = 
{
  1, 1, 4 };
  
const int beat_length = 300;


void setup() {
  Serial.begin(9600);
  pinMode(speaker, OUTPUT);
  pinMode(pot, INPUT);
}

void loop()
{
  sensorValue = analogRead(pot);
  Serial.println(sensorValue);
  int sensorReading = analogRead(A0);
  
  if (sensorValue >= 400 && sensorValue <= 800)
  {
    tone(13, NOTE_C4, 2000);
  }
  else if (sensorValue >= 801 && sensorValue <= 820) 
  {
  tone(13, NOTE_E7, 200);
  delay (200);
  tone(13, NOTE_E7, 200);
  noTone (speaker);
  }
  delay(1);
}

any help or even points in the right direction would be great, it seems to be one thing to learn a specific range of functions but then applying them to other code seems to be another

i get an expected primary function before "for" error

Because on the line before you have a left bracket ( not a left brace {

Having fixed that you still have a problem with a later else function due to miss counting the braces or simply getting the way they are nested wrong. There is a semicolon missing from the delay function call.

The second code has no "pitches.h" file to include on my machine.

here's the pitch.h file

thanks for the info on the button maria code twas enough to get me back tinkering with it
got it to play only when the button is pushed however when the button is released it continues to play
think i just need to do some more tinkering with it and watch my braces

#define NOTE_B0  31
#define NOTE_C1  33
#define NOTE_CS1 35
#define NOTE_D1  37
#define NOTE_DS1 39
#define NOTE_E1  41
#define NOTE_F1  44
#define NOTE_FS1 46
#define NOTE_G1  49
#define NOTE_GS1 52
#define NOTE_A1  55
#define NOTE_AS1 58
#define NOTE_B1  62
#define NOTE_C2  65
#define NOTE_CS2 69
#define NOTE_D2  73
#define NOTE_DS2 78
#define NOTE_E2  82
#define NOTE_F2  87
#define NOTE_FS2 93
#define NOTE_G2  98
#define NOTE_GS2 104
#define NOTE_A2  110
#define NOTE_AS2 117
#define NOTE_B2  123
#define NOTE_C3  131
#define NOTE_CS3 139
#define NOTE_D3  147
#define NOTE_DS3 156
#define NOTE_E3  165
#define NOTE_F3  175
#define NOTE_FS3 185
#define NOTE_G3  196
#define NOTE_GS3 208
#define NOTE_A3  220
#define NOTE_AS3 233
#define NOTE_B3  247
#define NOTE_C4  262
#define NOTE_CS4 277
#define NOTE_D4  294
#define NOTE_DS4 311
#define NOTE_E4  330
#define NOTE_F4  349
#define NOTE_FS4 370
#define NOTE_G4  392
#define NOTE_GS4 415
#define NOTE_A4  440
#define NOTE_AS4 466
#define NOTE_B4  494
#define NOTE_C5  523
#define NOTE_CS5 554
#define NOTE_D5  587
#define NOTE_DS5 622
#define NOTE_E5  659
#define NOTE_F5  698
#define NOTE_FS5 740
#define NOTE_G5  784
#define NOTE_GS5 831
#define NOTE_A5  880
#define NOTE_AS5 932
#define NOTE_B5  988
#define NOTE_C6  1047
#define NOTE_CS6 1109
#define NOTE_D6  1175
#define NOTE_DS6 1245
#define NOTE_E6  1319
#define NOTE_F6  1397
#define NOTE_FS6 1480
#define NOTE_G6  1568
#define NOTE_GS6 1661
#define NOTE_A6  1760
#define NOTE_AS6 1865
#define NOTE_B6  1976
#define NOTE_C7  2093
#define NOTE_CS7 2217
#define NOTE_D7  2349
#define NOTE_DS7 2489
#define NOTE_E7  2637
#define NOTE_F7  2794
#define NOTE_FS7 2960
#define NOTE_G7  3136
#define NOTE_GS7 3322
#define NOTE_A7  3520
#define NOTE_AS7 3729
#define NOTE_B7  3951
#define NOTE_C8  4186
#define NOTE_CS8 4435
#define NOTE_D8  4699
#define NOTE_DS8 4978

Once it starts playing it will continue to play until the end.
Do you want it to,stop immediately you release the button? For that you will have to test the push button after each note and break out of your loop if you find it has been released.

that was the general idea

its kind of a warm up idea i have for the breathalyzer dictating that the code only runs when a button is being held and stops if let go, also i want to be able to reset the loop if the pressure sensor detects that the breath rate has been exceeded before the required reading time has been reached

was thinking i could use a while command

while (digitalRead (button) == HIGH)
{
noTone(speakerPin)
}

is there a "simple" way in doing what you suggest without having to code each tone or could i write it into the array?

yet again apologies for my absolute noobieness this is my first experience with coding and i seem to have been thrown into the deep end

so i spent the night trying to implement a non blocked architecture type code and example which was put up by Robin2

basically i have a binary thermometer that i coded the other week with simple if and else commands

int ledPin1= 13;
int ledPin2= 12;
int ledPin3= 11;
int ledPin4= 10;
int ledPin5= 9;
int sensorValue = 0;

void setup() {
  pinMode(ledPin1, OUTPUT);
  pinMode(ledPin2, OUTPUT);
  pinMode(ledPin3, OUTPUT);
  pinMode(ledPin4, OUTPUT);
  pinMode(ledPin5, OUTPUT);
}

void loop(){
  sensorValue = analogRead(A0);
      Serial.println(sensorValue);
  
  if (sensorValue <= 37)//18
  {
    digitalWrite(ledPin1, HIGH);
    digitalWrite(ledPin2, LOW);
    digitalWrite(ledPin3, LOW);
    digitalWrite(ledPin4, HIGH);
    digitalWrite(ledPin5, LOW);
  }
  else if (sensorValue > 37 && sensorValue <= 39)//19
  {
    digitalWrite(ledPin1, HIGH);
    digitalWrite(ledPin2, LOW);
    digitalWrite(ledPin3, LOW);
    digitalWrite(ledPin4, HIGH);
    digitalWrite(ledPin5, HIGH);
  }
  else if (sensorValue > 39 && sensorValue <= 41)//20
  {
    digitalWrite(ledPin1, HIGH);
    digitalWrite(ledPin2, LOW);
    digitalWrite(ledPin3, HIGH);
    digitalWrite(ledPin4, LOW);
    digitalWrite(ledPin5, LOW);
  }
  else if (sensorValue > 41 && sensorValue <= 43)//21
  {
    digitalWrite(ledPin1, HIGH);
    digitalWrite(ledPin2, LOW);
    digitalWrite(ledPin3, HIGH);
    digitalWrite(ledPin4, LOW);
    digitalWrite(ledPin5, HIGH);
  }
  else if (sensorValue > 43 && sensorValue <= 45)//22
  {
    digitalWrite(ledPin1, HIGH);
    digitalWrite(ledPin2, LOW);
    digitalWrite(ledPin3, HIGH);
    digitalWrite(ledPin4, HIGH);
    digitalWrite(ledPin5, LOW);
  }
  else if (sensorValue > 45 && sensorValue <= 47)//23
  {
    digitalWrite(ledPin1, HIGH);
    digitalWrite(ledPin2, LOW);
    digitalWrite(ledPin3, HIGH);
    digitalWrite(ledPin4, HIGH);
    digitalWrite(ledPin5, HIGH);
  }
  else if (sensorValue > 47 && sensorValue <= 49)//24
  {
    digitalWrite(ledPin1, HIGH);
    digitalWrite(ledPin2, HIGH);
    digitalWrite(ledPin3, LOW);
    digitalWrite(ledPin4, LOW);
    digitalWrite(ledPin5, LOW);
  }
  else if (sensorValue > 49 && sensorValue <= 52)//25
  {
    digitalWrite(ledPin1, HIGH);
    digitalWrite(ledPin2, HIGH);
    digitalWrite(ledPin3, LOW);
    digitalWrite(ledPin4, LOW);
    digitalWrite(ledPin5, HIGH);
  }
  else if (sensorValue > 52 && sensorValue <= 54)//26
  {
    digitalWrite(ledPin1, HIGH);
    digitalWrite(ledPin2, HIGH);
    digitalWrite(ledPin3, LOW);
    digitalWrite(ledPin4, HIGH);
    digitalWrite(ledPin5, LOW);
  }
  else if (sensorValue > 54 && sensorValue <= 56)//27
  {
    digitalWrite(ledPin1, HIGH);
    digitalWrite(ledPin2, HIGH);
    digitalWrite(ledPin3, LOW);
    digitalWrite(ledPin4, HIGH);
    digitalWrite(ledPin5, HIGH);
  }
  else if (sensorValue > 56 && sensorValue <= 58)//28
  {
    digitalWrite(ledPin1, HIGH);
    digitalWrite(ledPin2, HIGH);
    digitalWrite(ledPin3, HIGH);
    digitalWrite(ledPin4, LOW);
    digitalWrite(ledPin5, LOW);
  }
  else if (sensorValue > 58 && sensorValue <= 60)//29
  {
    digitalWrite(ledPin1, HIGH);
    digitalWrite(ledPin2, HIGH);
    digitalWrite(ledPin3, HIGH);
    digitalWrite(ledPin4, LOW);
    digitalWrite(ledPin5, HIGH);
  }
  else if (sensorValue > 60 && sensorValue <= 62)//30
  {
    digitalWrite(ledPin1, HIGH);
    digitalWrite(ledPin2, HIGH);
    digitalWrite(ledPin3, HIGH);
    digitalWrite(ledPin4, HIGH);
    digitalWrite(ledPin5, LOW);
  }
  delay (500);
}

ive tried to set up the code that each reading acts like its own function thus giving me an idea of how to integrate separate parts into one full working bit of code, i got some error when compiling and after a bit of head scratching i got the code to compile. However when uploaded the code dosnt implement as it should instead some of led's are half on and some are full on but not the right ones

const int ledPin1= 13;
const int ledPin2= 12;
const int ledPin3= 11;
const int ledPin4= 10;
const int ledPin5= 9;
int sensorValue = 0;

// ==================variables==================================== 

int degrees_21 = LOW;
int degrees_22 = LOW;
int degrees_23 = LOW;
int degrees_24 = LOW;
int degrees_25 = LOW;
int degrees_26 = LOW;

//===================================================================

void setup()

{
  pinMode(ledPin1, OUTPUT);
  pinMode(ledPin2, OUTPUT);
  pinMode(ledPin3, OUTPUT);
  pinMode(ledPin4, OUTPUT);
  pinMode(ledPin5, OUTPUT);
  
  Serial.begin(9600);
}

//========================================================================

void loop()

{
  sensorValue = analogRead(A0);
  Serial.println(sensorValue);
  

updatedegrees_21(); 
updatedegrees_22(); 
updatedegrees_23(); 
updatedegrees_24(); 
updatedegrees_25(); 
updatedegrees_26(); 


}

//=====================================================================

void updatedegrees_21()
{
  if (degrees_21 == LOW)
  {
    if (sensorValue <= 43)//21
    degrees_21 = HIGH;
  }  
  else 
   {
     (degrees_21 = LOW);
   }  
        if (degrees_21 == HIGH)  
      {  
          digitalWrite(ledPin1, HIGH);
          digitalWrite(ledPin2, LOW);
          digitalWrite(ledPin3, HIGH);
          digitalWrite(ledPin4, LOW);
          digitalWrite(ledPin5, HIGH);
      }
   
}

  
//==================================================================

void updatedegrees_22()
{
  if (degrees_22 == LOW)
  {
    if (sensorValue > 43 && sensorValue <= 45)//22
    degrees_22 = HIGH;
  }
  else 
   {
     (degrees_22 = LOW);
   }
        if (degrees_22 == HIGH)
      {
          digitalWrite(ledPin1, HIGH);
          digitalWrite(ledPin2, LOW);
          digitalWrite(ledPin3, HIGH);
          digitalWrite(ledPin4, HIGH);
          digitalWrite(ledPin5, LOW);
       }
   
}
      
//======================================================================

void updatedegrees_23()
{
  if (degrees_23 == LOW)
  {
    if (sensorValue > 45 && sensorValue <= 47)
     degrees_23 = HIGH;
   }
  else 
   {
     (degrees_23 = LOW);
   } 
        if (degrees_23 == HIGH); 
        {
            digitalWrite(ledPin1, HIGH);
            digitalWrite(ledPin2, LOW);
            digitalWrite(ledPin3, HIGH);
            digitalWrite(ledPin4, HIGH);
            digitalWrite(ledPin5, HIGH);
        }
    
}
  
//==========================================================================

void updatedegrees_24()
{
  if (degrees_24 == LOW)
  {
    if (sensorValue > 45 && sensorValue <= 47)
     degrees_24 = HIGH;
   }
  else 
   {
     (degrees_24 = LOW);
   } 
        if (degrees_24 == HIGH); 
        {
            digitalWrite(ledPin1, HIGH);
            digitalWrite(ledPin2, HIGH);
            digitalWrite(ledPin3, LOW);
            digitalWrite(ledPin4, LOW);
            digitalWrite(ledPin5, LOW);
        }
    
}

//========================================================================

void updatedegrees_25()
{
  if (degrees_25 == LOW)
  {
    if (sensorValue > 49 && sensorValue <= 52)//25
    degrees_25 == HIGH;
  }
  else 
   {
     (degrees_25 = LOW);
   }
      if (degrees_25 == HIGH) 
      {
          digitalWrite(ledPin1, HIGH);
          digitalWrite(ledPin2, HIGH);
          digitalWrite(ledPin3, LOW);
          digitalWrite(ledPin4, LOW);
          digitalWrite(ledPin5, HIGH);
      }
  
}

//===========================================================================

void updatedegrees_26()
{
  if (degrees_26 == LOW)
  {
     if (sensorValue > 52 && sensorValue <= 54)
     degrees_26 = HIGH;
  }
  else 
   {
     (degrees_26 = LOW);
   }
        if (degrees_26 == HIGH)
      {
          digitalWrite(ledPin1, HIGH);
          digitalWrite(ledPin2, HIGH);
          digitalWrite(ledPin3, LOW);
          digitalWrite(ledPin4, HIGH);
          digitalWrite(ledPin5, LOW);
      }
  
}

bit of a pain as i feel like im damn near close to getting it to work and i think it probably has been a good exercise for my current task

i have tried it without the else function but still get some functioning errors

There is a general rule which says if you find yourself writing almost the same code over and over you are doing it wrong.
This means you should let variables drive the code more. That will make it much easier to follow.
If an LED is half on that means you are turning it on and off rapidly so check your logic.
Learning how to use arrays will cut the whole of your code down to about a dozen lines.

Please use the code tag the # icon and. It the quote icon next to it. Please modify that last post.

cool thanks for the advice
im gona have to go back to my exercises on variables and arrays and use that then to see if there is some way of fitting what i want into that format

il post again when i get there

This might help you with arrays
http://www.thebox.myzen.co.uk/Tutorial/Arrays.html

thank you very much mike!

great site, well worded and very informative without melting the brain

thanks for taking the time to do this

i reckon il be spending alot of time reading over your stuff and doing tutorials as i get to grips with programming, want to end up making a robotic arm and a 3d printer at some stage

this will really give me the a head start for next year as i start to get into process control and automation