Troubles with two programs

Hi everybody. I'm working on this project. This code has two programs in one, and the problem is: why is my led matrix display showing the string but i can't change the color of my ws2812b strip?

#include "FastLED.h"

//FIXED GLOBAL VARS
#define LED_TYPE NEOPIXEL  //define type of led
#define NUM_LEDS 10  //num of leds in strip
//#define DATA_PIN 11  //Define led data pin in
//#define Button 8

#define DATA_PIN 11  //for noiasca
#define Button 9


bool oldState = HIGH;
int showType = 0;

CRGB leds[NUM_LEDS];  // Initialize LED array
CRGBPalette16 currentPalette;

uint8_t hue = 0;


char msg[] ="BONNYS PRINTS  ";//Change the text here.
int scrollspeed=12;//Set the scroll speed ( lower=faster) c = "CIAO";


int x;
int y;

//Columns
int clockPin1 = 2; //Arduino pin connected to Clock Pin 11 of 74HC595
int latchPin1 = 3; //Arduino pin connected to Latch Pin 12 of 74HC595
int dataPin1 = 4;  //Arduino pin connected to Data Pin 14 of 74HC595

//Rows
int clockPin2 = 5; //Arduino pin connected to Clock Pin 11 of 74HC595
int latchPin2 = 6; //Arduino pin connected to Latch Pin 12 of 74HC595
int dataPin2 = 7;  //Arduino pin connected to Data Pin 14 of 74HC595

//BITMAP
//Bits in this array represents one LED of the matrix
// 8 is # of rows, 6 is # of LED matrices
byte bitmap[8][8]; 

int numZones = sizeof(bitmap) / 8; // One Zone refers to one 8 x 8 Matrix ( Group of 8 columns)
int maxZoneIndex = numZones-1;
int numCols = numZones * 8;

//FONT DEFENITION
byte alphabets[][8] = {
 {0,0,0,0,0},//@ as SPACE
 //{8,28,54,99,65},//<<
 {31, 36, 68, 36, 31},//A
 {127, 73, 73, 73, 54},//B
 {62, 65, 65, 65, 34},//C
 {127, 65, 65, 34, 28},//D
 {127, 73, 73, 65, 65},//E
 {127, 72, 72, 72, 64},//F
 {62, 65, 65, 69, 38},//G
 {127, 8, 8, 8, 127},//H
 {0, 65, 127, 65, 0},//I
 {2, 1, 1, 1, 126},//J
 {127, 8, 20, 34, 65},//K
 {127, 1, 1, 1, 1},//L
 {127, 32, 16, 32, 127},//M
 {127, 32, 16, 8, 127},//N
 {62, 65, 65, 65, 62},//O
 {127, 72, 72, 72, 48},//P
 {62, 65, 69, 66, 61},//Q
 {127, 72, 76, 74, 49},//R
 {50, 73, 73, 73, 38},//S
 {64, 64, 127, 64, 64},//T
 {126, 1, 1, 1, 126},//U
 {124, 2, 1, 2, 124},//V
 {126, 1, 6, 1, 126},//W
 {99, 20, 8, 20, 99},//X
 {96, 16, 15, 16, 96},//Y
 {67, 69, 73, 81, 97},//Z
};

void setup() {
  Serial.begin(115200);
  FastLED.addLeds<WS2812B, DATA_PIN, GRB>(leds, NUM_LEDS).setCorrection(TypicalLEDStrip);
  pinMode(Button, INPUT_PULLUP);
  // put your setup code here, to run once:
  delay(2000); //for safe startup
  FastLED.addLeds<WS2812B, DATA_PIN, GRB>(leds, NUM_LEDS);
  FastLED.setBrightness(25);

    
 pinMode(latchPin1, OUTPUT);
 pinMode(clockPin1, OUTPUT);
 pinMode(dataPin1, OUTPUT);

 pinMode(latchPin2, OUTPUT);
 pinMode(clockPin2, OUTPUT);
 pinMode(dataPin2, OUTPUT);
 
 //Clear bitmap
 for (int row = 0; row < 8; row++) {
   for (int zone = 0; zone <= maxZoneIndex; zone++) {
     bitmap[row][zone] = 0; 
   }
 }
}

//FUNCTIONS
// Displays bitmap array in the matrix
void RefreshDisplay()
{
 for (int row = 0; row < 8; row++) {
   int rowbit = 1 << row;
   digitalWrite(latchPin2, LOW);//Hold latchPin LOW for transmitting data
   shiftOut(dataPin2, clockPin2, MSBFIRST, rowbit);   //Transmit data

   //Start sending column bytes
   digitalWrite(latchPin1, LOW);//Hold latchPin LOW for transmitting data

   //Shift out to each matrix
   for (int zone = maxZoneIndex; zone >= 0; zone--) 
   {
     shiftOut(dataPin1, clockPin1, MSBFIRST, bitmap[row][zone]);
   }

   //flip both latches at once to eliminate flicker
   digitalWrite(latchPin1, HIGH);//Return the latch pin 1 high to signal chip
   digitalWrite(latchPin2, HIGH);//Return the latch pin 2 high to signal chip

   //Wait
   delayMicroseconds(300);
 }
}

// Converts row and colum to bitmap bit and turn it off/on
void Plot(int col, int row, bool isOn)
{
 int zone = col / 8;
 int colBitIndex = x % 8;
 byte colBit = 1 << colBitIndex;
 if (isOn)
   bitmap[row][zone] =  bitmap[y][zone] | colBit;
 else
   bitmap[row][zone] =  bitmap[y][zone] & (~colBit);
}
// Plot each character of the message one column at a time, updated the display, shift bitmap left.
void XProcess()
{
 for (int charIndex=0; charIndex < (sizeof(msg)-1); charIndex++)
 {
   int alphabetIndex = msg[charIndex] - '@';
   if (alphabetIndex < 0) alphabetIndex=0;
   
   //Draw one character of the message
   // Each character is 5 columns wide, loop two more times to create 2 pixel space betwen characters
   for (int col = 0; col < 7; col++)
   {
     for (int row = 7; row > 0; row--)
     {
       // Set the pixel to what the alphabet say for columns 0 thru 4, but always leave columns 5 and 6 blank.
       bool isOn = 0; 
       if (col<5) isOn = bitRead( alphabets[alphabetIndex][col], row-1 ) == 1;
       Plot( numCols-1, row, isOn); //Draw on the rightmost column, the shift loop below will scroll it leftward.
     }
     for (int refreshCount=0; refreshCount < scrollspeed; refreshCount++)
       RefreshDisplay();
     //Shift the bitmap one column to left
     for (int row=7; row > 0; row--)
     {
       for (int zone=0; zone < numZones; zone++)
       {
         //This right shift would show as a left scroll on display because leftmost column is represented by least significant bit of the byte.
         bitmap[row][zone] = bitmap[row][zone] >> 1;
         // Shift over lowest bit from the next zone as highest bit of this zone.
         if (zone < maxZoneIndex) bitWrite(bitmap[row][zone], 7, bitRead(bitmap[row][zone+1],0));
       }
     }
   }
 }
}

void loop() {
  
     XProcess();
  
  bool newState = digitalRead(Button);

  // Check if state changed from high to low (button press).
  if (newState == LOW && oldState == HIGH) {
    // Short delay to debounce button.
    delay(20);
    // Check if button is still low after debounce.
    newState = digitalRead(Button);
    if (newState == LOW) {
      showType++;
      if (showType > 8)
        showType = 0;
      Serial.print(F("new showType=")); Serial.println(showType);
      startShow(showType);
    }
   }

  // Set the last button state to the old state.
  oldState = newState;

  if (showType == 6) rainbowCycle(); // needs to be called permanentely

}

void fill(CRGB newColor)   // there might be an built function in FASTled also...
{
  for (int i = 0; i < NUM_LEDS; i++) {
    leds[i] = newColor;
  }
  FastLED.show();
}

void startShow(int g) {
  switch (g) {
    case 0: fill(CRGB::Red);
      break;
    case 1: fill(CRGB::Black);
      break;
    case 2: fill(CRGB::Green);
      break;
    case 3: fill(CRGB::Yellow);
      break;
    case 4: fill(CRGB::Blue);
      break;
    case 5: fill(CRGB::Purple);
      break;
    case 6:
      // leds[i] = CHSV(hue + (i * 200), 255, 255);
      //rainbowCycle();
      break;
    case 7: fill(CRGB::Aqua);
      break;
    case 8: fill(CRGB::HotPink);
      break;
  }
}

void rainbowCycle() {
  for (int i = 0; i < NUM_LEDS; ++i) {
    leds[i] = CHSV(hue + (i * 10), 255, 255);
  }
  EVERY_N_MILLISECONDS(15) {
    hue++;
  }

  FastLED.show();
     
}

why do you have FastLED.addLeds(...) twice?

what's you circuit? (for the LEDs)
where do you think you set the color?

So, I put the middle pin of ws2812b on pin 11 of arduino.
I wrote two times fastled, for a mistake.
I would like to set color on my ws2812b strip (it has 10 leds). The main problem regarding the conflict of two programs: one controll display matrix (I used HC595 shift register) and other one for change the leds color using a button (on pin 9 of arduino)

what's the conflict ? does the button work ?

take a piece of paper
draw what the circuit look like. identify components
take a picture
post here by dragging and dropping the picture in an answer


Those are my circuits. In the first immage, you can see the circuit attached on my arduino nano (for push button, i opted for touch sensor ttp223*). In the last one, you can see the Led matrix circuit. If you want, I can send you images of pcbs that I made.
Anyway, if I upload the single program on my arduino (change colour leds or show string on led matrix), the program still work without problems, but when I put togheter, only one works

what do you see if you run this:

#include "FastLED.h"
#define LED_TYPE NEOPIXEL  //define type of led
#define NUM_LEDS 10  //num of leds in strip
#define DATA_PIN 11  //for noiasca
#define Button 9

CRGB leds[NUM_LEDS];  // Initialize LED array
CRGBPalette16 currentPalette;

char message[] = "BONNYS PRINTS  ";
int scrollspeed = 12;

int x;
int y;

//Columns
const byte clockPin1 = 2; //Arduino pin connected to Clock Pin 11 of 74HC595
const byte latchPin1 = 3; //Arduino pin connected to Latch Pin 12 of 74HC595
const byte dataPin1  = 4;  //Arduino pin connected to Data Pin 14 of 74HC595

//Rows
const byte clockPin2 = 5; //Arduino pin connected to Clock Pin 11 of 74HC595
const byte latchPin2 = 6; //Arduino pin connected to Latch Pin 12 of 74HC595
const byte dataPin2  = 7;  //Arduino pin connected to Data Pin 14 of 74HC595

byte bitmap[8][8];

const int numZones = sizeof(bitmap) / 8; // One Zone refers to one 8 x 8 Matrix ( Group of 8 columns)
const int maxZoneIndex = numZones - 1;
const int numCols = numZones * 8;

byte alphabets[][8] = {
  {0, 0, 0, 0, 0}, //@ as SPACE
  //{8,28,54,99,65},//<<
  {31, 36, 68, 36, 31},//A
  {127, 73, 73, 73, 54},//B
  {62, 65, 65, 65, 34},//C
  {127, 65, 65, 34, 28},//D
  {127, 73, 73, 65, 65},//E
  {127, 72, 72, 72, 64},//F
  {62, 65, 65, 69, 38},//G
  {127, 8, 8, 8, 127},//H
  {0, 65, 127, 65, 0},//I
  {2, 1, 1, 1, 126},//J
  {127, 8, 20, 34, 65},//K
  {127, 1, 1, 1, 1},//L
  {127, 32, 16, 32, 127},//M
  {127, 32, 16, 8, 127},//N
  {62, 65, 65, 65, 62},//O
  {127, 72, 72, 72, 48},//P
  {62, 65, 69, 66, 61},//Q
  {127, 72, 76, 74, 49},//R
  {50, 73, 73, 73, 38},//S
  {64, 64, 127, 64, 64},//T
  {126, 1, 1, 1, 126},//U
  {124, 2, 1, 2, 124},//V
  {126, 1, 6, 1, 126},//W
  {99, 20, 8, 20, 99},//X
  {96, 16, 15, 16, 96},//Y
  {67, 69, 73, 81, 97},//Z
};

void RefreshDisplay() {
  for (int row = 0; row < 8; row++) {
    int rowbit = 1 << row;
    digitalWrite(latchPin2, LOW);                       // Hold latchPin LOW for transmitting data
    shiftOut(dataPin2, clockPin2, MSBFIRST, rowbit);    // Transmit data

    //Start sending column bytes
    digitalWrite(latchPin1, LOW);                       // Hold latchPin LOW for transmitting data

    //Shift out to each matrix
    for (int zone = maxZoneIndex; zone >= 0; zone--)
      shiftOut(dataPin1, clockPin1, MSBFIRST, bitmap[row][zone]);

    //flip both latches at once to eliminate flicker
    digitalWrite(latchPin1, HIGH);//Return the latch pin 1 high to signal chip
    digitalWrite(latchPin2, HIGH);//Return the latch pin 2 high to signal chip

    //Wait
    delayMicroseconds(300);
  }
}

void Plot(int col, int row, bool isOn) {
  int zone = col / 8;
  int colBitIndex = x % 8;
  byte colBit = 1 << colBitIndex;
  if (isOn)
    bitmap[row][zone] =  bitmap[y][zone] | colBit;
  else
    bitmap[row][zone] =  bitmap[y][zone] & (~colBit);
}

void scrollText() {
  for (size_t charIndex = 0; charIndex < strlen(message); charIndex++)   {
    int alphabetIndex = message[charIndex] - '@';
    if (alphabetIndex < 0) alphabetIndex = 0;

    //Draw one character of the message
    // Each character is 5 columns wide, loop two more times to create 2 pixel space betwen characters
    for (int col = 0; col < 7; col++) {
      for (int row = 7; row > 0; row--) {
        // Set the pixel to what the alphabet say for columns 0 thru 4, but always leave columns 5 and 6 blank.
        bool isOn = false;
        if (col < 5) isOn = bitRead(alphabets[alphabetIndex][col], row - 1 ) == 1;
        Plot( numCols - 1, row, isOn); //Draw on the rightmost column, the shift loop below will scroll it leftward.
      }
      for (int refreshCount = 0; refreshCount < scrollspeed; refreshCount++)
        RefreshDisplay();
      //Shift the bitmap one column to left
      for (int row = 7; row > 0; row--) {
        for (int zone = 0; zone < numZones; zone++) {
          //This right shift would show as a left scroll on display because leftmost column is represented by least significant bit of the byte.
          bitmap[row][zone] = bitmap[row][zone] >> 1;
          // Shift over lowest bit from the next zone as highest bit of this zone.
          if (zone < maxZoneIndex) bitWrite(bitmap[row][zone], 7, bitRead(bitmap[row][zone + 1], 0));
        }
      }
    }
  }
}


void setup() {
  pinMode(latchPin1, OUTPUT);
  pinMode(clockPin1, OUTPUT);
  pinMode(dataPin1, OUTPUT);

  pinMode(latchPin2, OUTPUT);
  pinMode(clockPin2, OUTPUT);
  pinMode(dataPin2, OUTPUT);
  pinMode(Button, INPUT_PULLUP);

  Serial.begin(115200); Serial.println();
  FastLED.addLeds<WS2812B, DATA_PIN, GRB>(leds, NUM_LEDS).setCorrection(TypicalLEDStrip);
  FastLED.setBrightness(25);
  memset(bitmap, 0, sizeof bitmap);

}

void loop() {
  static uint8_t hue = 0;
  scrollText();
  FastLED.showColor(CHSV(hue++, 255, 255));
}

Why do you use 74HC595? It is better to replace them with MAX7219 and so even programming will be easier.

I do not see well your scheme in the picture but it seems to me that it LEDs connection is wrong.

So, I followed an old tutorial on youtube, and I noticed the same project with MAX7219 too late. I order PCBs before saw MAX7219 tutorial. The program run without any problem althought I used HC595

I can't send you a video because I have max 4mb file size, and my video is 75mb size

If you have a YouTube account, you can post there and share the link here

Did it work ?

I posted the video

And what does it look like if you comment out

scrollText();

And add a small delay(50); at the end of the loop?

It doesn't work anyway. I think that the problem is a conflict between two programs inside the void loop. In fact in the void loop, scroll text ends and change color program starts

Ah that’s what you mean…

The function that scrolls does this

So you do go through all the message. Modify the code so that you only push one col and go back to the loop

I tried this, but It didn't work. I don't now why, but I think the problem Is in another part of the code. Anyway, thanks for time that you spend for help me!

Show the code where you tried.

It seems the problem is NOT that it’s not displaying the colors if you use the text but that you only get to the strip once the text has finished to completely scroll in

So it’s all working fine - just not doing what you want and the only way to get there is to modify how vertical patterns are injected and moved so that it’s not blocking until the full text has been handled.

#include "FastLED.h"
#define LED_TYPE NEOPIXEL  //define type of led
#define NUM_LEDS 10  //num of leds in strip
#define DATA_PIN 11  //for noiasca
#define Button 9

CRGB leds[NUM_LEDS];  // Initialize LED array
CRGBPalette16 currentPalette;

char message[] = "BONNYS PRINTS  ";
int scrollspeed = 12;

int x;
int y;

//Columns
const byte clockPin1 = 2; //Arduino pin connected to Clock Pin 11 of 74HC595
const byte latchPin1 = 3; //Arduino pin connected to Latch Pin 12 of 74HC595
const byte dataPin1  = 4;  //Arduino pin connected to Data Pin 14 of 74HC595

//Rows
const byte clockPin2 = 5; //Arduino pin connected to Clock Pin 11 of 74HC595
const byte latchPin2 = 6; //Arduino pin connected to Latch Pin 12 of 74HC595
const byte dataPin2  = 7;  //Arduino pin connected to Data Pin 14 of 74HC595

byte bitmap[8][1];

const int numZones = sizeof(bitmap) / 8; // One Zone refers to one 8 x 8 Matrix ( Group of 8 columns)
const int maxZoneIndex = numZones - 1;
const int numCols = numZones * 8;

byte alphabets[][8] = {
  {0, 0, 0, 0, 0}, //@ as SPACE
  //{8,28,54,99,65},//<<
  {31, 36, 68, 36, 31},//A
  {127, 73, 73, 73, 54},//B
  {62, 65, 65, 65, 34},//C
  {127, 65, 65, 34, 28},//D
  {127, 73, 73, 65, 65},//E
  {127, 72, 72, 72, 64},//F
  {62, 65, 65, 69, 38},//G
  {127, 8, 8, 8, 127},//H
  {0, 65, 127, 65, 0},//I
  {2, 1, 1, 1, 126},//J
  {127, 8, 20, 34, 65},//K
  {127, 1, 1, 1, 1},//L
  {127, 32, 16, 32, 127},//M
  {127, 32, 16, 8, 127},//N
  {62, 65, 65, 65, 62},//O
  {127, 72, 72, 72, 48},//P
  {62, 65, 69, 66, 61},//Q
  {127, 72, 76, 74, 49},//R
  {50, 73, 73, 73, 38},//S
  {64, 64, 127, 64, 64},//T
  {126, 1, 1, 1, 126},//U
  {124, 2, 1, 2, 124},//V
  {126, 1, 6, 1, 126},//W
  {99, 20, 8, 20, 99},//X
  {96, 16, 15, 16, 96},//Y
  {67, 69, 73, 81, 97},//Z
};

void RefreshDisplay() {
  for (int row = 0; row < 8; row++) {
    int rowbit = 1 << row;
    digitalWrite(latchPin2, LOW);                       // Hold latchPin LOW for transmitting data
    shiftOut(dataPin2, clockPin2, MSBFIRST, rowbit);    // Transmit data

    //Start sending column bytes
    digitalWrite(latchPin1, LOW);                       // Hold latchPin LOW for transmitting data

    //Shift out to each matrix
    for (int zone = maxZoneIndex; zone >= 0; zone--)
      shiftOut(dataPin1, clockPin1, MSBFIRST, bitmap[row][zone]);

    //flip both latches at once to eliminate flicker
    digitalWrite(latchPin1, HIGH);//Return the latch pin 1 high to signal chip
    digitalWrite(latchPin2, HIGH);//Return the latch pin 2 high to signal chip

    //Wait
    delayMicroseconds(300);
  }
}

void Plot(int col, int row, bool isOn) {
  int zone = col / 8;
  int colBitIndex = x % 8;
  byte colBit = 1 << colBitIndex;
  if (isOn)
    bitmap[row][zone] =  bitmap[y][zone] | colBit;
  else
    bitmap[row][zone] =  bitmap[y][zone] & (~colBit);
}

void scrollText() {
  for (size_t charIndex = 0; charIndex < strlen(message); charIndex++)   {
    int alphabetIndex = message[charIndex] - '@';
    if (alphabetIndex < 0) alphabetIndex = 0;

    //Draw one character of the message
    // Each character is 5 columns wide, loop two more times to create 2 pixel space betwen characters
    for (int col = 0; col < 7; col++) {
      for (int row = 7; row > 0; row--) {
        // Set the pixel to what the alphabet say for columns 0 thru 4, but always leave columns 5 and 6 blank.
        bool isOn = false;
        if (col < 5) isOn = bitRead(alphabets[alphabetIndex][col], row - 1 ) == 1;
        Plot( numCols - 1, row, isOn); //Draw on the rightmost column, the shift loop below will scroll it leftward.
      }
      for (int refreshCount = 0; refreshCount < scrollspeed; refreshCount++)
        RefreshDisplay();
      //Shift the bitmap one column to left
      for (int row = 7; row > 0; row--) {
        for (int zone = 0; zone < numZones; zone++) {
          //This right shift would show as a left scroll on display because leftmost column is represented by least significant bit of the byte.
          bitmap[row][zone] = bitmap[row][zone] >> 1;
          // Shift over lowest bit from the next zone as highest bit of this zone.
          if (zone < maxZoneIndex) bitWrite(bitmap[row][zone], 7, bitRead(bitmap[row][zone + 1], 0));
        }
      }
    }
  }
}


void setup() {
  pinMode(latchPin1, OUTPUT);
  pinMode(clockPin1, OUTPUT);
  pinMode(dataPin1, OUTPUT);

  pinMode(latchPin2, OUTPUT);
  pinMode(clockPin2, OUTPUT);
  pinMode(dataPin2, OUTPUT);
  pinMode(Button, INPUT_PULLUP);

  Serial.begin(115200); Serial.println();
  FastLED.addLeds<WS2812B, DATA_PIN, GRB>(leds, NUM_LEDS).setCorrection(TypicalLEDStrip);
  FastLED.setBrightness(25);
  memset(bitmap, 0, sizeof bitmap);

}

void loop() {
  static uint8_t hue = 0;
  scrollText();
  FastLED.showColor(CHSV(hue++, 255, 255));
  //delay(50);
}

that code goes through all the text: when you call scrollText() in the loop() you have this

for (size_t charIndex = 0; charIndex < strlen(message); charIndex++) {

so the function will go through all the chars and in the drawing you even have a call to
for (int refreshCount = 0; refreshCount < scrollspeed; refreshCount++) RefreshDisplay();

not to mention the 2 nested for loops for then bits.

you need to break up those loops so that a calls to scrollText does not do everything in one go, but one sub-step at a time, the granularity of which you need to define.

This way the loop() would do A step for the matrix and then one step for the LEDs. That's how you simulate multi-tasking

Ok...i don't understand vert well what I will do. Can you modify the code for me?