Yet another Binary LED counter.......with a twist

Sorry for being a complete Noob, but I've got a little project that I'm trying to build, unfortunately its been 20 years since I've done any coding, so I'm a little rusty.

Long story short, I work for an elevator company and I'm trying to make a Binary floor indicator simulator for a long term test unit.

I've looked at a few of the Binary codes and they all work on my UNO, but here's the thing, I need it to count up and down, here's the breakdown of what I'm trying to simulate.

Digital pins 0-7
Binary count on pins 0-4
Binary count up from 00001 to 11111 then count down to 00001
While the binary count is going up turn pin 5 HIGH, pin 6 LOW (simulates the up arrow)
While the binary count is going down turn pin 5 LOW, pin 6 HIGH (simulates the down arrow)

Lastly it would be fun to simulate a random door open signal on pin 7 going HIGH, but when this is active the binary count needs to pause for 10 seconds while pin 7 is HIGH (this isn't completely needed but would be fun)

Currently this is the code that I'm working with, got it from another old thread.

int ledPins[8] = {0, 1, 2, 3, 4, 5, 6, 7};
const int numberofleds = 5;

/* pins start at zero
sorry, I didn't bother checking if this array was necessary or if I could use pin output states in an "if" statement
*/
int pinstates[8] = {0, 0, 0, 0, 0, 0, 0, 0};

// the setup function runs once when you press reset or power the board
void setup() {
// initialize digital pins as outputs.
for (int i = 0; i < numberofleds + 3; i++)
pinMode(ledPins*, OUTPUT);*
}
// the loop function runs over and over again forever
void loop() {

  • delay(2000);*
  • fliploop(leastsignificantones());*
    }
    int leastsignificantones() {
  • int i = 0;*
    _ while (pinstates == 1 && i <= numberofleds - 2) {_
    * i = i + 1;*
    * }*
    * return i ;*
    }
    //starting from least significant bits
    void fliploop(int numberofbitstoflip) {
    * for (int i = 0; i <= numberofbitstoflip; i++)*
    * flipbit(i);*
    }
    void flipbit(int i) {
    * digitalWrite(5, HIGH);*
    * digitalWrite(6, LOW);*
    _ if (pinstates == 0) {
    digitalWrite(ledPins*, HIGH);
    pinstates = 1;
    }
    else {
    digitalWrite(ledPins, LOW);
    pinstates = 0;
    }
    }*_

Nevermind figured it out, might not be pretty but it works

const int pins[] = { 0, 1, 2, 3, 4, 5, 6, 7};
const int intervalo = 2000;
int i = 0;
long r;

int pinstates[8] = {0, 0, 0, 0, 0, 0, 0, 0};

void setup() {
for (int n = 0; n < 8; n++)
pinMode(pins[n], OUTPUT);
}

byte value = 0;

void loop()
{
if (i == 0)
{
value++;
r = random(0, 10);
if (r == 0)
{
digitalWrite(7, LOW);
delay(10000);
}
delay(intervalo);
digitalWrite(5, LOW);
digitalWrite(6, HIGH);
digitalWrite(7, HIGH);
if (value == 31)
i = 1;
for (int n = 0; n < 5; n++)
{
if ( value & (1 << n) )
digitalWrite(pins[n], LOW);
else
digitalWrite(pins[n], HIGH);
}
}
if (i == 1)
{
value--;
r = random(0, 10);
if (r == 0)
{
digitalWrite(7, LOW);
delay(10000);
}
delay(intervalo);
digitalWrite(5, HIGH);
digitalWrite(6, LOW);
digitalWrite(7, HIGH);
if (value == 0)
i = 0;
for (int n = 0; n < 5; n++)
{
if ( value & (1 << n) )
digitalWrite(pins[n], LOW);
else
digitalWrite(pins[n], HIGH);
}
}
}

Never ever, never ever, never ever, use delay(), we’ll only if using ‘does not’ significantly lock your code.

Take 20 minutes and learn how to use the BWD (blink without delay) technique and what a StateMachine can do for you. :slight_smile:

BTW, please follow proper sketch posting rules.
Use CTRL T to format your code.
Attach your ‘complete’ sketch between code tags, use the </> icon in the posting menu.
[code]Paste your sketch here[/code]

Always add comments to your code!