binary leds

hello,

i need a little help

so de arduino picks a random number from 1-15, then it must show the random number in binary, for that part i have 4 leds installed,

how can i make a simple code that if the random number is 3 ,he will putout 3 in binary on the 4 leds like 0011

do i need to make a code for every number so 3 will put that led on and that led off, then 4 puts that led off and that one on, etc

or is there a more easier way

thank you guys

Take a look at shift registers. TPIC6B595, 74HC595, etc.

check bit logic operator & logical and.

pseudocode

inx x = random(0..15)
all leds off
if (x & 0b0000001) led1 = on
if (x & 0b0000010) led2 = on
if (x & 0b0000100) led3 = on
if (x & 0b0001000) led4 = on

0b00001000 etc are bit patterns

e.g
13 = 1101 = 1x8 + 1x4 +0x2 +1x1

1101
0001 &

0001 >> led on

1101
0010 &

0000 >> led off

1101
0100 &

0100 >> led on

etc

all values <> 0 are interpreted as true.

Read up about the C bit-fiddling operators
& | ^ << >> >>>

Start with any of these links:

thank u so mush guys, my code is mush smaller and i understand how i can work with it now.

hello,

so i have made a code that in generates a rondom number 1, 15, and it displays binair on 4 leds i have installed.
that works so far.

de problem is it keeps generating, it needs to stop, so i can press a button the amount of time the random number is,

so if the number is 7, show with the leds, i have to press the button 7 times, if it is correct it must light led10 and if wrong must light led13, then it must restart and generate a random number again so i can play the "game" again

this is my code so far, but cant fix it :confused: :

 int buttonPin = 2;
  int ledPin1   = 10;
  int ledPin2   = 11;
  int ledPin3   = 12;
  int ledPin4   = 13;
  byte randNumber;

  int buttonPushCounter = 0;
  int buttonState = 0;
  int lastButtonState = 0;
  
 





void setup() {
  // put your setup code here, to run once:
  pinMode (buttonPin, INPUT);
  pinMode (ledPin1, OUTPUT);
  pinMode (ledPin2, OUTPUT);
  pinMode (ledPin3, OUTPUT);
  pinMode ( ledPin4, OUTPUT);
  Serial.begin(9600);
  randomSeed(analogRead(0));
  
}

void loop() {
    buttonState = digitalRead(buttonPin);
    randNumber = random(1, 15);
    Serial.println(randNumber);
    
  
      if (randNumber & 0b0000001){digitalWrite(10, HIGH);} else {digitalWrite(10, LOW);}
      if (randNumber & 0b0000010){digitalWrite(11, HIGH);} else {digitalWrite(11, LOW);}
      if (randNumber & 0b0000100){digitalWrite(12, HIGH);} else {digitalWrite(12, LOW);}
      if (randNumber & 0b0001000){digitalWrite(13, HIGH);} else {digitalWrite(13, LOW);}
      delay(2000);



      
      if (buttonState != lastButtonState) {
   
        if (buttonState == HIGH) {
          buttonPushCounter++;
          }
          delay(50);
          }
         lastButtonState = buttonState;
          
         if (buttonPushCounter % randNumber == 0) {
          digitalWrite(10, HIGH);
          } 
          else {
            digitalWrite(13, HIGH);
            }
    }

aragonj5:
hello,

so i have made a code that in generates a rondom number 1, 15, and it displays binair on 4 leds i have installed.
that works so far.

de problem is it keeps generating, it needs to stop, so i can press a button the amount of time the random number is,

so if the number is 7, show with the leds, i have to press the button 7 times, if it is correct it must light led10 and if wrong must light led13, then it must restart and generate a random number again so i can play the "game" again

this is my code so far, but cant fix it :confused: :

 int buttonPin = 2;

int ledPin1   = 10;
 int ledPin2   = 11;
 int ledPin3   = 12;
 int ledPin4   = 13;
 byte randNumber;

int buttonPushCounter = 0;
 int buttonState = 0;
 int lastButtonState = 0;
 
void setup() {
 // put your setup code here, to run once:
 pinMode (buttonPin, INPUT);
 pinMode (ledPin1, OUTPUT);
 pinMode (ledPin2, OUTPUT);
 pinMode (ledPin3, OUTPUT);
 pinMode ( ledPin4, OUTPUT);
 Serial.begin(9600);
 randomSeed(analogRead(0));
 
}

void loop() {
   buttonState = digitalRead(buttonPin);
   randNumber = random(1, 15);
   Serial.println(randNumber);
   
 
     if (randNumber & 0b0000001){digitalWrite(10, HIGH);} else {digitalWrite(10, LOW);}
     if (randNumber & 0b0000010){digitalWrite(11, HIGH);} else {digitalWrite(11, LOW);}
     if (randNumber & 0b0000100){digitalWrite(12, HIGH);} else {digitalWrite(12, LOW);}
     if (randNumber & 0b0001000){digitalWrite(13, HIGH);} else {digitalWrite(13, LOW);}
     delay(2000);

if (buttonState != lastButtonState) {
 
       if (buttonState == HIGH) {
         buttonPushCounter++;
         }
         delay(50);
         }
        lastButtonState = buttonState;
         
        if (buttonPushCounter % randNumber == 0) {
         digitalWrite(10, HIGH);
         }
         else {
           digitalWrite(13, HIGH);
           }
   }

Your code is changing the random number every time around the loop.

instead of doing everything in the loop() function break the program into more functions:

  • void displayBinary(int randomNumber);
  • int readButton();
  • void allOn();
    This function lights all LEDS, (correct Answer)
  • void allOff();
    Bad answer!
loop(){
 int randomNumber =random(1,15);
displayBinary(randomNumber);
int buttonPresses = readButton();
if(buttonPresses == randomNumber) allOn();
else allOff();
delay(2000); 
allOff();
}

Chuck.

So, every time someone presses a button, one of two things may happen

1 - they haven reached the right count yet, do nothing but note that the button was pressed

2 - this button press has reached the count. Generate a new number.

IOW, your sketch gets turned inside-out. The central thing is responding to events - in this case, a button press.

@aragonj5, do not cross-post.

If you want to show the binary of 4 bits then wire your leds and resistors to the low 4 bits of one of the Arduino ports then write the data to that port. You won't have to translate anything.

Simplified routine displays binary value easily adaptable to various bit length by modifying (adding.subtracting) the entries in the array 'pinsLED'.

#define ARRAY_ENTRIES(ARRAY)    (sizeof(ARRAY) / sizeof(ARRAY[0]))

const uint8_t pinBUTTON =  2;

const uint8_t pinLED_1  = 10;
const uint8_t pinLED_2  = 11;
const uint8_t pinLED_4  = 12;
const uint8_t pinLED_8  = 13;

const uint8_t LED_OFF   = LOW;
const uint8_t LED_ON    = HIGH;

const uint8_t pinsLED[] = { pinLED_1, pinLED_2, pinLED_4, pinLED_8 };

void displayBinary(int value)
{
    for ( size_t i = 0; i < ARRAY_ENTRIES(pinsLED); value >>= 1, i++ )
    {
        digitalWrite(pinsLED[i], ((value & 1) ? LED_ON : LED_OFF));
    }
}

void loop()
{
    uint8_t state   = digitalRead(pinBUTTON);
    int     value   = random(1, ((1 << ARRAY_ENTRIES(pinsLED)) - 1));
    displayBinary(value);

    Serial.println(value);

    delay(2000);
}

void setup()
{
    pinMode(pinBUTTON, INPUT);

    for ( size_t i = 0; i < ARRAY_ENTRIES(pinsLED); i++ )
    {
        pinMode(pinsLED[i], OUTPUT);
    }

    Serial.begin(9600);
    
    randomSeed(analogRead(0));
}

Hello and welcome,

Here is a minimalist example using port manipulation, for Arduino UNO or similar. Connect LEDs to pins 8 to 11. Untested because I don't have a Uno... but it should work.

void setup()
{
  randomSeed( analogRead( 0 ) );

  uint8_t randomNumber = random( 15 ) + 1; // get a random number from 1 to 15

  Serial.begin( 9600 );
  Serial.println( randomNumber );

  DDRB |= 0b00001111; // set pins 8 to 11 as outputs
  PORTB = ( PORTB & 0b11110000 ) | randomNumber; // write the random number to pins 8 to 11
}

void loop()
{
}

If you want to learn about port manipulation, see here and these images: Uno, Mega.

If you run a 168/328 on internal clock, port B has 8 free pins, it could do bytes.
Yes it's half-fast in one way but it's 3.3V compatible and needs 3 fewer parts.
And it can do parallel xfers.

The port use occurred to me because the hardware is binary, this shows the bits directly in order.

I think that for 8/16/etc bits, using shift registers is probably the way to fly. A 40-pin AVR would cost more. >:(

Learn to read hex digits and know the bits, after a while it's a lot easier to read data as hex.