Hi There,
relatively new here to Arduino and the forum, but I noticed lot's experienced user who offer help, so maybe I should just ask for help
I found some code samples here and tried to fix them together to create my own (monkey see, monkey do).
So far so good, just piece by piece and not changing to much. I use Serial.Println command to check my results (this works good)
But now I came to the point where I need some professional help from experienced users.
Below you will find some code, but let me explain what it should do.
I use a ultrasonic distance meter which is constantly measuring distance. The distance is send to Serial.Println so I can see it changing.
Furthermore we have a variable called "i" (i will be used later to set a value for DMX lightning brightness)
If the distance changes, the "i"value should change as well. This is no problem, however I would like to change it slowly by using increments.
The minimum value is 0, the maximum value is 255. Once the upper value (255) is reached, it should remain 255 unless the distance changes. If that happens, the "i" value should decrease from 255 back to 0 in steps.
In short: When distance <= 100, value "i" should start at 0 and change to 255 in steps of 1.
When distance >100, value "i" should go back from 255 to 0 in steps of 1 and stay at 0, until value changes again to <=100 ... etc.
When I execute code, the initial distance is printed but does not change anymore (like it's frozen or there is a loop ?), and value "i" remains 0.
Any clues what I'm doing wrong? I look forward to your replies
#define trigPin 7 // pin for ultrasonic
#define echoPin 6
void setup()Â Â Â Â Â Â
{
Serial.begin(9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
}
// the loop routine runs over and over again forever:
void loop()
{
 long duration;
 int distance;
 digitalWrite(trigPin, LOW);
 delayMicroseconds(2);
 digitalWrite(trigPin, HIGH);
 delayMicroseconds(10);
 digitalWrite(trigPin, LOW);
 duration = pulseIn(echoPin, HIGH);
 distance = duration*0.034/2;
 int x=1;
Â
if (distance <= 100)
{
  for (int i = 0;256> i >0 ; i=i+x)
Â
 { if (i>=255)x=0;
  if (i<=0)x=1;
  Serial.println(i);
  Serial.println(distance);
  delay(10); }
Â
}
//else
Â
 {
  for (int i = 0;256> i >0 ; i=i+x)
Â
 Â
 {if (i>=255)x=-1;
  if (i<=0)x=0;
  Serial.println(i);
  Serial.println(distance);
  delay(10); }
}
}
/[code]