hello!
i’m working on a piece where there are two sets of leds that fade and brighten based on proximity. the code is basic and works - however it seems that i have a couple of issues:
- the ping readings are slightly erratic and cause the LEDs to flicker
- the fade in seems to be faster than the fade out (could be perception
following is my code. any suggestions on betters ways to do things to avoid either of the above issues or just improve performance would be hugely helpful!
const int led_01 = 10; // LED connected to PWM 10
const int led_02 = 9; // LED connected to PWM 9
const int led_03 = 11; // LED connected to PWN 11
const int led_04 = 5; // LED connected to PWN 5
const int led_05 = 3; // LED connected to PWN 3
const int led_06 = 6; // LED connected to PWN 6
const int pingPin = 2;
boolean state_01 = false;
boolean state_02 = false;
int i = 0;
int j = 0;
void setup() // run once, when the sketch starts
{
pinMode(led_01, OUTPUT);
pinMode(led_02, OUTPUT);
pinMode(led_03, OUTPUT);
pinMode(led_04, OUTPUT);
pinMode(led_05, OUTPUT);
pinMode(led_06, OUTPUT);
Serial.begin(9600);
}
void loop(){
//The PING))) is triggered by a HIGH pulse of 2 or more microseconds.
// Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
/Serial.print("state_01 is ");
Serial.print(state_01, DEC);
Serial.println();/
long duration, inches, cm, avgInches;
pinMode(pingPin, OUTPUT); // sets the digital pin as output
digitalWrite(pingPin, LOW);
delayMicroseconds(2);
digitalWrite(pingPin, HIGH);
delayMicroseconds(5);
digitalWrite(pingPin, LOW);
// The same pin is used to read the signal from the PING))): a HIGH
// pulse whose duration is the time (in microseconds) from the sending
// of the ping to the reception of its echo off of an object.
pinMode(pingPin, INPUT);
duration = pulseIn(pingPin, HIGH);
// convert the time into a distance
inches = microsecondsToInches(duration);
delayMicroseconds(200);
if (inches < 15){
state_01 = true;
state_02 = false;
}
else if (inches >= 15 && inches < 48){
state_01 = false;
state_02 = true;
}
else{
state_01 = false;
state_02 = false;
}
if (state_01 == true){
if (i < 255){
i++;
analogWrite(led_01, i);
analogWrite(led_03, i);
analogWrite(led_05, i);
analogWrite(led_06, i);
}
}
else if (state_01 == false){
if (i >0){
i–;
analogWrite(led_01, i);
analogWrite(led_03, i);
analogWrite(led_05, i);
analogWrite(led_06, i);
}
}
if (state_02 == true){
if (j < 40){
j++;
analogWrite(led_02, j);
analogWrite(led_04, j);
}
}
else if (state_02 == false){
if (j >0){
j–;
analogWrite(led_02, j);
analogWrite(led_04, j);
}
}
}
long microsecondsToInches(long microseconds){
// According to Parallax’s datasheet for the PING))), there are
// 73.746 microseconds per inch (i.e. sound travels at 1130 feet per
// second). This gives the distance travelled by the ping, outbound
// and return, so we divide by 2 to get the distance of the obstacle.
// See: http://www.parallax.com/dl/docs/prod/acc/28015-PING-v1.3.pdf
return microseconds / 74 / 2;
}
long microsecondsToCentimeters(long microseconds){
// The speed of sound is 340 m/s or 29 microseconds per centimeter.
// The ping travels out and back, so to find the distance of the
// object we take half of the distance travelled.
return microseconds / 29 / 2;
}