/*
Arduino Forum
Topics: Lights code problem
Category: Using Arduino
Sub-Category: Programming Questions
Link: https://forum.arduino.cc/t/8x8-led-spectrum-analyzer-faint-threshold-value/1138147/8
*/
#include "LedControl.h" // Library for MAX7219 LED display
// Pin definitions for MAX7219
#define msg7RESET 13
#define msg7Strobe 9
#define msg7DCout A7
// Pin definitions for LEDs and trim pots
#define maxDataIn 10
#define maxLoad 11
#define maxCLK 12
#define led0 2
#define led1 3
#define led2 4
#define led3 5
#define led4 6
#define led5 7
#define led6 8
int TRIM_POT_A[7] = {A0, A1, A2, A3, A4, A5, A6};
// LED logic levels
#define LED_LOW HIGH // Logic level for turning LED off (adjusted from LOW to HIGH)
#define LED_HIGH LOW // Logic level for turning LED on (adjusted from HIGH to LOW)
#define CYCLE_DELAY 50 // Delay for LED cycle (adjusted based on relay type)
LedControl lc = LedControl(maxDataIn, maxCLK, maxLoad, 1); // Initializing the LedControl library
/* we always wait a bit between updates of the display */
unsigned long delaytime = 100;
byte ledPins[7] = {led0, led1, led2, led3, led4, led5, led6};
byte level[8] = {128, 192, 224, 240, 248, 252, 254, 255};
byte setlevel[8] = {128, 64, 32, 16, 8, 4, 2, 1};
unsigned int valueMSG[7];
unsigned int potValue_A[7] = {0};
byte potSet_A[7] = {0};
void setup() {
Serial.begin(115200); // Initializing serial communication
// MAX7219 initialization
pinMode(msg7RESET, OUTPUT);
pinMode(msg7Strobe, OUTPUT);
digitalWrite(msg7Strobe, HIGH);
digitalWrite(msg7RESET, LOW);
// LED and trim pot initialization
for (int i = 0; i < 7; i++) {
pinMode(ledPins[i], OUTPUT);
digitalWrite(ledPins[i], LED_LOW); // Set initial LED state to off
}
lc.shutdown(0, false); // Disable power-saving mode for LED matrix
lc.setIntensity(0, 1); // Set the brightness to a medium value
lc.clearDisplay(0); // Clear the display
}
void loop() {
// MAX7219 initialization sequence
digitalWrite(msg7Strobe, HIGH);
digitalWrite(msg7RESET, HIGH);
digitalWrite(msg7RESET, LOW);
delayMicroseconds(72);
// Main loop for reading potentiometer and spectrum data
for (int x = 0; x < 7; x++) {
// Reading potentiometer values
potValue_A[x] = analogRead(TRIM_POT_A[x]);
potSet_A[x] = map(potValue_A[x], 0, 1023, 0, 7);
// Reading spectrum data and updating LED matrix
digitalWrite(msg7Strobe, LOW);
delayMicroseconds(36);
int spectrumRead = analogRead(msg7DCout);
valueMSG[x] = map(spectrumRead, 0, 1023, 0, 7);
byte data = setlevel[potSet_A[x]] | level[valueMSG[x]];
lc.setRow(0, x, data);
digitalWrite(msg7Strobe, HIGH);
delayMicroseconds(72);
// Setting LED lights based on spectrum and potentiometer values
if (valueMSG[x] >= potSet_A[x]) {
digitalWrite(ledPins[x], LED_HIGH); // Turn LED on
} else {
digitalWrite(ledPins[x], LED_LOW); // Turn LED off
}
// Printing debug information
Serial.print("x = ");
Serial.println(x);
Serial.print("spectrumRead = ");
Serial.println(spectrumRead);
Serial.print("Potentiometer Value = ");
Serial.println(potValue_A[x]);
Serial.print("valueMSG[x] = ");
Serial.println(valueMSG[x]);
Serial.print("Potentiometer Setting = ");
Serial.println(potSet_A[x]);
Serial.print('n');
}
delay(CYCLE_DELAY); // Pause to allow the LEDs to be on or off for a bit of time
}