LED Binary Code

Hi Im fairly new to programming and I am making a project where I output binary code to the serial monitor and light up certain LEDs but I am having trouble. I need to do a initial loop to make sure all the LEDs are working, then continue with code, and finally output to serial monitor which is not working. Here is my code. Any tips?

Please post your code inside code tag and wiring diagram

Try using switch case statements if there are not to many different values you need to decode. Try this link:
https://www.arduino.cc/en/Reference.SwitchCase
Good Luck & Have Fun!
Gil

int button = 2; // pin to connect the button
int presses = 0; // variable to store number of presses
long time = 0; // used for debounce
long debounce = 100; // how many ms to "debounce"
const byte numPins = 8; // how many leds
int state; // used for HIGH or LOW
long randNumber;// pins to connect leds
byte pins[] = { 5, 6, 7, 8, 9, 10, 11, 12 };




void setup()
{
  /* we setup all led pins as OUTPUT */
  for (int i = 0; i < numPins; i++) {
    pinMode(pins[i], OUTPUT);
  }
  pinMode(button, INPUT);
   /* use pin 2 which has interrupt 0 on Arduino UNO */
  attachInterrupt(0, count, LOW);
}


void loop()
{
  {
    for (int i = 0; i<numPins;i++){
      digitalWrite(pins[i], HIGH);
      delay(550);
      digitalWrite(pins[i], LOW);
      delay(550);  
      continue;}
      
      
  }

  String binNumber = String(presses, BIN); /* convert presses to binary and store it as a string */
  int binLength = binNumber.length(); /* get the length of the string */
  if (presses <= 255) { // if we have less or equal to 255 presses
    for (int i = 0, x = 1; i < binLength; i++, x += 2) {
      if (binNumber[i] == '0') state = LOW;
      if (binNumber[i] == '1') state = HIGH;
      digitalWrite(pins[i] + binLength - x, state);
    }
  }
  else {
    // do something when we reach 255
  }
}

/* function to count the presses */
void count() {
  // we debounce the button and increase the presses
  if (millis() - time > debounce) presses++;
  time = millis();
}

void randomfuncation() {
  randNumber = random(0, 255); // generate random number between 1 & 5
  Serial.println(9600); // show the value in Serial Monitor
  delay(1000); // wait 1 second before displaying the next number

I found two problem in your code:

  1. attachInterrupt() for input pin is is not correctly used. See example in attachInterrupt() - Arduino Reference. You SHOULD pull-up the input pin

  2. In count() function, you debounce incorrectly. See debouncing example

Thank you!

Just need pointers on how I should approach this, I have a bit of code so far but here is what I need to do:

  • Create a function to verify whether the connections are working properly at each port. The void function MUST display sequentially each LED turning ON in intervals of 500 ms. After all LEDs are turned ON turn
    OFF all LEDs

  • Create a function that converts an integer number (in the range 0 - 255) into a binary sequence
    of 8 bits by turning ON/OFF each LED: if the bit is “1” turn ON the corresponding LED. If the
    bit is “0” turn OFF the corresponding LED. Also create a “check-test” to verify whether the
    binary conversion was carried out correctly (see example below). Display on the “serial
    monitor”: the generated random number, the binary sequence, and the check-test

I linked example

//LED Pin Variables
int ledPins[] = {2,3,4,5,6,7,8,9}; //An array to hold the pin each LED is connect
long randNumber;

void setup()
{
  Serial.begin(9600);
  //Set each pin connected to an LED to output mode (pulling high (on) or low (off)
  for(int i = 0; i < 8; i++){         //this is a loop and will repeat eight times
      pinMode(ledPins[i],OUTPUT); //we use this to set each LED pin to output
  } 
  randomSeed(analogRead(0));
}

void loop()                   
{
  randNumber = random(0,256);
  Serial.println(randNumber);
  for (int i=(randNumber); i<256; i++) {
    showBinNumber(randNumber);
    delay(256-i);
    delay(2000);
  }
}
void showBinNumber(int randNumber) {
  for (int i=0; i<8; i++) {
    if (randNumber%2)
      digitalWrite(ledPins[i], HIGH);
    else
      digitalWrite(ledPins[i], LOW);
    randNumber/=2; 
  }
}

Capture4.PNG

you where not really specific on what part of the project you need help on.

is this what you need?

   byte mybyte = 123;

 for(int i = 0; i < 8; i++){  
    digitalWrite(ledPins[i],bitRead(mybyte,i));
    delay(1000);
    }

you can use bitread() in a loop to grab all the bits in a byte and assign them to your pins

I don't understand what anything random has to do with this.

A "walking one test" (as taterking showed you) or a walking zero test is the standard solution for finding opens & shorts. It will find a short between pins as well as a short to power or ground.

Where I work we have test programs for some of our boards that test the I/O ports that way. Typically, there is a walking-one and a walking-zero test. Or in some cases the programs count from 0-256, testing all of the possibilities, but that's not necessary to find a failure. In either case, the results are displayed in hex (because it's easy to read and easy to convert between binary and hex in your head).

But if there is a possibility of a short the Arduino outputs , they need to be protected (usually with resistors) because a high-output shorted to a low-output can damage the Arduino.

I need help with the addition of a test loop to make sure that led work, that then continue on with program. I was wondering how exactly I output it all like in the instructions. The random(0,256) is there so that a random number will be shown by the LEDS and outputted to serial monitor along with binary sequence and a check list. I attached the guidelines.

When is our assignment due?

Hi I had made a prior post but I guess I provided too little information to help. I have a code that will convert a random integer to binary then output it through 8 LEDs. The problem is I am not that knowledgeable when it comes to coding and I am trying to do an initial loop that turns the LEDs off and then on to show they work. Then a random number is inputted and displayed by the LEDs in a 8 byte binary code. So far I can get the random number to generate and output on LEDs as well as show the number on the serial monitor, but I also need to display the binary sequence and a test to make sure the number is correctly outputted to binary. Guidelines to steps are attached in png.

Also here

It is due next week

Why have we left it so late?

Why have we started a new topic?

instead of running your leds to ground in your circut, you could run them to another digital pin.
and then test if you are receiving voltage when sending power to each led.
this would test each pin and pause the scetch if an LED becomes disconnected:

void setup(){
  byte mybyte = 123;
  pinMode(10,INPUT);

 for(int i = 0; i < 8; i++){ 
     for(int i2 = 0; i2 < 8; i2++){ 
     if(i==i2){digitalWrite(ledPins[i],HIGH);}
     else{digitalWrite(ledPins[i],LOW);}}

     if(digialRead(10)==LOW){
    Serial.print("please check LED to continue: ");
    Serial.print(i);
    while(digialRead(10)==LOW){}
    }
}}

Double posting is frowned upon - can waste peoples time - one post with a clear description of what happens or not happens - code (you did do that in the other thread) - schematic (hand drawn is fine) etc.

Also learn a bit of patience - in your other thread you double posted in less than 2 hours - the people that help here are not paid and depending on the problem it might be a bit of time before someone with the knowledge to help you drops by.

Have DELETED one cross post and merged two others.

Please do NOT cross post / duplicate as it wastes peoples time and efforts to have more than one post for a single topic.

As you seem unable to comply with an earlier request from another member you now have 7 days to read the link provided below

Please READ THIS POST to help you get the best out of the forum.

Bob.

Hi,

The void function MUST display sequentially each LED turning ON in intervals of 500 ms. After all LEDs are turned ON turn
OFF all LEDs

Try this as your setup

void setup()
{
  /* we setup all led pins as OUTPUT */
  for (int i = 0; i < numPins; i++)
  {
    pinMode(pins[i], OUTPUT);
    digitalWrite(pins[i], HIGH);
    delay(500);
  }
  for (int i = 0; i < numPins; i++)
  {
    digitalWrite(pins[i], LOW);
  }
  pinMode(button, INPUT);
  /* use pin 2 which has interrupt 0 on Arduino UNO */
  attachInterrupt(0, count, LOW);
}

As you only need to do it at startup and only once, you can strobe your LEDs in the void setup() part of the program.
BUT you cannot use pins 0 and 1 as outputs, these are the controller programming pins.
What model Arduino are you using?
Tom.... :slight_smile:

TomGeorge:
What model Arduino are you using?

We may - indeed will - be waiting a week or more for the answer ... :astonished: