Need help to combine two different task using LED matrix

Hello Guys,

I have built two Arduino projects using an LED matrix (MAX7219). I need help and guidance on connecting two projects to a single UNO.

Task1:
By varying ultrasonic range from 0 to 400, rows from 0 to 7 of LED matrix will glow.

#include "LedControl.h"
#include "binary.h"
/* DIN connects to pin 12 CLK connects to pin 11 CS connects to pin 10 */
LedControl lc=LedControl(12,11,10,1);
#define echoPin 2 // attach pin D2 Arduino to pin Echo of HC-SR04
#define trigPin 3 //attach pin D3 Arduino to pin Trig of HC-SR04 // defines variables
int duration; // variable for the duration of sound wave travel
int distance;//variable for the distance measurement
byte dot[8]= {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};

void setup()
{
	lc.shutdown(0,false); //sets up 8x8 matrix
	lc.setIntensity(0,16); // Set brightness to a medium value
	lc.clearDisplay(0); // Clear the display
	pinMode(trigPin, OUTPUT); // Sets the trigPin as an OUTPUT
	pinMode(echoPin, INPUT); // Sets the echoPin as an INPUT
}

void printByte(byte character [])
{
  int i = 0;
  for(i=0;i<8;i++)
	{
    lc.setRow(0,i,character[i]);
  }
}

void loop()
{
	digitalWrite(trigPin, LOW);
	delayMicroseconds(2);// Sets the trigPin HIGH (ACTIVE) for 10 microseconds
	digitalWrite(trigPin, HIGH);
	delayMicroseconds(10); //Creates a ten microsecond delay
	digitalWrite(trigPin, LOW); // Reads the echoPin, returns the sound wave travel time in microseconds

	duration = pulseIn(echoPin, HIGH);// Calculating the distance
	distance = duration * 0.034 / 2; // Speed of sound wave divided by 2 (go and back)
  {
	int scale = map(distance, 0 , 400, 0, 8);  // Mapping 0 to 400CM with 0 to 8 rows
	for (int i=0;i<8;i++){
		if (i<= scale){
			dot[i] = 0xff;
		}
		else{
			dot[i] = 0x00;
		}
	}
  }
	printByte(dot);
	delay(100);

Task2:
By adjusting potentiometer, 2 panels of LED matrix will light up row wise.
POT value 0 to 512 - panel 1 will light up
POT value 512 to 1023 - panel 2 will light up

#include <LedControl.h>
int DIN = 11;
int CS =  10;
int CLK = 13;
LedControl lc = LedControl(DIN, CLK, CS, 2);

void setup() {
  Serial.begin(115200);
  lc.shutdown(0, false);
  lc.setIntensity(0, 50);
  lc.clearDisplay(0);
}

void printByte(byte character [], int panel) {
  int i = 0;
  for (i = 0; i < 8; i++) {
    lc.setRow(panel, i, character[i]);
  }
}

void loop() {
  int panel; // which panel to use
  byte cero[8] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
  int value = analogRead(A0); // read of potentiometer value
  int scale = map(value, 0, 1023, 0, 17); // 16 rows, plus one

  if (value > 512) { // if raw potentiometer is above middle, use panel 0, subtract 8 for row 
    panel = 0;
    scale = scale - 8;
  } else { // if raw potentiometer is below middle, use panel 1, subtract row from 8
    panel = 1;
    scale = 8 - scale;
  }
  for (int i = 0; i < scale; i++) {
    cero[i] = 0xff;
  }
  printByte(cero, panel);
  // delay(100);

Here in this projects, I have used different panels of MAX7219.

The code you posted for both tasks is incomplete.

Post your attempt to combine them and explain what the problems are.

Hi Paul,
Thanks for the reply.
FYI, I got output for both tasks. I want to connect them in single UNO, but I have no idea how to combine them

You need to merge the 2 codes into a single code which achieves whatever pattern of LEDs you need.

It's hard for anyone to help with more detail because you have not made it clear how you want to make the led patterns. How many 8x8 panels will your project use? How will the pattern of LEDs look as the pot and distance sensor values change? Your attempt to explain that is very ambiguous.

Totally 3 8x8 panels will be used. 1 panel for task1 and the remaining panels for task2.

Task1: If I vary the ultrasonic range from 0 to 400cm, LED matrix will start to light up from row 0 to row 7
ultrasonicwithLEDmatrix-WokwiESP32STM32ArduinoSimulator2-ezgif.com-video-to-gif-converter

Task2: Using Potentiometer
Value range from 512 to 0 - panel 1 will glow like row 0 to row 7
Value range from 512 to 1024 - panel 2, row 0 to row 7
ledmatrix-WokwiESP32STM32ArduinoSimulator2-ezgif.com-video-to-gif-converter

Hope I explained in detail.

Ok, that's somewhat clearer. Should be quite straight-forward I think.

Have you thought how you will connect 3 panels to same Arduino? The easiest way should be to chain them together, I think.

The code is here:

I see. So today, @noorulameen has come back to the forum asking to be given another fish?

Hi Paul,
No fish, just dealing with some prototypes involving LED panels and Arduino. That's all

@noorulameen did you understand my reference to fish?

no idea :roll_eyes:

Popular saying.
Teach how to fish and don't give away the fish.

1 Like

You were given "a fish" (some code to solve your problem) when you previously asked for help on this forum.

Now you seem to be waiting to be given another fish. You seem to be making no effort to learn to fish. The forum would prefer that you lean, and wants to help you learn. But it takes 2 to Tango.

Do you understand my reference to Tango?

I have no problem giving away fish in the short term. But doing so can quickly create a dependency! That's not good for anyone.

Ironically, I don't eat fish myself, don't like the smell. :wink:

1 Like

Fish have noses?

Yes, or at least, they have nostrils (called nares). But unlike us they don't/can't breathe through their nostrils.

1 Like

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