Re: What em I doing wrong?

Hello People,

i am quite new here and i need your help very fast.

i am at a big exhibition with my project and something with my programming dont work out. For you it should be very easy i guess.

A light reacts on an approaching and leaving person. An ultrasonic sensor measures the distance and gives the signal to the engine to roll the light bulb up or down. When i come closer the engine rolls the light up, when i retreat the light is coming down.

my problem: i want the engine to have a starting point and an ending point. every step up and down should be counted and if its for example 5000 steps its not going higher and it should not go under 0.
what did i do wrong?
i would be so happy about your help!
here is my programming:

#define trigPin 7
#define echoPin 8

#define STEPS 200
#include <Stepper.h>
Stepper stepper(STEPS, 1, 2, 3, 4);
int steps = 0;
int temp;
long distance;
void setup() {
 Serial.begin (9600);
 pinMode(trigPin, OUTPUT);
 pinMode(echoPin, INPUT);
 stepper.setSpeed(100);
}

void loop() {
 long duration;
 digitalWrite(trigPin, LOW);  // Added this line
 delayMicroseconds(2); // Added this line
 digitalWrite(trigPin, HIGH);
//  delayMicroseconds(1000); - Removed this line
 delayMicroseconds(10); // Added this line
 digitalWrite(trigPin, LOW);
 duration = pulseIn(echoPin, HIGH);
 
 int tmp_distance = (duration/2) / 29.1;
 if (tmp_distance >= 250 || tmp_distance <= 10){
   Serial.println("Out of range");
 }
 else {
   temp = distance;
   distance = tmp_distance;
   int differenz = temp - distance;
   Serial.print("Differenz: ");
   Serial.print(differenz);
   Serial.println(" cm");
  /*Serial.print("Distance: ");
   Serial.print(distance);
   Serial.println(" cm");*/
   Serial.println("----");
//approaching
  if (differenz > 5 && differenz < 50 && steps <= 5000) {
     stepper.step((differenz*24)*-1);   
     steps = steps + differenz*24;
     
   }
///leaving
   if (differenz < -5 && differenz > -50 && steps >= 0) {
    
     stepper.step((differenz*24)*-1);
     steps = steps + (differenz*24);
     
   }
 }
 Serial.print("Steps: ");
 Serial.println(steps);
 delay(200);
 
 
 
}

Thread hijack -topic split

You have posted code without using code tags. This creates certain problems and obstacles for other forum members. The code tags make the code look

like this

when posting source code files. It makes it easier to read, and can be copied with a single mouse click.
If you have already posted without using code tags, open your message and select “modify” from the pull down menu labelled, “More”, at the lower left corner of the message. Highlight your code by selecting it (it turns blue), and then click on the “</>” icon at the upper left hand corner. Click on the “Save” button. Code tags can also be inserted manually in the forum text using the [code] and [/code] metatags.

When you are finished that, please read these two posts:

How to use this forum - please read.
and
Read this before posting a programming question …

AnnaMaria:
my problem: i want the engine to have a starting point and an ending point. every step up and down should be counted and if its for example 5000 steps its not going higher and it should not go under 0.

I can’t see what variable holds that value.

You probably just need code like this

if (myVar > 5000) {
   myVar = 5000;
}
if (myVar < 0) {
   myVar = 0;
}

…R

You tell the stepper to step some number of times, without checking to see that the number of steps it will have taken doesn't exceed the maximum, when stepping in one direction, and doesn't drop below the minimum when stepping in the other direction.

Given that that is a requirement, I have to wonder why you are not implementing that requirement.

i thought i did it here:

if (differenz > 5 && differenz < 50 && steps <= 5000)

and here:

if (differenz < -5 && differenz > -50 && steps >= 0)

between 0 and 5000 steps. and its counting the steps here:

steps = steps + differenz*24

i thought i did it here:

You thought you did what?

Which lines of code are telling the stepper to step? I could be wrong (though I doubt it), but it looks to me like it is these two:

     stepper.step((differenz*24)*-1);  
     stepper.step((differenz*24)*-1);

Now, I don't see anything that prevents the stepper from stepping to more than 5000 steps or to step to less than 0 steps.

Now, those calls are in if blocks, and will not always be called, but the function could be called when steps is 5000 and differenz is 49, causing the stepper to step to step # 6175. The function could be called when steps is 0 and cause the stepper to step to step # -1175.

That is hardly what I would describe as not allowing the stepper to step below 0 or above 5000.

if (differenz > 5 && differenz < 50 && steps <= 5000) {
     stepper.step((differenz*24)*-1);  
     steps = steps + differenz*24;
     
   }

But that code will make steps exceed 5000, potentially.

i define steps at the beginning.so it starts at 0 everytime.

int steps = 0;

and than what awol said

if (differenz > 5 && differenz < 50 && steps <= 5000) {
     stepper.step((differenz*24)*-1);  
     steps = steps + differenz*24;
     
   }

its only going on in the if function if steps are counted under 5000

its only going on in the if function if steps are counted under 5000

You will only tell the stepper to step if it has not yet reached 5000 steps. But you WILL tell it to take up to 1175 steps, so you COULD step well over 5000.

ok please imagine this ist my first arduino project. i dont understand where i tell it to take up to 1175 steps. sorry.

i dont understand where i tell it to take up to 1175 steps.

49 * 24 = ?

i dont understand where i tell it to take up to 1175 steps.

Right here:

    stepper.step((differenz*24)*-1);

You will call this function if differenz is between 5 and 49, inclusive.

49 * 24 * -1 = -1176

(I misread that *-1 as -1, becauseyoucan'tbebotheredtousepsaces)

ok thank you! Thats true. And now?

 if (differenz > 5 && differenz < 50 &&  steps + (differenz*24) < 5000) {
      stepper.step(differenz*24);   
      steps = steps + differenz*24;

its still going over 5000 and under 0

If steps is the variable that should not exceed 5000 then apply to it the code in Reply #3

...R