Blinkwithoutdelay help!

Hello everyone,

I have been triyng to adapt blinkwithoutdelay to my code. I want the arduino to run a certain steps if the sensor value goes above 200.

Example:

if sensor value= true

//do something
delay 1000
Repeat the following 3 times
//do semething else

delay 3000

do this

I already did this with the delay function and it works well but I need to remove the delays so that a led will turn on based on a sensor value.

Can someone explain how I could do this code with blinkwithoutdelay.

Thank you

Post your working Sketch.

Here’s the code with delay:

const int m1r=9;
const int m1l=2;
const int m2f=2;
const int m2b=8; 
const int pin=10; 
const int pin1=11; 
const int threshold = 200;
const int threshold1= 600;
const int sensorMin = 100;
const int ledPin =  13; 


void setup(){
   pinMode(m1r, OUTPUT);
    pinMode(m1l, OUTPUT);
     pinMode(m2f, OUTPUT);
      pinMode(m2b, OUTPUT);
      pinMode(ledPin, OUTPUT); 
   digitalWrite(pin,LOW);
   digitalWrite(pin1,HIGH);
}
void loop(){
  int x = 0;
  int analogValue = analogRead(1);
     int analogValue1 = analogRead(0);
  //Motor control   
 if (analogValue > threshold) {
  
   
     digitalWrite(m2b,LOW);
  digitalWrite(m2f,HIGH);
  
   delay(1000);
   
    while (x <5){
      
   digitalWrite(m1r,HIGH);
   digitalWrite(m1l,LOW);
   
   digitalWrite(m2b,LOW);
   digitalWrite(m2f,HIGH);

   
   delay(700);
   
   digitalWrite(m2b,HIGH);
   digitalWrite(m2f,LOW);
   
   digitalWrite(m1r,LOW);
   digitalWrite(m1l,HIGH);
   
   delay(1000);
   
    digitalWrite(m2b,HIGH);
   digitalWrite(m2f,LOW);
   
   digitalWrite(m1r,LOW);
   digitalWrite(m1l,HIGH);
   
   delay(1000);
   x=x+1;
    }
x=0;  
}
   
else{
  
  digitalWrite(m2f,LOW);
  digitalWrite(m2b,HIGH);
digitalWrite(m1r,HIGH);
   digitalWrite(m1l,HIGH);
}

    }

I had made a code without delay but deleted it because it was not working and was badly written.

Didn’t test this, but it’s close:

const int m1r=9;
const int m1l=2;
const int m2f=2;
const int m2b=8; 
const int pin=10; 
const int pin1=11; 
const int threshold = 200;
const int threshold1= 600;
const int sensorMin = 100;
const int ledPin =  13; 


void setup(){
pinMode(m1r, OUTPUT);
pinMode(m1l, OUTPUT);
pinMode(m2f, OUTPUT);
pinMode(m2b, OUTPUT);
pinMode(ledPin, OUTPUT); 
digitalWrite(pin,LOW);
digitalWrite(pin1,HIGH);
}

void loop()
{
static int Iterations=0;
static int Step=0;
static unsigned long TimeStamp;

//Motor control   
if (analogRead(1) > threshold)
  Step=1;

switch(Step)
  {
  case 1:
    TimeStamp=millis();
    digitalWrite(m2b,LOW);
    digitalWrite(m2f,HIGH);
    Step++;
    break;

  case 2:
    if(millis()-TimeStamp > 1000)
       {
       TimeStamp=millis();
       digitalWrite(m1r,HIGH);
       digitalWrite(m1l,LOW);
       digitalWrite(m2b,LOW);
       digitalWrite(m2f,HIGH);
       Step++;
       }
    break;

  case 3:
    if(millis()-TimeStamp > 700)
       {
       TimeStamp=millis();
       digitalWrite(m2b,HIGH);
       digitalWrite(m2f,LOW);
       digitalWrite(m1r,LOW);
       digitalWrite(m1l,HIGH);
       Step++;
       }
    break;
  
  case 4:
    if(millis()-TimeStamp > 2000)
       {
       if(Iterations<4)
         {
         Iterations++;
         Step=2;
         }
       else
         Step=0;
       }
    break;

    default:
      digitalWrite(m2f,LOW);
      digitalWrite(m2b,HIGH);
      digitalWrite(m1r,HIGH);
      digitalWrite(m1l,HIGH);
      break;
  }
}

Here’s what I wrote but it doesn’t work(motors do nothing)

const int motorright=9;
const int motorleft=2;
const int motorfront=2;
const int motorback=8; 
const int distancesensor = 200;
const int lightsensor= 600;
const int ledPin =  13; 

int ledState = LOW;            
long previousMillis = 0;
long interval = 1000; 

void setup(){
  
    for (int thisPin = 2; thisPin < 8; thisPin++)  {
    pinMode(thisPin, OUTPUT);
  }
}

void loop() {
 
  int analogValue = analogRead(1);
  
if (analogValue > distancesensor) {
  
   
     digitalWrite(motorback,LOW);
  digitalWrite(motorback,HIGH);
  
   if (millis() - previousMillis > 1000) {
   
   if (analogValue > distancesensor) {
      digitalWrite(motorright,HIGH);
   digitalWrite(motorleft,LOW);
   
   digitalWrite(motorback,LOW);
   digitalWrite(motorfront,HIGH);

   
    if (millis() - previousMillis > 1700) {
   
     if (analogValue > distancesensor) {
   digitalWrite(motorback,HIGH);
   digitalWrite(motorfront,LOW);
   
   digitalWrite(motorright,LOW);
   digitalWrite(motorleft,HIGH);
   
   if (millis() - previousMillis > 2700) {
   
    digitalWrite(motorback,HIGH);
   digitalWrite(motorfront,LOW);
   
   digitalWrite(motorright,LOW);
   digitalWrite(motorleft,HIGH);
   
   if (millis() - previousMillis > 3700) {
  
   
}
   }
else{
  
  digitalWrite(motorfront,HIGH);
  digitalWrite(motorback,LOW);
digitalWrite(motorright,LOW);
   digitalWrite(motorleft,LOW);
}
  
 previousMillis = millis();
     }
    }
   }
   }
}
}

Thanks wildbil for writing the full code. I will test it.

Hello everyone,

It has been a while but I have finally rebuilt the robot(the chip wasn’t working anymore). I have tried the code from wildbil but it partially works.When the sensor value is true, the robot takes a right for about four seconds then the motorback is activated for about 10 seconds. After that it stops working. Please note that the motors activate when the value is LOW.

I updated the code but I still need some help figuring out what’s wrong:

const int Right=4;
const int Left=9;
const int Front=8;
const int Back=2;
const int distancesensor = 300;
const int lightsensor= 600;
const int ledPin =  13;  

//Front 8 
//Back 4
//Left 9 
//Right 2

void setup(){

  for (int thisPin = 2; thisPin < 8; thisPin++)  {
    pinMode(thisPin, OUTPUT);
  }
}


void loop()
{
static int Iterations=0;
static int Step=0;
static unsigned long TimeStamp;

//Motor control   
if (analogRead(1) > distancesensor)
  Step=1;

switch(Step)
  {
  case 1:
    TimeStamp=millis();
    
    digitalWrite(Back,LOW);
    digitalWrite(Front,HIGH);
    Step++;
    break;

  case 2:
    if(millis()-TimeStamp > 1000)
       {
       TimeStamp=millis();
       
       digitalWrite(Right,LOW);
       digitalWrite(Left,HIGH);
       
       digitalWrite(Back,HIGH);
       digitalWrite(Front,LOW);
       Step++;
       }
    break;

  case 3:
    if(millis()-TimeStamp > 1700)
       {
       TimeStamp=millis();
       
       digitalWrite(Right,LOW);
       digitalWrite(Left,HIGH);
       
       digitalWrite(Back,HIGH);
       digitalWrite(Front,LOW);
       
       Step++;
       }
    break;
  
  case 4:
    if(millis()-TimeStamp > 3000)
       {
       if(Iterations<4)
         {
         Iterations++;
         Step=2;
         }
       else
         Step=0;
       }
    break;

    default:
      
      
      digitalWrite(Right,HIGH);
      digitalWrite(Left,HIGH);
      
      digitalWrite(Front,LOW);
      digitalWrite(Back,HIGH);
      
      break;
  }
}

You are using these pins as outputs but they are not configured as such…

const int Left=9;
const int Front=8;

I suggest you drop the for-loop and initialize the pins individually…

void setup()
{
  pinMode( Right, OUTPUT );
  pinMode( Left, OUTPUT );
  pinMode( Front, OUTPUT );
  pinMode( Back, OUTPUT );
  pinMode( ledPin, OUTPUT );
}

Thanks Coding Badly I didn't even see it. It solved my problem. Thanks to everyone for their help!!!!!(special thanks to wildbil for writing the code)

By the way, for anyone who has a similar problem, here’s the final code:

const int Right=4;
const int Left=9;
const int Front=8;
const int Back=2;
const int distancesensor = 300;
const int lightsensor= 600;
const int ledPin =  13;  

//Front 8 
//Back 4
//Left 9 
//Right 2

void setup(){

  pinMode( Right, OUTPUT );
  pinMode( Left, OUTPUT );
  pinMode( Front, OUTPUT );
  pinMode( Back, OUTPUT );
  pinMode( ledPin, OUTPUT );
}


void loop()
{
static int Iterations=0;
static int Step=0;
static unsigned long TimeStamp;

//Motor control   
if (analogRead(1) > distancesensor)
  Step=1;

switch(Step)
  {
  case 1:
    TimeStamp=millis();
    
    digitalWrite(Back,LOW);
    digitalWrite(Front,HIGH);
    Step++;
    break;

  case 2:
    if(millis()-TimeStamp > 1000)
       {
       TimeStamp=millis();
       
       digitalWrite(Right,LOW);
       digitalWrite(Left,HIGH);
       
       digitalWrite(Back,HIGH);
       digitalWrite(Front,LOW);
       Step++;
       }
    break;

  case 3:
    if(millis()-TimeStamp > 1700)
       {
       TimeStamp=millis();
       
       digitalWrite(Right,LOW);
       digitalWrite(Left,HIGH);
       
       digitalWrite(Back,HIGH);
       digitalWrite(Front,LOW);
       
       Step++;
       }
    break;
  
  case 4:
    if(millis()-TimeStamp > 3000)
       {
       if(Iterations<4)
         {
         Iterations++;
         Step=2;
         }
       else
         Step=0;
       }
    break;

    default:
      
      
      digitalWrite(Right,HIGH);
      digitalWrite(Left,HIGH);
      
      digitalWrite(Front,LOW);
      digitalWrite(Back,HIGH);
      
      break;
  }
}

Thank you for the follow-up.