motor time delay without delay

if (state == LOW) {
digitalWrite(ledPin, HIGH);
// i want here delay 4sec only here! if i put{ delay(4000);} the delay is going and to if (state == HIGH) how i do this?
esc.writeMicroseconds(2300);
}
if (state == HIGH){ digitalWrite(ledPin, LOW);
esc.writeMicroseconds(1000);
}

if (voltage <11.00) { digitalWrite(ledPin, LOW);
esc.writeMicroseconds(1000);

} }

Try This:

void loop() {

  static unsigned long DelayTimer;

  int state = digitalRead(hallSensorPin);
  int sensorValue = analogRead(A0);
  float voltage = (5 * sensorValue * (R1 + R2)) / (1024 * R2);
  Serial.println(voltage);
  delay(100);


  if (state == LOW)     {
    digitalWrite(ledPin, HIGH);
    // i want here delay 4sec only here! if i put{ delay(4000);} the delay is going and to if (state == HIGH) how i do this?
    if ((millis() - DelayTimer) >= (4000)) {// waits until the DeltaT of millis() and DelayTimer exceed 4000 miliseconds
      esc.writeMicroseconds(2300);
    }
  }
  if (state == HIGH) {
    DelayTimer = millis(); // Sets the delay DelayTimer to start over as long as state == HIGH
    digitalWrite(ledPin, LOW);
    esc.writeMicroseconds(1000);
  }

  if (voltage < 11.00) {
    digitalWrite(ledPin, LOW);
    esc.writeMicroseconds(1000);

  }
}

Have a look at how millis() is used to manage timing without blocking in Several things at a time

...R

Robin2:
Have a look at how millis() is used to manage timing without blocking in Several things at a time

...R

I will read it thank you

zhomeslice many many thanks! It works!!! :slight_smile:

one more problem that i still have it is the read voltage if the arduino it is Connected to the computer USB and it is open the Serial monitor The volts are read correctly but if the arduino it is not connected to pc and i give it power from battery Does not read the volts correctly about -3 volts!
I write all the code from the beginning to see! Sorry for my bad English...

int ledPin = 13;

float R1=30000;
float R2=7500;

void setup()
{

pinMode(hallSensorPin, INPUT);
esc.attach(9);
delay(3000);

esc.writeMicroseconds(1000);
delay(1000);
}

void loop() {

static unsigned long DelayTimer;

int state = digitalRead;
int sensorValue = analogRead(A0);
float voltage = (5 * sensorValue * (R1 + R2)) / (1024 * R2);
Serial.println(voltage);
delay(100);

if (state == LOW) {
digitalWrite(ledPin, HIGH);
// i want here delay 4sec only here! if i put{ delay(4000);} the delay is going and to if (state == HIGH) how i do this?
if ((millis() - DelayTimer) >= (4000)) {// waits until the DeltaT of millis() and DelayTimer exceed 4000 miliseconds
esc.writeMicroseconds(2300);
}
}
if (state == HIGH) {
DelayTimer = millis(); // Sets the delay DelayTimer to start over as long as state == HIGH
digitalWrite(ledPin, LOW);
esc.Microseconds(1000);
}

if (voltage < 11.00) {
digitalWrite(ledPin, LOW);

}
}

xenofon:
zhomeslice many many thanks! It works!!! :slight_smile:

one more problem that i still have it is the read voltage if the arduino it is Connected to the computer USB and it is open the Serial monitor The volts are read correctly but if the arduino it is not connected to pc and i give it power from battery Does not read the volts correctly about -3 volts!
I write all the code from the beginning to see! Sorry for my bad English...

this sounds more like a electrical issue rather than a programming issue.
Schematic with analog input wiring and battery voltage would be necessary for me to evaluate.
Z

zhomeslice:
this sounds more like a electrical issue rather than a programming issue.
Schematic with analog input wiring and battery voltage would be necessary for me to evaluate.
Z

yes! Input Voltage (recommended) 7-12V
Input Voltage (limit) 6-20V

i gave 5.35 volts...! I gave him over 6v and works correctly!

one more ask :slight_smile:

if (voltage < 11.00) {
digitalWrite(ledPin, LOW);
esc.writeMicroseconds(1000);
// i want, if read once voltage <11 And execute the other two commands, if read again over 11V do not star the motor. i want to stay [esc.writeMicroseconds(1000);] to must Disconnect arduino from battery and put again to new battery to have bigger volts. because When ιit is marginally in the volt to stop the motor it is star and after 1sec stop And this is done for a long time.

xenofon:
yes! Input Voltage (recommended) 7-12V
Input Voltage (limit) 6-20V

i gave 5.35 volts...! I gave him over 6v and works correctly!

one more ask :slight_smile:

if (voltage < 11.00) {
digitalWrite(ledPin, LOW);
esc.writeMicroseconds(1000);
// i want, if read once voltage <11 And execute the other two commands, if read again over 11V do not star the motor. i want to stay [esc.writeMicroseconds(1000);] to must Disconnect arduino from battery and put again to new battery to have bigger volts. because When ιit is marginally in the volt to stop the motor it is star and after 1sec stop And this is done for a long time.

I'm glad you have discovered the voltage reading problem. :slight_smile:
I did not understand your sequence. Please generate some code or describe it differently to help us see what you are thinking.
Z

zhomeslice:
I'm glad you have discovered the voltage reading problem. :slight_smile:
I did not understand your sequence. Please generate some code or describe it differently to help us see what you are thinking.
Z

Hi! thank you.

}

if (voltage < 11.00) // if reads this once {
digitalWrite(ledPin, LOW);
esc.writeMicroseconds(1000); // forever! Even if the volts go up again.

}
}

xenofon:
Hi! thank you.

}

if (voltage < 11.00) // if reads this once {
digitalWrite(ledPin, LOW);
esc.writeMicroseconds(1000); // forever! Even if the volts go up again.

}
}

"forever" until uno power is reset?

while(1){ // locks in loop and will never release!
 esc.writeMicroseconds(1000); // forever! Even if the volts go up again.
}
while(1){ // locks in loop and will never release!
 esc.writeMicroseconds(1000); // forever! Even if the volts go up again.
 if(digitalRead(releasePin) == LOW) break; // you can break the loop with this
}

Z

zhomeslice:
"forever" until uno power is reset?

while(1){ // locks in loop and will never release!

esc.writeMicroseconds(1000); // forever! Even if the volts go up again.
}







while(1){ // locks in loop and will never release!
esc.writeMicroseconds(1000); // forever! Even if the volts go up again.
if(digitalRead(releasePin) == LOW) break; // you can break the loop with this
}




Z

the firs code works fine ,the The second does not work [releasePin was not declared in this scope]

xenofon:
the firs code works fine ,the The second does not work [releasePin was not declared in this scope]

Excellent. ignore the second one :slight_smile:
Z

thank you man :slight_smile:

last question(I am telling you the truth)...! :slight_smile:

void setup()
{
Serial.begin(9600);
pinMode(ledPin, OUTPUT);
pinMode(hallSensorPin, INPUT);
esc.attach(9);
esc.Microseconds(1900); // i want this command run for 6sec
esc.Microseconds(1000); // and this for 2sec :slight_smile:
delay(50);
}

xenofon:
thank you man :slight_smile:

last question(I am telling you the truth)...! :slight_smile:

void setup()
{
Serial.begin(9600);
pinMode(ledPin, OUTPUT);
pinMode(hallSensorPin, INPUT);
esc.attach(9);
esc.writeMicroseconds(1900); // i want this command run for 6sec
esc.writeMicroseconds(1000); // and this for 2sec :slight_smile:

These are Blink without delay timers just manipulated differently this runs in the setup so it only gets 1 chance so i use a while loop that breaks after the blink without delay time expires.
Note that this will execute the esc.writeMicroseconds(1900); over and over very rapidly during the time period!

  void setup()
{
   Serial.begin(9600);
   pinMode(ledPin, OUTPUT);      
   pinMode(hallSensorPin, INPUT); 
  esc.attach(9);
  unsigned long _ATimer;
  _ATimer = millis() ;
  while ((millis() - _ATimer) < (6000)) {
      esc.writeMicroseconds(1900); // i want this command run for 6sec
  }
  _ATimer = millis() ;
  while ((millis() - _ATimer) < (2000)) {
       esc.writeMicroseconds(1000); // and this for 2sec 
  }
  delay(50);
}

Z

zhomeslice:
These are Blink without delay timers just manipulated differently this runs in the setup so it only gets 1 chance so i use a while loop that breaks after the blink without delay time expires.
Note that this will execute the esc.writeMicroseconds(1900); over and over very rapidly during the time period!

  void setup()

{
  Serial.begin(9600);
  pinMode(ledPin, OUTPUT);     
  pinMode(hallSensorPin, INPUT);
  esc.attach(9);
  unsigned long _ATimer;
  _ATimer = millis() ;
  while ((millis() - _ATimer) < (6000)) {
      esc.writeMicroseconds(1900); // i want this command run for 6sec
  }
  _ATimer = millis() ;
  while ((millis() - _ATimer) < (2000)) {
      esc.writeMicroseconds(1000); // and this for 2sec
  }
  delay(50);
}



Z

It is works!!! Many thank you my friend :wink: :slight_smile: