thank you for your comments guy this is not my homework
this is a project I am doing through uni and one of my classes is software engineering and this year we are doing it with Arduino's last term was Raspberry Pi
but thanks
not very good at programming
but any advice would be good to put me on the right path
Develop a prototype embedded system using the Arduino to implement an adaptive cruise control system. The basic specification is as follows:
A method of setting the desired distance. (HAVE THIS AND WORKING)
A button to confirm a new setting
Collision Warning (HAVE THIS AND WORKING)
Cancel setting/exit ACC
LCD displays mode and set-distance and the actual distance
Engine Acceleration (PWM output to LED) Brake Control (PWM output to LED)
#define trigPin 13
#define echoPin 12
const byte green1LED = 2;
const byte green2LED = 3;
const byte yellow1LED = 4;
const byte yellow2LED = 5;
const byte red1LED = 6;
const byte red2LED = 7;
const byte redPin = 10;
const byte greenPin = 11;
int readValue = 0;
double writeValue = 0;
const byte pushbutton = 9;
const byte piezo = 8;
const byte potentiometer = A0;
float actualDistance = 0;
float desiredDistance = 0;
int SensorValue =0;
int digitalValue =0;
void setup()
{
Serial.begin(9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(green1LED,OUTPUT);
pinMode(green2LED,OUTPUT);
pinMode(yellow1LED,OUTPUT);
pinMode(yellow2LED,OUTPUT);
pinMode(red1LED,OUTPUT);
pinMode(red2LED,OUTPUT);
pinMode(redPin,OUTPUT);
pinMode(greenPin,OUTPUT);
pinMode(piezo, OUTPUT);
pinMode(potentiometer, INPUT);
pinMode(pushbutton, INPUT);
//delay (10000);
attachInterrupt(digitalPinToInterrupt(pushbutton),flashRed, CHANGE);
}
void loop()
{
long duration, distance;
int sensorValue;
digitalWrite(trigPin, LOW);
delay(2);
digitalWrite(trigPin, HIGH);
delay(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration/2)/29.1;
Serial.print(distance);
Serial.println(" cm Distance");
//Serial.println(sensorValue);
if(distance > 300)
// theese are my actual distance
{
digitalWrite(2,HIGH);
digitalWrite(3,LOW);
digitalWrite(4,LOW);
digitalWrite(5,LOW);
digitalWrite(6,LOW);
digitalWrite(7,LOW);
//tone(piezo, 1000, 100);
//tone( type of pin or pin number, frequency in hertz, duration in milliseconds)
}
if(distance > 250 && distance < 300)
{
digitalWrite(2,LOW);
digitalWrite(3,HIGH);
digitalWrite(4,LOW);
digitalWrite(5,LOW);
digitalWrite(6,LOW);
digitalWrite(7,LOW);
//tone(piezo, 900, 800);
}
if(distance > 200 && distance < 250)
{
digitalWrite(2,LOW);
digitalWrite(3,LOW);
digitalWrite(4,HIGH);
digitalWrite(5,LOW);
digitalWrite(6,LOW);
digitalWrite(7,LOW);
//tone(piezo, 800, 1000);
}
if(distance < 150 && distance < 200)
{
digitalWrite(2,LOW);
digitalWrite(3,LOW);
digitalWrite(4,LOW);
digitalWrite(5,HIGH);
digitalWrite(6,LOW);
digitalWrite(7,LOW);
tone(piezo, 700, 600);
}
if(distance < 100 && distance < 150)
{
digitalWrite(2,LOW);
digitalWrite(3,LOW);
digitalWrite(4,LOW);
digitalWrite(5,LOW);
digitalWrite(6,HIGH);
digitalWrite(7,LOW);
tone(piezo, 600, 800);
}
if(distance < 50 && distance < 100)
{
digitalWrite(2,LOW);
digitalWrite(3,LOW);
digitalWrite(4,LOW);
digitalWrite(5,LOW);
digitalWrite(6,LOW);
digitalWrite(7,HIGH);
tone(piezo, 500, 1000);
}
delay(500);
attachInterrupt(digitalPinToInterrupt(pushbutton),flashRed CHANGE);
}
NOT WORKING FROM HERE DOWN__________________________________________________
void flashRed()
{
Serial.println(" Desired Distance ");
SensorValue = analogRead(potentiometer);
desiredDistance = SensorValue /3.14;
actualDistance = echoPin;
if (desiredDistance > actualDistance)
{
analogWrite(desiredDistance, acceleraterPin);//green light on
Serial.print(desiredDistance);
Serial.println(" Desired Distance ");
}
else (desiredDistance < actualDistance);
{
analogWrite(breakPin, desiredDistance); //red light on
Serial.print(actualDistance);
Serial.println(" actualDistance ");
}
{
readValue = analogRead(potentiometer);
Serial.println(SensorValue);
delay(500);
writeValue =(8./1023.)*readValue;
analogWrite(redPin,writeValue);
analogWrite(greenPin,writeValue);
Serial.print(readValue);
Serial.println(writeValue);
delay(5000);
}
}