using millis instead of delay

so im trying to use millis instead of delay but im not sure how to apply it anyone can help?

#define DATA 8    
#define LATCH A3      
#define CLOCK 12
#define LATCH_ON PORTC = PORTC | B00001000
#define LATCH_OFF PORTC = PORTC & B11110111
#define CLOCK_ON PORTB = PORTB | B00010000
#define CLOCK_OFF PORTB = PORTB & B11101111
#define DATA_ON PORTB = PORTB | B00000001
#define DATA_OFF PORTB = PORTB & B11111110      


void shiftByteMSF(byte leds){
DDRC|=LATCH_OFF;
shiftOut(DATA, CLOCK, LSBFIRST, leds); 
shiftOut(DATA, CLOCK, MSBFIRST, leds);     
      
    
DDRC|=LATCH_ON;
}

void setup(){
DDRC|=LATCH_ON;
DDRB|=DATA_ON;  
DDRB|=CLOCK_ON;
}


void loop(){
    byte leds = B10000000;
    shiftByteMSF(leds);
    delay(500);
    byte led = B00000000;
    shiftByteMSF(led);
    delay(500);    

    
        
    }

yes i am aware of the amount of define i have defined but not used they are for another section :slight_smile:

Have a look at the blink without delay example in the IDE, without delay.

#define DATA 8    
#define LATCH A3      
#define CLOCK 12
#define LATCH_ON PORTC = PORTC | B00001000
#define LATCH_OFF PORTC = PORTC & B11110111
#define CLOCK_ON PORTB = PORTB | B00010000
#define CLOCK_OFF PORTB = PORTB & B11101111
#define DATA_ON PORTB = PORTB | B00000001
#define DATA_OFF PORTB = PORTB & B11111110      

unsigned long previousMillis = 0;   
const long interval = 1000; 
void shiftByteMSF(byte leds){
DDRC|=LATCH_OFF;
shiftOut(DATA, CLOCK, LSBFIRST, leds); 
shiftOut(DATA, CLOCK, MSBFIRST, leds);     
      
    
DDRC|=LATCH_ON;
}

void setup(){
DDRC|=LATCH_ON;
DDRB|=DATA_ON;  
DDRB|=CLOCK_ON;
}


void loop(){
   unsigned long currentMillis = millis();
    
    byte leds = B10000000;
    shiftByteMSF(leds);
   if (currentMillis - previousMillis >= interval) {
    // save the last time you blinked the LED
    previousMillis = currentMillis;
    byte led = B00000000;
    shiftByteMSF(led);
    delay(500);    

    
        
    }
}

so i manage to take a few lines from the blinkwithoutdelay example but i dont know how to get rid of the 2nd delay also, how would chnage the duration of those delays i tried to change the 1000 on the cons long interval but that didnt work?

i manage to take a few lines from the blinkwithoutdelay example

It’s not enough to copy lines from it. You need to copy the entire approach. Try to understand how it works, it’s made simple as an example so you can learn from it.

#define DATA 8    
#define LATCH A3      
#define CLOCK 12
#define LATCH_ON PORTC = PORTC | B00001000
#define LATCH_OFF PORTC = PORTC & B11110111
#define CLOCK_ON PORTB = PORTB | B00010000
#define CLOCK_OFF PORTB = PORTB & B11101111
#define DATA_ON PORTB = PORTB | B00000001
#define DATA_OFF PORTB = PORTB & B11111110      

unsigned long previousMillis = 0;   
const long interval = 1000; 
  

void shiftByteMSF(byte leds){
DDRC|=LATCH_OFF;

shiftOut(DATA, CLOCK, LSBFIRST, leds); 
shiftOut(DATA, CLOCK, MSBFIRST, leds);     
      
    
DDRC|=LATCH_ON;
}

void setup(){
DDRC|=LATCH_ON;
DDRB|=DATA_ON;  
DDRB|=CLOCK_ON;
}


void loop(){
   unsigned long currentMillis = millis();
    
    byte leds = B10000000;
    shiftByteMSF(leds);
   if (currentMillis - previousMillis >= interval) {
    // save the last time you blinked the LED
    previousMillis = currentMillis;
    byte led = B00000000;
    shiftByteMSF(led);
   }
    
    if (byte leds=HIGH ){
      byte leds=LOW;
    } else {
       byte led=LOW;
     
 
        
    }
}

iv studied that blinkwithoutdelay sketch religiously lol i still cant get it to make it work any ideas where i am gaoing wrong?

if (byte leds=HIGH ){

Oops2

byte leds=LOW; Oops.

The demo Several Things at a Time illustrates the use of millis() to manage timing without blocking. It may help with understanding the technique.

Have a look at Using millis() for timing. A beginners guide if you need more explanation.

...R

TheMemberFormerlyKnownAsAWOL:

if (byte leds=HIGH ){

Oops2

byte leds=LOW;

Oops.

im assuming you have gathered what im trying to do basically turn of the led if it is on and turn it on if it is off. iv tried replicating and dapting it for my code clearly its not right. so what should it say instead whats written
so basically i think u know what im tryin g to do im trying to say is if the led is on, turn it off and vice versa

Robin2:
The demo Several Things at a Time illustrates the use of millis() to manage timing without blocking. It may help with understanding the technique.

Have a look at Using millis() for timing. A beginners guide if you need more explanation.

…R

iv had a read through this iv kinda grasped what he/she done on their code and i dont see much difference in what iv done so im still confused on whats gone wrong

OK, here if (byte leds=HIGH ){ you defining a byte variable called “leds” and assigning it the value HIGH (aka 1).
Then you are testing the result of that assignment to see if it is non zero.
It is. It always will be.

So this would be executed byte leds=LOW; Here you are defining a new variable called “leds”, assigning it the value “LOW” (aka 0), and mere nanoseconds later, this variable goes out of scope, so nothing has effectively happened.

Can I suggest you spend some time working through some basic programming examples.

In the long-run, this will save us all time.

thanks for the reply and in terms of saving you time can i just add because im not sure you're aware that you dont have to answer questions on forums like this. so if you think you dont have time to answer i suggest you dont waste time on people like me who just want to learn. it may seem like a dumb question to you because you're experience and all but just remember how you started in the first place.

thanks again for the help and sorry for wasting your time

…and you don’t have to flip-flop between Arduino API and direct port manipulation when you clearly don’t understand the basics.

Stick to Arduino, and leave the advanced stuff for later - it’ll save on overloading your learning.

m005a:
i suggest you dont waste time on people like me who just want to learn.

It's only a waste of time if you don't take the trouble to learn from the hints that you have been given.

If you look up the IF statement in the Reference section you will see that it must use == rather the =

...R

    if (byte leds = HIGH ){   // =  sets leds to HIGH
    if (byte leds == HIGH) {  // ==  compares leds to HIGH

      byte leds=LOW;         // set the variable  leds, 
/* warning, leds was already defined as  byte leds = B100000000  in loop()  */

    } else {
       byte led=LOW;         //  sets a different variable   led
     }
  }

to break it down to the simple minded so even I can understand.... what is the difference between the variables led and leds in your if() statement ? rhetorical, don't need to answer
to show how blink-without-delay, as I use it, very rudimentary example.

   if (currentMillis - previousMillis >= interval) {
    previousMillis = currentMillis;  // resets timer every interval
    count++;    // do something here, increment each time this if() is true
   }

if (count == 1 ) {
   leds = !leds ;
   count = 0 ;  // resets back to zero
}

this is my way of trying bring blingWithoutDelay into a simpler example.
of course, you could nest the second if() inside of the BWD timeing loop
or just put the leds = !leds inside of the BWD loop.
also, in this example, since you cannot create the variable 'leds' in loop() and retain it's value, you would need to define it as a global.

as a note, people of all levels ask questions, and readers of all levels lurk and try to get answers. so not every reply is always for the OP, some times it is to clear up things for the future lurkers.

one thing that I really like about what TheMemberFormallyKnownAsAWOL does with his 'ops' posts is to make one look and make one think... "what is he trying to tell me.... ? " so you figure it out on your own.

Actually, the if (byte leds == HIGH) is much worse than it looks, and is one of those cases where the compiler really should ( I haven’t tested it) throw up its hands and say “the syntax AND the semantics here are so badly broken, I’m not even going to guess what you intended”.

Initially, it looks like a variable definition with a comparison, but with such limited scope AND the fact that the variable isn’t initialised, such an expression would be pointless.

The point of “oops” is, indeed, exactly that - the opportunity to re-examine, and/or rephrase and re-ask the question.
If the asker contributes to the solution, they will remember the solution better, when they trigger those achievement endorphins.

using millis() for timing is similar to what a lot of people do every day.

Sitting down at your desk at 0800, you set a timer, in your mind, that at 1000 you will take a break and a 2nd timer that at 1200 you will take a break. Heck, you can even set 2 more timers, one for the 1500 break and one for the 1700 get off work.

So now with your day started off with 4 timers, t1stBreak, tLunch, t2ndBreak, tOffWork, and 0801 comes up. In your mind, somewhere, you compare the current time with the time stored in the other 4 timers, Is it break time, no. Is it lunch time, no. Is it 2nd break time, no, Is it time to get off work, no. In fact you can determine that at 0801 there is 3 hours and 59 minutes till t1stBreak.

Now, let’s just use the 1000 and 1500 break times. At 0800 a place holder is set aside to keep track of 1000 break time. At 0801, break time is checked against the current time, does the time of the break equal to or exceeded the current time? If it does not then do nothing, if it does then do something.

Using millis() is the similar to being at work and ticking off the minutes till its time to do something, except the time scales are very small and not kept in your head.

And give @TheMemberFormerlyKnownAsAWOL a bit of slack, he/she is correct in pointing out errors but can be kind of vague in pointing out solutions but his/hers pointers can be a great way to learn.

#define DATA 8    
#define LATCH A3      
#define CLOCK 12
#define LATCH_ON PORTC = PORTC | B00001000
#define LATCH_OFF PORTC = PORTC & B11110111
#define CLOCK_ON PORTB = PORTB | B00010000
#define CLOCK_OFF PORTB = PORTB & B11101111
#define DATA_ON PORTB = PORTB | B00000001
#define DATA_OFF PORTB = PORTB & B11111110      

const long interval = 1000;   
unsigned long previousMillis = 0;
void shiftByteMSF(byte HB){


LATCH_OFF; //sets latch low before data sending
  for(int shiftBit=0; shiftBit<8;  shiftBit++) { //repeat 8 times, incrementing .
    if(bitRead(HB, 7-shiftBit) == 1) { //read the bit of the variable for sReg, and start from Right-most bit
      DATA_ON;
      //Serial.println(bitRead(sRegState, 7-shiftBit));
    }
    else{
     DATA_OFF;
      //Serial.println(bitRead(sRegState, 7-shiftBit));
    }
    CLOCK_ON; //cycle clock once data value stable
    CLOCK_OFF;
    DATA_OFF; //0 on data, if it was already high, after clock pulse
    }
  LATCH_ON; //latch to output data to register
  

     
  
    }  
    
void setup(){
DDRC|=LATCH_ON;
DDRB|=DATA_ON;  
DDRB|=CLOCK_ON;
}


void loop(){
  
    
    byte HB_ON = B10000000;
    shiftByteMSF(HB_ON);
   if (millis() - previousMillis >= interval) {
    // save the last time you blinked the HB_OFF
    previousMillis = millis();
    byte HB_OFF = B00000000;
    shiftByteMSF(HB_OFF);
    delay(500);
   
        
    }
}

so in my project i am prohibited to use ethe delay function so iv introduced the millis function the problem is that iv managed to swapped one of the delays to millis( previousMillis = millis():wink: but i dont know how to swap out the last delay seen on the last line of the code and impliment the millis once again in exchange.

Useful techniques here

unsigned long previousMillis = 0; // used with the first Timer

unsigned long timer2Millis = 0; // used with the 2nd Timer

unsigned long timer3Millis = 0; // used with the 3rd Timer

etc.

See how to use millis() instead of multiple delay()

@m005a

TOPIC MERGED.

Do NOT duplicate in future for related topics please.

Could you take a few moments to Learn How To Use The Forum.
Other general help and troubleshooting advice can be found here.
It will help you get the best out of the forum.