Displaying a text in binary: My code is not working

Hello!

The purpose of my code and project is to display a text sequence (the one stored in the text String) byte by byte on 8 LEDs.
I uploaded my code on the attiny 84a I am using for this project.( I tested the attiny setup with a simpler code and the LEDs light up correctly.)
There wasn't any error message; the code uploaded succesfuly. However it is not working as expected and I can't figure out where the flaw could be.

Thanks in advance!

int led0 = 6;
int led1 = 7;
int led2 = 5;
int led3 = 8;
int led4 = 0;
int led5 = 2;
int led6 = 9;
int led7 = 10;

String text = "01110100011001010111001101110100";

void setup() {
  pinMode(led0, OUTPUT);
  pinMode(led1, OUTPUT);
  pinMode(led2, OUTPUT);
  pinMode(led3, OUTPUT);
  pinMode(led4, OUTPUT);
  pinMode(led5, OUTPUT);
  pinMode(led6, OUTPUT);
  pinMode(led7, OUTPUT);
}

void loop() {

  int x = text.length();

  for(int i = 0; i <= x + 1; i++){
    switch (i % 8) {
      case 0:
        if (text.charAt(i) == "1"){
          digitalWrite(led0, HIGH);
        }
        break;
      case 1:
        if (text.charAt(i) == "1"){
          digitalWrite(led1, HIGH);
        }
        break;
      case 2:
        if (text.charAt(i) == "1"){
          digitalWrite(led2, HIGH);
        }
        break;
      case 3:
        if (text.charAt(i) == "1"){
          digitalWrite(led3, HIGH);
        }
        break;
      case 4:
        if (text.charAt(i) == "1"){
          digitalWrite(led4, HIGH);
        }
        break;
      case 5:
        if (text.charAt(i) == "1"){
          digitalWrite(led5, HIGH);
        }
        break;
      case 6:
        if (text.charAt(i) == "1"){
          digitalWrite(led6, HIGH);
        }
        break;
      case 7:
        if (text.charAt(i) == "1"){
          digitalWrite(led7, HIGH);
          delay(3000);
          digitalWrite(led0, LOW);
          digitalWrite(led1, LOW);
          digitalWrite(led2, LOW);
          digitalWrite(led3, LOW);
          digitalWrite(led4, LOW);
          digitalWrite(led5, LOW);
          digitalWrite(led6, LOW);
          digitalWrite(led7, LOW);
        }
        break;

    }
  }

  
}

... not as expected.. That tells nothing. Try again.

1 Like

What are your thoughts on making your FOR loop index beyond the bounds of the text string?

1 Like
const byte ledCount = 8;
const byte led[ledCount] = {6, 7, 5, 8, 0, 2, 9, 10};

const byte textLen = 4;
const byte text[textLen] = { 
  0b01110100,
  0b01100101,
  0b01110011,
  0b01110100
}; 

const byte lowHigh[2] = {LOW, HIGH};

void setup() {
  for(byte n=0; n<ledCount; n++)
    pinMode(led[n], OUTPUT);
}

void loop() {
  for(byte i=0; i<textLen; i++) {
    for(byte n=0; n<ledCount; n++)
      digitalWrite(led[n], lowHigh[bitRead(text[i], n)]);
    delay(3000);
  }
}
2 Likes

@ALEXARDELEANU

You have very inefficient code.
Firstly, it would be more correct to organize the pins for the LEDs in an array, then you won’t need switch-case to access them, but simply give a pin by index

Secondly, in a computer, all data is ALREADY stored in binary form, so it would be more correct to store text as text. not as a string of zeros and ones.

To extract bits from bytes of text, use bitwise operations.

Addition:
While I was writing, @PaulRB already posted an example of such code :slight_smile:

1 Like

It is not working at all, as in the LEDs do not light up at all.

Thanks for pointing out these aspects.

I've been reading about a null element at the end of a string and I wasn't sure if it is included in the output of .length(). Nevertheless, it was irrelevant, as the LEDs didn't work not even for the first iterations of the for loop.

Thanks for the suggestions. It looks very promising, however, I'm not so familiar with the syntax you used so I'm not able to figure out why I get this error message when trying to upload the code:

sketch_dec24c:1:16: error: 'ledCount' was not declared in this scope
 const byte led[ledCount] = {6, 7, 5, 8, 0, 2, 9, 10};
                ^~~~~~~~
C:\Users\alexa\AppData\Local\Temp\arduino_modified_sketch_884528\sketch_dec24c.ino:1:16: note: suggested alternative: 'lround'
 const byte led[ledCount] = {6, 7, 5, 8, 0, 2, 9, 10};
                ^~~~~~~~
                lround
C:\Users\alexa\AppData\Local\Temp\arduino_modified_sketch_884528\sketch_dec24c.ino: In function 'void setup()':
sketch_dec24c:14:19: error: 'ledCount' was not declared in this scope
   for(byte n=0; n<ledCount; n++)
                   ^~~~~~~~
C:\Users\alexa\AppData\Local\Temp\arduino_modified_sketch_884528\sketch_dec24c.ino:14:19: note: suggested alternative: 'lround'
   for(byte n=0; n<ledCount; n++)
                   ^~~~~~~~
                   lround
sketch_dec24c:15:13: error: 'led' was not declared in this scope
     pinMode(led[n], OUTPUT);
             ^~~
C:\Users\alexa\AppData\Local\Temp\arduino_modified_sketch_884528\sketch_dec24c.ino: In function 'void loop()':
sketch_dec24c:20:21: error: 'ledCount' was not declared in this scope
     for(byte n=0; n<ledCount; n++)
                     ^~~~~~~~
C:\Users\alexa\AppData\Local\Temp\arduino_modified_sketch_884528\sketch_dec24c.ino:20:21: note: suggested alternative: 'lround'
     for(byte n=0; n<ledCount; n++)
                     ^~~~~~~~
                     lround
sketch_dec24c:21:20: error: 'led' was not declared in this scope
       digitalWrite(led[n], lowHigh[bitRead(text[i], n)]);
                    ^~~
exit status 1
'ledCount' was not declared in this scope

Schematics please.....



Arduino as ISP is connected as in the official example:
pins MOSI, MISO and SCK are the same pins as digital pin 11, 12 and 13, respectively.(https://docs.arduino.cc/built-in-examples/arduino-isp/ArduinoISP)

But I am quite sure the problem comes from my code as with a test sketch that just pulled the LED pins high, the hardware worked just fine:

type orint led0 = 6;
int led1 = 7;
int led2 = 5;
int led3 = 8;
int led4 = 0;
int led5 = 2;
int led6 = 9;
int led7 = 10;


void setup() {
  pinMode(led0, OUTPUT);
  pinMode(led1, OUTPUT);
  pinMode(led2, OUTPUT);
  pinMode(led3, OUTPUT);
  pinMode(led4, OUTPUT);
  pinMode(led5, OUTPUT);
  pinMode(led6, OUTPUT);
  pinMode(led7, OUTPUT);
}

void loop() {
  digitalWrite(led0, HIGH);
  delay(1000);
  digitalWrite(led0, LOW);
  digitalWrite(led1, HIGH);
  delay(1000);
  digitalWrite(led1, LOW);
  digitalWrite(led2, HIGH);
  delay(1000);
  digitalWrite(led2, LOW);
  digitalWrite(led3, HIGH);
  delay(1000);
  digitalWrite(led3, LOW);
  digitalWrite(led4, HIGH);
  delay(1000);
  digitalWrite(led4, LOW);
  digitalWrite(led5, HIGH);
  delay(1000);
  digitalWrite(led5, LOW);
  digitalWrite(led6, HIGH);
  delay(1000);
  digitalWrite(led6, LOW);
  digitalWrite(led7, HIGH);
  delay(1000);
  digitalWrite(led7, LOW);
  
}
 paste code here

Did you try the last version of the @PaulRB code? He was edited it recently.
As far I see the code should works.

Looks like you may be missing the first line. Copy the code from post #4 again.

the edited version works. Thanks!

Thank you all very much!
Happy Holidays!

If you have any questions about how that code works, please ask. I want you to understand it.

I was aware of the fact that my initial code was not the most efficient, but it was the only one I could come up with. After reviewing the solution you mentioned I really makes sense and I also learned some quite useful new syntax. Thanks again for the help and lesson!

1 Like

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