2

I'm doing my first project with Arduino, however I got in a block and cannot figure out what I am doing wrong.

This project is 'simple'. It's that Stranger Thing's Wall.

However, I want to use the 74HC595 to leave pins free on the arduino for maybe expand and do something else with that too, so, I tried doing the arduino tutorial (https://www.arduino.cc/en/Tutorial/Foundations/ShiftOut)

So my current schematic is this one below

enter image description here

My code, however, is kind of a frankenstein. I used the shiftOut from the tutorials, however I must confess I do not know how it works (And I think its where the error may lie). I do know VBA and Python, but I know nothing of C (Or whichever this language is)

    //Pin connected to ST_CP of 74HC595
#define latchPin 5
//Pin connected to SH_CP of 74HC595
#define clockPin 7
////Pin connected to DS of 74HC595
#define dataPin 6
String text;                                                    //Variável do tipo string que receberá o texto
void setup() {
  Serial.begin(9600);                                           //Velocidade de comunicação
  for (int i = 2; i < 7; i++) {
    pinMode(i, OUTPUT);                                   //Declarando o pino como saída
  }
  Serial.println("Iniciando");
  inicializationEffect();                                       //Chamada da Função responsável pelo efeito inicial
  //basicTest();
}
void loop() {
  

  receiveText();                                               //Chamada da função responsável por receber o texto digitado
}
//Função que recebe o texto diitado
void receiveText() {
  while (Serial.available() > 0) {                              //Verifica se existe alguma informação na Serial
    char c = Serial.read();                                     //atribui cada caractere a variável c que é do tipo char
    if (c != '\n') text.concat(c) ;                              //verifica se a variável do tipo char recebeu uma quebra de linha que indica o fim da palavra ou frase e concatena todos os caracteres na String text
    delay(10);                                                  //aguarda 10 milissegundos
  }
  if (text.length() > 0) {                                      //verifica se a string text tem tamanho maior que 0
    text.toUpperCase();                                       //transforma tudo que estiver na string text para maiúsculo
    if (text[0] == '#'){
      waveEffect();
    }else {
      executeEffect(text);                                        //chama a função executeEffect passando a string text como parâmetro
    }
  }
  text = "";                                                    //limpa a string text
}
//Função responsável por ativar linha que recebe como parâmetro o número da linha
void lineActivation(int line, int keepalive) {
  if (keepalive == 0) {
    for (int i = 2; i <= 4; i++) {
      digitalWrite(i, LOW);
    }
  }
    digitalWrite(line+1, HIGH);                               //coloca a linha 1 em estado lógico alto
}

void activateLetter(char letter, int on, int off) {
  int lt;
  lt = (((int)letter - 64) % 9);
  if (((int)letter - 64) > 8) {
    lt = lt + 1;
  }
  if (on) {
    callCol(1<<lt-1);
    delay(on);                                                //mantém o pino em nível lógico alto durante o tempo que foi determinado no parâmetro da função
  }
  if (off) {
    callCol(0);
    delay(off);                                               //mantém o pino em nível lógico baixo durante o tempo que foi determinado no parâmetro da função
  }

}

void callCol(int col){
  digitalWrite(latchPin, LOW);
  shiftOut(dataPin, clockPin, 0);
  shiftOut(dataPin, clockPin, col);
  digitalWrite(latchPin, HIGH);

}
void executeEffect(String text) {
  for (int i = 0; i < text.length(); i++) {
    if (text[i] == ' ') {
      delay(1000);
    } else if ((int)text[i] < 65 or (int)text[i] > 90) {
    } else {
      lineActivation(int(((int)text[i] - 64) / 9) + 1, 0);
      activateLetter(text[i], 1000, 200);
    }
  }
}

void inicializationEffect() {
  for (int i = 1; i < 27; i++) {
    lineActivation(int(i / 9) + 1, 0);                          //ativa a linha
    activateLetter(char(64 + i), 1000, 10);                   //ativa a letra
  }
}
void waveEffect() {
  for (int i = 1; i < 4; i++) {
    lineActivation(i, 1);                          //ativa a linha
  }
  for (int i = 1; i < 10; i++) {
    activateLetter(char(72 + i), 100, 0);
  }

  for (int i = 1; i < 10; i++) {
    activateLetter(char(72 + i), 0, 100);
  }
  for (int i = 1; i < 4; i++) {
    lineActivation(i, 1);                          //ativa a linha
  }
}

void basicTest() {
  lineActivation(2,1);
    for (int i = 0; 1 < 500; i++) {
    callCol(255-i);
    delay(100);
    }
    
  
}


void shiftOut(int myDataPin, int myClockPin, byte myDataOut) {
// This shifts 8 bits out MSB first,
//on the rising edge of the clock,
//clock idles low
//internal function setup
int i=0;
int pinState;
pinMode(myClockPin, OUTPUT);
pinMode(myDataPin, OUTPUT);
//clear everything out just in case to
//prepare shift register for bit shifting
digitalWrite(myDataPin, 0);
digitalWrite(myClockPin, 0);
//for each bit in the byte myDataOut&#xFFFD;
//NOTICE THAT WE ARE COUNTING DOWN in our for loop
//This means that %00000001 or "1" will go through such
//that it will be pin Q0 that lights.
for (i=7; i>=0; i--)  {
digitalWrite(myClockPin, 0);
//if the value passed to myDataOut and a bitmask result
// true then... so if we are at i=6 and our value is
// %11010100 it would the code compares it to %01000000
// and proceeds to set pinState to 1.
if ( myDataOut & (1<<i) ) {
pinState= 1;
}
else {
pinState= 0;
}
//Sets the pin to HIGH or LOW depending on pinState
digitalWrite(myDataPin, pinState);
//register shifts bits on upstroke of clock pin
digitalWrite(myClockPin, 1);
//zero the data pin after shift to prevent bleed through
digitalWrite(myDataPin, 0);
}
//stop shifting
digitalWrite(myClockPin, 0);
}

If I understood right (Which I may not) theoretically, when I send a value over 255, it should "overflow" to the next 74HC595, which would be used to light the ninth column.

4
  • 1
    why shiftOut(dataPin, clockPin, 0);? – Juraj Nov 14 '20 at 6:15
  • To ensure its clear, however I removed that line for the solution I posted – Feeds Nov 17 '20 at 14:50
  • 1
    where did you get that idea? – Juraj Nov 17 '20 at 14:54
  • I mean, I was wrong. But the point was to always set the Binary value to 0 before setting it to the correct value. Just as a Failsafe, however the more I deal with this I realize the less I know. It doesnt need a failsafe, right? – Feeds Nov 17 '20 at 14:56
3

Solved using https://arduino.stackexchange.com/a/17093/37827

By removing the custom shiftOut function and changing my

shiftOut(dataPin, clockPin, 0);
shiftOut(dataPin, clockPin, col);

with

shiftOut(dataPin, clockPin, MSBFIRST, highByte(col));  
shiftOut(dataPin, clockPin, MSBFIRST, lowByte(col));  

Works like a charm

PS: Feel free to suggest something else, but this way it fixed my problem

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.