Go Down

Topic: programming not working (Read 1 time) previous topic - next topic

oris

/*
Ultrasonic Backup Sensor
By David Halloran

Summary:
This program was intended for an automobile backup sensor and takes reeadings from a
PING))) ultrasonic sensor and alerts you to an objects distance in four ways:
-Numeric readout
-Color coded zones
-Bar graph
-Audible tone
*/




//--------------------- include the library code: ---------------------------


#include <LiquidCrystal.h>
#include <Ping.h>


//--------------------------- Initialize --------------------------------------


LiquidCrystal lcd(7, 8, 9, 10, 11, 12);
Ping ping = Ping(13,0,0);
float In = 0;
int green = 2;
int yellow = 3;
int red = 4;
float low = 72;  //Set distance for warnings to start
int med = 0;
int high = 0;
int Inb = 0;
float space =0;
int flash = 0;
char* unit[]={"Feet", "INCHES"};
int u = 1;

//Special Characters for Bar Graph
byte line[8] = {
   B11111,
   B00000,
   B00000,
   B00000,
   B00000,
   B00000,
   B00000,
   B11111
};
byte block[8] = {
   B11111,
   B11111,
   B11111,
   B11111,
   B11111,
   B11111,
   B11111,
   B11111
};
byte car[8] = {
   B11111,
   B01100,
   B11110,
   B00111,
   B00111,
   B11110,
   B01100,
   B11111
};
byte car1[8] = {
   B11111,
   B00110,
   B11111,
   B11000,
   B11000,
   B11111,
   B00110,
   B11111
};




//----------------------------- Setup -------------------------------------

void setup() {
  lcd.begin(16, 2);
  pinMode(6, OUTPUT);
  pinMode(green, OUTPUT);
  pinMode(yellow, OUTPUT);
  pinMode(red, OUTPUT);
  lcd.createChar(0, line);
  lcd.createChar(1, block);
  lcd.createChar(2, car1);
  lcd.createChar(3, car);
  med = (low/3)*2;          // This breaks your warning distance into three even zones
  high = low/3;             // meaning the program will automatically scale if you
                            // change the start of the first warning zone   
}


//------------------------------ Loop -----------------------------------


void loop() {
 
 
  ping.fire();
  In = ping.inches();
  Inb = In;
  if(In > 12){           //If measurement is greater than 12 inches use feet
    space = In/12;
    u = 0;
  }else{                 //else use inches
    space = In;
    u = 1;
  }
  lcd.clear();
 

  if(In < high){       // Is the object in the Red Zone?
    redLight();
    distance();
    bar();
   }
   else if ((In >= high) && (In < med)){  // Is the object in the Yellow Zone?
     yellowLight();
     distance();
     bar();
   }
   else if ((In >= med) && (In < low)){  // Is the object in the Green Zone?
     greenLight();
     distance();
     bar();
   }
   else if (In > low){           // If no object are in the set range print Drive Safely
    lcd.setCursor(5, 0);
    lcd.print("Drive");
    lcd.setCursor(5, 1);
    lcd.print("Safely");
    noTone(6);               //Tone and lights off
    digitalWrite(red, LOW);
    digitalWrite(yellow, LOW);
    digitalWrite(green, LOW);
  }
   
  delay(200);

}


//---------------------- Red Zone ----------------------------


void redLight(){
  tone(6,1440);
  digitalWrite(green, HIGH);
  digitalWrite(yellow, HIGH);
  digitalWrite(red, HIGH);
}

//--------------------- Yellow Zone ----------------------------

void yellowLight(){
  tone(6,880);
  digitalWrite(green, HIGH);
  digitalWrite(yellow, HIGH);
  digitalWrite(red, LOW);
}

//-------------------- Green Zone ----------------------------

void greenLight(){
  tone(6,440);
  digitalWrite(green, HIGH);
  digitalWrite(red, LOW);
  digitalWrite(yellow, LOW);
}

//------------------- Print Distance -----------------------

void distance(){
  lcd.setCursor(0, 0);
    lcd.print("Distance: ");
    lcd.setCursor(10, 0);
    lcd.print(space);
    lcd.setCursor(10, 1);
    lcd.print(unit);
}

//--------------------- Bar Graph ---------------------------


void bar(){
    if (Inb >= ((double)5/(double)6*low)){   //Farthest Away
    lcd.setCursor(0,1);
    lcd.write(2);
    lcd.write(3);
    lcd.write(0);
    lcd.write(0);
    lcd.write(0);
    lcd.write(0);
    lcd.write(0);
    lcd.write(1);
  }
    else if ((Inb < ((double)5/(double)6*low)) && (Inb >= ((double)4/(double)6*low))){
    lcd.setCursor(0,1);
    lcd.write(0);
    lcd.write(2);
    lcd.write(3);
    lcd.write(0);
    lcd.write(0);
    lcd.write(0);
    lcd.write(0);
    lcd.write(1);
  }
    else if ((Inb < ((double)4/(double)6*low)) && (Inb >= ((double)3/(double)6*low))){
    lcd.setCursor(0,1);
    lcd.write(0);
    lcd.write(0);
    lcd.write(2);
    lcd.write(3);
    lcd.write(0);
    lcd.write(0);
    lcd.write(0);
    lcd.write(1);
  }
    else if ((Inb < ((double)3/(double)6*low)) && (Inb >= ((double)2/(double)6*low))){
    lcd.setCursor(0,1);
    lcd.write(0);
    lcd.write(0);
    lcd.write(0);
    lcd.write(2);
    lcd.write(3);
    lcd.write(0);
    lcd.write(0);
    lcd.write(1);
  }
    else if ((Inb < ((double)2/(double)6*low)) && (Inb >= ((double)1/(double)6*low))){
    lcd.setCursor(0,1);
    lcd.write(0);
    lcd.write(0);
    lcd.write(0);
    lcd.write(0);
    lcd.write(2);
    lcd.write(3);
    lcd.write(0);
    lcd.write(1);
  }
  else if (Inb < ((double)1 / (double)6 * low)){  //Closest also makes the graph flash
    if (flash < 1){
      flash = flash++;
    lcd.setCursor(0,1);
    lcd.write(0);
    lcd.write(0);
    lcd.write(0);
    lcd.write(0);
    lcd.write(0);
    lcd.write(2);
    lcd.write(3);
    lcd.write(1);
    tone(6, 2000);
    }
    else{
      flash = 0;
      lcd.setCursor(0,1);
    lcd.write(0);
    lcd.write(0);
    lcd.write(0);
    lcd.write(0);
    lcd.write(0);
    lcd.write(0);
    lcd.write(0);
    lcd.write(1);
    tone(6, 2000);
  }
}
}

robtillaart

please use code tags ... see sticky note @ top of forum
Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Go Up