for(int x = 0; x < 30000; x++) - How can I go above 30000...?

Hi there,

Have the following to run 30 sec but want to run it 40 sec (or more) but when I change the value to 40000 the motor just keeps running forever...

for(int x = 0; x < 30000; x++) {
digitalWrite(stepPin,HIGH);
delayMicroseconds(800);
digitalWrite(stepPin,LOW);

Any suggestions on how I add the extra sec's...?

Cheers,
Soren

Change datatype from "int" to "unsigned int" would allow you to go to 65535.

@Danois90
Awesome, that was exactly what I needed...
Thank you so much...

No problem, but IMHO there should be a better sollution to do what you want.

You really should not be using delay for this kind of thing. Have a look at
Using millis for timing
Demonstration for several things at the same time
And apply what you learn to your project.

I am glad that you got it working but would be interested to see the rest of the code block of the for loop and I hope that your program never needs to do anything during the 40 (or more) seconds

@Danois90
Yeah 100%... I'm not the best coder in the world but it works now...

@PerryBebbington
Thank you for this... Well I understand my code is far from perfect but it works perfectly as it is now so why change it...?

@UKHeliBob
In short i've built a lift which goes up when LED light is on and goes down when LED light is off. (very simple right...)

Here's the code:

int sensorPin = A0;   // select the input pin for ldr
int sensorValue = 0;  // variable to store the value coming from the sensor

int RelayPin = 3; //Control the relay

const int stepPin = 6;  //PUL -Pulse
const int dirPin = 7; //DIR -Direction
const int enPin = 8;  //ENA -Enable

int alreadyRunMotor = 0;


void setup() {
  //pinMode(2, OUTPUT); //pin connected to the relay
  //Serial.begin(9600); //sets serial port for communication
  pinMode(LED_BUILTIN, OUTPUT); //Test LDR on pin13
  pinMode(stepPin,OUTPUT); 
  pinMode(dirPin,OUTPUT);
  pinMode(enPin,OUTPUT);
  digitalWrite(enPin,LOW);
  
  pinMode(RelayPin, OUTPUT);

}

void loop(){
  sensorValue = analogRead(sensorPin);    
  Serial.println(sensorValue); 
  //digitalWrite(dirPin,HIGH); 


if (sensorValue > 800) {  //Low light goes down


digitalWrite(dirPin,HIGH); 

  if(alreadyRunMotor == 0) {

digitalWrite(RelayPin, HIGH);
delay(1000);
digitalWrite(LED_BUILTIN, HIGH); 
    
  
  for(unsigned int x = 0; x < 60000; x++) {
    digitalWrite(stepPin,HIGH);
    delayMicroseconds(800);
    digitalWrite(stepPin,LOW);

    alreadyRunMotor = 1;}
   
  digitalWrite(RelayPin, LOW);
  digitalWrite(LED_BUILTIN, LOW); 
  }
  }
 
else {

  if (sensorValue < 800) { //high light goes up

  
 digitalWrite(dirPin,LOW); 
  

  if(alreadyRunMotor == 1) {

digitalWrite(RelayPin, HIGH);
delay(1000);
digitalWrite(LED_BUILTIN, HIGH); 
    
  
  for(unsigned int x = 0; x < 60000; x++) {
    digitalWrite(stepPin,LOW);
    delayMicroseconds(800);
    digitalWrite(stepPin,HIGH);
    
    alreadyRunMotor = 0;}

    
    digitalWrite(RelayPin, LOW);
    digitalWrite(LED_BUILTIN, LOW); 
    
}}



 delay(100);                  
  }}

@PerryBebbington
Thank you for this... Well I understand my code is far from perfect but it works perfectly as it is now so why change it...?

Up to you of course but so you learn something. This is how your code works at the moment, based on a visit to a restaurant:
A waiter meets you at the door, takes you to a table, gives you a menu then waits by your table while you decide what to order. The waiter takes your order, goes to the kitchen and waits there while the chef cooks your food. When the food is ready the waiter brings it to your table then waits by your table while you eat it. When you’ve finished eating the waiter takes your plates away and returns to ask if you want anything else. This continues until you leave. No one else gets served. This is how your code is working at the moment.
I’m not going to describe what really happens in a restaurant as you already know. A waiter uses exactly the same system as a state machine to serve people when they need serving and check to see who needs serving next between dealing with customers.

One day you will need code that can do more than one thing at a time.

In short i've built a lift which goes up when LED light is on and goes down when LED light is off. (very simple right...)

OK as far as it goes but what if there are several floors, each with a call button and an emergency stop button in the lift. You would not want to be hanging about with the Arduino doing nothing with users not getting a response.

As others have suggested it would be a useful exercise for you to implement the code in a different way and you will certainly learn a useful technique for when you really need it

Danois90:
Change datatype from "int" to "unsigned int" would allow you to go to 65535.

Yes, and if you change from "int" to "long" you can go up to 2147483647.

unsigned long will get you to 2^32 - 1.

millis() and micros() both use unsigned long, you should too for time variables.