0% found this document useful (0 votes)
19 views2 pages

Speed Control

This code controls a vehicle's speed and distance using an ultrasonic distance sensor, potentiometer to control acceleration, and LCD display. It defines pins for components, initializes the LCD and distance sensor, and enters a main loop that: 1) measures distance, 2) sets speed limit based on distance, 3) reads potentiometer for acceleration value, 4) displays values on LCD, and 5) controls motor direction and speed accordingly. It also includes functions to move the vehicle forward and stop it.

Uploaded by

chinju mdas
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
Download as txt, pdf, or txt
0% found this document useful (0 votes)
19 views2 pages

Speed Control

This code controls a vehicle's speed and distance using an ultrasonic distance sensor, potentiometer to control acceleration, and LCD display. It defines pins for components, initializes the LCD and distance sensor, and enters a main loop that: 1) measures distance, 2) sets speed limit based on distance, 3) reads potentiometer for acceleration value, 4) displays values on LCD, and 5) controls motor direction and speed accordingly. It also includes functions to move the vehicle forward and stop it.

Uploaded by

chinju mdas
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1/ 2

#include <Wire.

h>
#include <LiquidCrystal_I2C.h>
#include <HCSR04.h>
UltraSonicDistanceSensor distanceSensor(D5, D6);
LiquidCrystal_I2C lcd(0x27,16,2);

#define pwm_mot D4
#define m11 D7
#define m12 D8
#define push_sw D3
#define acc_pot A0

unsigned long previousMillis1 = 0;


unsigned int interval1 = 1000, sec=0,alert=0,sec1=0;
unsigned int pot_read=0,speed_lim,dis=0,moving=0,acc;

void forward();
void stp();
void setup()
{
Serial.begin(9600);
lcd.begin();

pinMode(acc_pot,INPUT);
pinMode(pwm_mot,OUTPUT);
pinMode(m11, OUTPUT);
pinMode(m12, OUTPUT);
pinMode(push_sw,INPUT_PULLUP);

digitalWrite(m11,LOW);digitalWrite(m12,LOW);

lcd.setCursor(0,0); lcd.print(" Vehicle ");


lcd.setCursor(0,1); lcd.print(" Speed Control ");
delay(2000);
//***********************************************************

lcd.setCursor(0, 0);lcd.print("D: acc: ");


lcd.setCursor(0, 1);lcd.print("Veh: ");

analogWrite(pwm_mot,1023);

}
void loop()
{
timer();
if (digitalRead(push_sw)==0 && moving==0) { delay(300); moving=1; }
else if(digitalRead(push_sw)==0 && moving==1) { delay(300); moving=0; }
if(moving==1) { forward(); } else if (moving==0) { stp(); }

dis=distanceSensor.measureDistanceCm();if(dis<2){dis=2;}else if(dis>100)
{dis=100;}
if (dis<10){lcd.setCursor(3, 0);lcd.print(" ");}
else if(dis<100){lcd.setCursor(4, 0);lcd.print(" ");}
lcd.setCursor(2, 0);lcd.print(dis);

if ( dis>2 && dis<20 && moving==1 ) { speed_lim=0; stp();}


else if ( dis>19 && dis<30 && moving==1 ) { speed_lim=750; }
else if ( dis>29 && dis<40 && moving==1 ) { speed_lim=950; }
else if ( dis>39 && dis<101&& moving==1 ) { speed_lim=1023; }

pot_read = analogRead(acc_pot);
if(pot_read>speed_lim){pot_read=speed_lim;}
acc = map(pot_read, 0, 1023, 0, 100);
if (acc<10) {lcd.setCursor(13, 0);lcd.print(" "); }
else if(acc<100){lcd.setCursor(14, 0); lcd.print(" "); }
lcd.setCursor(12, 0); lcd.print(acc);

analogWrite(pwm_mot,pot_read);

delay(50);
Serial.print("moving:");Serial.print(moving);Serial.print("
acc:");Serial.println(acc);

void timer()
{ unsigned long currentMillis = millis();
if ((unsigned long)(currentMillis - previousMillis1) >= interval1) // check for
rollover delay 1
{
sec++;sec1++;
previousMillis1 = currentMillis;
}
}
void forward()
{
lcd.setCursor(5,1); lcd.print("RUN");
digitalWrite(m11,HIGH);digitalWrite(m12,LOW);
}

void stp()
{
lcd.setCursor(5,1); lcd.print("STP");
digitalWrite(m11,LOW); digitalWrite(m12,LOW);
}

You might also like