I am building a simple parking assistant with a ping sensor and a RGB LED as the indicator and everything works great when running on an arduino uno. When I try the adapted code on the ATtiny85 with a breadboard powersupply, there is a severe lag in the speed of the pings. It slows from a ping every 10 milliseconds or so to about one ping each second and each ping causes the LED to flicker. Also, the tone of the ping changes from a barely audible click to more of a buzz. Im just to new to circuits to no whats going on. Another bit that seems strange is that in certain parts of the code, it pings at the right speed and the LED seems to behave as expected (it still could be flickering, just too fast to notice). It seems to be slow when inside a nested if statement, check code below for when exactly it slows down. Forgive my code as it may be somewhat sloppy, I only took one semester of java.
int gLED = 3;
int rLED = 4;
int trigger = 0;
int echo = 1;
int buttonPin = 2;
int duration = 0;
int distance = 0;
int set = 0;
int upper = 0;
int lower = 0;
int range = 4; //range is +/- so it is double the value
int count = 0;
boolean lastButton = LOW;
boolean currentButton = LOW;
boolean ledOn = false;
int previous = 0;
int current = 0;
void setup()
{
pinMode(gLED, OUTPUT);
pinMode(rLED, OUTPUT);
pinMode(trigger, OUTPUT);
pinMode(echo, INPUT);
pinMode(buttonPin, INPUT);
}
void loop()
{
digitalWrite(trigger, LOW); //reading distance in inches
delayMicroseconds(2);
digitalWrite(trigger, HIGH);
delayMicroseconds(10);
digitalWrite(trigger, LOW);
duration = pulseIn(echo, HIGH);
if(duration == 0) duration = 29600; //if no distance is returned, set at maximum distance
distance = (duration/2)/74; //convert to inches
current = distance;
delay(10);
currentButton = debounce(lastButton);
if (lastButton == LOW && currentButton == HIGH) //when button pushed, set distance for parking
{
set = distance;
digitalWrite(gLED, HIGH); //flicker led to indicate set
delay(50);
digitalWrite(gLED,LOW);
delay(50);
digitalWrite(gLED, HIGH);
delay(50);
digitalWrite(gLED,LOW);
delay(50);
digitalWrite(gLED, HIGH);
delay(50);
digitalWrite(gLED,LOW);
delay(50);
digitalWrite(gLED, HIGH);
delay(50);
digitalWrite(gLED,LOW);
delay(50);
}
lastButton = currentButton; //reset button
upper = set + range; // +/- inch tolerance range
lower = set - range;
if(distance <= upper && distance >= lower)
{
if(previous == current) count++;
if(count < 200) //make 100 after debug
{
digitalWrite(gLED, HIGH); //turn on red LED "stop" this part is slow!
digitalWrite(rLED, LOW);
}
else
{
digitalWrite(gLED, HIGH); //turn off both LED, car is parked, there is no need to indicate
digitalWrite(rLED, HIGH);
if(current != previous) count = 0; //if car moves, count is reset and exits power save
}
}
else if(distance > upper)
{
if(distance >= set+80);
{
digitalWrite(rLED, HIGH); //pull up resistor inverts command, oops
digitalWrite(gLED, HIGH);
}
if(distance < set+80 && distance > set+40) //if distance is less than set + 80 inches
{
digitalWrite(rLED, HIGH); //green LED on "keep coming" this part is slow!
digitalWrite(gLED, LOW);
}
if(distance <= set+40 && distance > upper)
{
digitalWrite(rLED, LOW); //yellow "slow down" this part is slow!
digitalWrite(gLED, LOW);
}
}
else if(distance < lower)
{
digitalWrite(gLED, HIGH); // flicker red LED "too close, back up" this part works fine!
digitalWrite(rLED, LOW);
delay(100);
digitalWrite(rLED, HIGH);
delay(100);
}
previous = current;
}
boolean debounce(boolean last)
{
boolean current = digitalRead(buttonPin);
if (last != current)
{
delay(5);
current = digitalRead(buttonPin);
}
return current;
}