Binary Counter, a different way

I am trying to find different ways to do different sketches to help learn functions, etc. The sketch I am working on would have the same outcome as septillion's 32 bit binary counter. This is not to improve anything, just to learn.

The 5 LEDs blink, each having their own delay (500, 1000, 2000, 4000, 8000 ms).

There is something going on, but not the desired output.

int dly=500;
int dly1=500;
int dly2=1000;
int dly4=2000;
int dly8=4000;
int dly16=8000;
const int ledPin8 = 8;
const int ledPin9 = 9;
const int ledPin10 = 10;
const int ledPin11 = 11;
const int ledPin12 = 12;
unsigned long currentMillis = 0;

void setup() {
  
pinMode(ledPin8, OUTPUT);
pinMode(ledPin9, OUTPUT);
pinMode(ledPin10, OUTPUT);
pinMode(ledPin11, OUTPUT);
pinMode(ledPin12, OUTPUT);
  
}

void loop() {

  currentMillis = millis();
  
  bit1();
  bit2();
  bit4();
  bit8();
  bit16();
}
void bit1() {
 
    
    digitalWrite(ledPin8, HIGH);
    delay(dly1);
    digitalWrite(ledPin8, LOW);
    delay(dly1);
}

void bit2() {
 
   
    digitalWrite(ledPin9, HIGH);
    delay(dly2);
    digitalWrite(ledPin9, LOW);
    delay(dly2);
}
void bit4() {
    
    digitalWrite(ledPin10, HIGH);
    delay(dly4);
    digitalWrite(ledPin11, LOW);
    delay(dly4);
}

void bit8() {
    
    digitalWrite(ledPin9, HIGH);
    delay(dly8);
    digitalWrite(ledPin9, LOW);
    delay(dly8);
}
void bit16() {
    
    digitalWrite(ledPin9, HIGH);
    delay(dly16);
    digitalWrite(ledPin9, LOW);
    delay(dly16);
}

It would produce the desired output if all of the functions were done in parallel, but they aren't. Each function is done sequentially. The first LED will be on for half a second and off for half a second. Then the second LED will do the same. Then the third will be on for a second and off for a second... After the last LED is on for 8 seconds and off for 8 seconds, the whole cycle starts again.

You also seem to have your pins mixed up. You are using 'ledPin9' for bit2, bit8, and bit16. For bit4 you turn on ledPin10 and turn off ledPin11.

Try this for blinking in a binary pattern:

void loop()
{
    digitalWrite(ledPin8, (millis()/250) & 1);
    digitalWrite(ledPin9, (millis()/500) & 1);
    digitalWrite(ledPin10, (millis()/1000) & 1);
    digitalWrite(ledPin11, (millis()/2000) & 1);
    digitalWrite(ledPin12, (millis()/4000) & 1);
}

Consolidate all those bitx functions into one with passed params.

(I polished up my variables and such before copying the code, I must have missed a few entries)

Thank you. That's what I was looking for. I knew there was a way, but I haven't used the "& 1" yet.

Another way that uses binary directly:

{
  const unsigned int timePerBit = 250;
  unsigned long int time = millis() / timePerBit;
    digitalWrite(ledPin8, time & 1);
    digitalWrite(ledPin9, time & 2);
    digitalWrite(ledPin10, time & 4);
    digitalWrite(ledPin11, time & 8);
    digitalWrite(ledPin12, time & 16);
}

[/quote]

Thanks again. It looks like I need to stop skimming the book. "Pass Parameters" hasn't been read yet. That helps a ton.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.