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.