Pages: [1]   Go Down
Author Topic: "Car-duino" Mk1  (Read 911 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hey all. Other than a little bit of HTML/PHP scripting this is my first real foray into programming. I'm working on what I call my "Car-duino". Essentially, I would like it to run a couple different things:

-Two servos being used to operate a retractable front license plate
-Brake lights and progressive turn signals

While it does work, the use of delay() is causing some issues, and I can't for the life of me figure out how to properly replace the timing using millis(). Also, I'm sure there's a better way to organize it rather than using all the if/else statements but that'll take some more learning on my part.

The main problem I'm having with the delay function is the amount of time it takes for the brake lights to turn on or off. From what I've read it seems like I should be able to use millis() instead, or possibly even an interrupt.

Code:

#include <Servo.h>
 
Servo servoA;  
Servo servoB;
 
const int lpPin = 0;
const int bPin = 3;
int switchState = 0;
int brakeState = 0;
int lastswitchState = 0;

const int llaPin = 9;
const int llbPin = 10;
const int llcPin = 11;

const int rlaPin = 8;  
const int rlbPin = 7;
const int rlcPin = 6;

const int lsPin = 2;
const int rsPin = 1;

int rsState = 0;
int lsState = 0;

void setup()
{
servoA.attach(12);
servoB.attach(13);
pinMode(lpPin, INPUT);
servoA.write(0);
servoB.write(0);
 
pinMode(lsPin, INPUT);
pinMode(rsPin, INPUT);
pinMode(bPin, INPUT);
pinMode(llaPin, OUTPUT);    
pinMode(llbPin, OUTPUT);  
pinMode(llcPin, OUTPUT);    
pinMode(rlaPin, OUTPUT);    
pinMode(rlbPin, OUTPUT);    
pinMode(rlcPin, OUTPUT);    

}
 
void loop() {

switchState = digitalRead(lpPin);

lsState = digitalRead(lsPin);
rsState = digitalRead(rsPin);
brakeState = digitalRead(bPin);

if (brakeState == LOW){

if (lsState == HIGH && rsState == LOW)
{

digitalWrite(llaPin, HIGH);  
delay(750);                  
digitalWrite(llbPin, HIGH);    
delay(750);            
digitalWrite(llcPin, HIGH);
delay(750);
digitalWrite(llaPin, LOW);
digitalWrite(llbPin, LOW);
digitalWrite(llcPin, LOW);
delay(750);
}
  
else if (rsState == HIGH && lsState == LOW)
{

digitalWrite(rlaPin, HIGH);  
delay(750);                  
digitalWrite(rlbPin, HIGH);    
delay(750);            
digitalWrite(rlcPin, HIGH);
delay(750);
digitalWrite(rlaPin, LOW);
digitalWrite(rlbPin, LOW);
digitalWrite(rlcPin, LOW);
delay(750);
}

else if (lsState == HIGH && rsState == HIGH)
{
digitalWrite(rlaPin, HIGH);  
digitalWrite(llaPin, HIGH);  
delay(750);                  
digitalWrite(rlbPin, HIGH);  
digitalWrite(llbPin, HIGH);  
delay(750);            
digitalWrite(rlcPin, HIGH);
digitalWrite(llcPin, HIGH);  
delay(750);
digitalWrite(rlaPin, LOW);
digitalWrite(rlbPin, LOW);  
digitalWrite(rlcPin, LOW);
digitalWrite(llaPin, LOW);  
digitalWrite(llbPin, LOW);
digitalWrite(llcPin, LOW);  
delay(750);
}

}

if (brakeState == HIGH) {

if (lsState == HIGH && rsState == LOW)
{

digitalWrite(rlaPin, HIGH);
digitalWrite(rlbPin, HIGH);
digitalWrite(rlcPin, HIGH);
digitalWrite(llaPin, HIGH);  
delay(750);                  
digitalWrite(llbPin, HIGH);    
delay(750);            
digitalWrite(llcPin, HIGH);
delay(750);
digitalWrite(llaPin, LOW);
digitalWrite(llbPin, LOW);
digitalWrite(llcPin, LOW);
delay(750);
}
  
else if (rsState == HIGH && lsState == LOW)
{

digitalWrite(llaPin, HIGH);
digitalWrite(llbPin, HIGH);
digitalWrite(llcPin, HIGH);
digitalWrite(rlaPin, HIGH);  
delay(750);                  
digitalWrite(rlbPin, HIGH);    
delay(750);            
digitalWrite(rlcPin, HIGH);
delay(750);
digitalWrite(rlaPin, LOW);
digitalWrite(rlbPin, LOW);
digitalWrite(rlcPin, LOW);
delay(750);
}

else
{
digitalWrite(rlaPin, HIGH);
digitalWrite(rlbPin, HIGH);  
digitalWrite(rlcPin, HIGH);
digitalWrite(llaPin, HIGH);  
digitalWrite(llbPin, HIGH);
digitalWrite(llcPin, HIGH);
}

}


else {
digitalWrite(rlaPin, LOW);
digitalWrite(rlbPin, LOW);  
digitalWrite(rlcPin, LOW);
digitalWrite(llaPin, LOW);  
digitalWrite(llbPin, LOW);
digitalWrite(llcPin, LOW);
}  



if (switchState != lastswitchState)
{
if (switchState == HIGH)
{
servoA.write(180);
delay(4000);
servoB.write(90);
}

if (switchState == LOW)
{
servoB.write(0);
delay(2000);
servoA.write(0);
}
}
lastswitchState = switchState;


}

As you can see it's a bit of a mess but any pointers would be greatly appreciated. Thanks!
Logged

Central MN, USA
Offline Offline
Tesla Member
***
Karma: 64
Posts: 6889
Phi_prompt, phi_interfaces, phi-2 shields, phi-panels
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
-Two servos being used to operate a retractable front license plate

Are you sure that is legal in your country/region?
Logged


Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
-Two servos being used to operate a retractable front license plate

Are you sure that is legal in your country/region?
The rectractability is legal, and the plate will be out while on public roads. However, I'm going to be taking the car to the track and shows quite often and it'd be cool to just flip a switch and fold it away.
Logged

Dubuque, Iowa, USA
Offline Offline
Edison Member
*
Karma: 29
Posts: 2243
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The main problem I'm having with the delay function is the amount of time it takes for the brake lights to turn on or off. From what I've read it seems like I should be able to use millis() instead, or possibly even an interrupt.

...or a library, like SimpleTimer.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The main problem I'm having with the delay function is the amount of time it takes for the brake lights to turn on or off. From what I've read it seems like I should be able to use millis() instead, or possibly even an interrupt.

...or a library, like SimpleTimer.

That looks perfect! Thanks, I'll give that a shot.
Logged

Pages: [1]   Go Up
Jump to: