Go Down

Topic: [Beginner] Ultrasound + Dimming (Read 86 times) previous topic - next topic

MelectroU

I'm only beginner on this stuff, so I decided to do something easy, while doing this easy task of mine I encountered a little problem. I was trying to make dimming light, when ultrasound detects it at certain point, the light either fades in completely or fades out completely. Here's the code:

#define trigPin 3
#define echoPin 2
int led = 9;           
int brightness2 = 255;
int brightness = 0;
int fadeAmount = 3 ;   

void setup(){
  Serial.begin(9600);
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  pinMode(led, OUTPUT);
}
void loop (){
  int duration, distance;
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(1000);
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);
  distance = (duration/2)/29.1;
  Serial.print(distance);
  Serial.print("cm ");
  delay(30);

  if (distance <= 25) {
  analogWrite(led, brightness);

  if (brightness < 255){ 
  brightness = brightness + fadeAmount;
}
  if (brightness == 255) {
    brightness = 255;
  }     
  delay(30);                           
}
else {
  analogWrite(led, brightness2);

  if (brightness2 > 0){ 
  brightness2 = brightness2 - fadeAmount;
}
  if (brightness2 == 0) {
    brightness2 = 0;
  }     
  delay(30);                           
}
 
}

The problem is when i reset, for the first time round works just right. It fades in when the distance is <= 25 and fades out when the distance is > than 25. But for the second or any other time it just turns on or off, the fading effect disappears. What am I doing wrong? Should I add one more delay somewhere?

PaulS

Quote
What am I doing wrong?
It's really hard to tell because your code is so poorly formatted. Every { goes on a new line. Use Tools + Auto Format to properly format the code.

MelectroU

#2
Jan 30, 2015, 09:57 pm Last Edit: Jan 30, 2015, 09:58 pm by AWOL
Didn't knew about that function, here's formatted version:

Code: [Select]

#define trigPin 3
#define echoPin 2
int led = 9;           
int brightness2 = 255;
int brightness = 0;
int fadeAmount = 3 ;   

void setup(){
  Serial.begin(9600);
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  pinMode(led, OUTPUT);
}
void loop (){
  int duration, distance;
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(5000);
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);
  distance = (duration/2)/29.1;
  Serial.print(distance);
  Serial.print("cm ");
  delay(30);
  if (distance <= 25) {
    analogWrite(led, brightness);
    if (brightness < 255){ 
      brightness = brightness + fadeAmount;
    }
    if (brightness == 255) {
      brightness = 255;
    }     
    delay(30);                           
  }
  else {
    analogWrite(led, brightness2);
    if (brightness2 > 0){ 
      brightness2 = brightness2 - fadeAmount;
    }
    if (brightness2 == 0) {
      brightness2 = 0;
    }     
    delay(30);                           
  }

}


Moderator edit:  the usual

johnwasser

You use separate brightness variables for fading in (which hits 255 and sticks) and fading out (which hits 0 and sticks).  Use the same brightness for both.
Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

MelectroU

Worked like a charm, thank you!

Go Up