Hello,
I have hooked up 2 Arduino Uno's to a button panel and DMD.
The Master controls buttons and displays values (increasing by 1's like a counter) on the DMD depending on which button is pressed.
I would like the Slave to know when a specific button is pressed, then flash an LED as the numbers on the DMD change.
Currently, i have it so when a button is pressed, a corresponding LED (hooked up to the Slave) stays on when the others flash off.
Example: LED's are on in standby...When button 1 is pressed, the DMD counts from 0-25. After 25 is reached, THEN LED's 2, 3, and 4 flash off while LED 1 stays on. All 4 LED's then return to an ON state.
see the video for reference:
What i would like to happen is as follows:
LED's are on in standby...when button 1 is pressed, the DMD counts from 0-25, and only LED 1 flashes on then off with every number change on the DMD. After the DMD reaches 25, all 4 LED's then turn back on.
Here is the Master Code:
#include <SD.h>
#include "SPI.h"
#include "DMD.h"
#include "TimerOne.h"
#include "Arial_black_16.h"
#include <Wire.h>
const int buttonPin = 2;
int buttonState = 0;
int L1 = 14;
int L2 = 15;
int L3 = 16;
int L4 = 17;
int score;
int totalscore;
int rep1;
int rep2;
int rep3;
int rep4;
int flash1;
//Fire up the DMD library as dmd
#define DISPLAYS_ACROSS 1
#define DISPLAYS_DOWN 1
DMD dmd(DISPLAYS_ACROSS, DISPLAYS_DOWN);
void ScanDMD()
{
dmd.scanDisplayBySPI();
}
const byte PanelWidth = 32;
const byte MaxStringLength = 5;
char CharBuf[MaxStringLength + 1];
// the setup routine runs once when you press reset:
void setup() {
Serial.begin(9600);
pinMode(buttonPin, INPUT);
pinMode(L1, INPUT_PULLUP);
pinMode(L2, INPUT_PULLUP);
pinMode(L3, INPUT_PULLUP);
pinMode(L4, INPUT_PULLUP);
//initialize TimerOne's interrupt/CPU usage used to scan and refresh the display
Timer1.initialize( 5000 ); //period in microseconds to call ScanDMD. Anything longer than 5000 (5ms) and you can see flicker.
Timer1.attachInterrupt( ScanDMD ); //attach the Timer1 interrupt to ScanDMD which goes to dmd.scanDisplayBySPI()
dmd.selectFont(Arial_Black_16);
Wire.begin();
}
byte x = 0;
byte y = 0;
byte z = 0;
byte zz = 0;
void loop() {
Wire.beginTransmission(8); // transmit to device #8
Wire.write(x); // sends one byte
Wire.write(y); // sends one byte
Wire.write(z); // sends one byte
Wire.write(zz); // sends one byte
Wire.endTransmission(); // stop transmitting
buttonState = digitalRead(buttonPin);
int light1 = analogRead(L1);
int light2 = analogRead(L2);
int light3 = analogRead(L3);
int light4 = analogRead(L4);
String displayString = String(totalscore);
center_theDisplay(displayString); delay(80);
Serial.println (totalscore);
x = light1;
y = light2;
z = light3;
zz = light4;
if ((x < 200) && (rep1 < 50)){
for(int score = totalscore ; score < (totalscore+25); score++){
delay (16);
displayString = score;
center_theDisplay(displayString); delay(16);
Serial.println (score);
rep1 = rep1 + 1;
}
totalscore = totalscore + 25;
}
if ((y < 200) && (rep2 < 100)){
for(int score = totalscore ; score < (totalscore+50); score++){
delay (16);
displayString = score;
center_theDisplay(displayString); delay(16);
Serial.println (score);
rep2 = rep2 + 1;
}
totalscore = totalscore + 50;
}
if ((z < 200) && (rep3 < 150)){
for(int score = totalscore ; score < (totalscore+75); score++){
delay (16);
displayString = score;
center_theDisplay(displayString); delay(16);
Serial.println (score);
rep3 = rep3 + 1;
}
totalscore = totalscore + 75;
}
if ((zz < 200) && (rep4 < 200)){
for(int score = totalscore ; score < (totalscore+100); score++){
delay (16);
displayString = score;
center_theDisplay(displayString); delay(16);
Serial.println (score);
rep4 = rep4 + 1;
}
totalscore = totalscore + 100;
}
delay(5);
}
void center_theDisplay(String input_Str) {
byte charCount, total_charWidth, x_position;
input_Str.toCharArray(CharBuf, MaxStringLength + 1); //string to char array
charCount= input_Str.length();
if (charCount==0) exit;
total_charWidth= 0;
for (byte thisChar = 0; thisChar <charCount; thisChar++) {
total_charWidth= total_charWidth + dmd.charWidth(CharBuf[thisChar]) +1; //add 1 pixel for space
}
total_charWidth= total_charWidth -1; //no space for last letter
x_position= (PanelWidth - total_charWidth) /2; //position(x) of first letter
// output the string to the DMD Panel
dmd.drawString( x_position, 1, CharBuf,charCount, GRAPHICS_NORMAL);
if (buttonState == HIGH){
dmd.clearScreen(true);
totalscore = 0;
rep1 = 0;
rep2 = 0;
rep3 = 0;
rep4 = 0;
}
}
Here is the SLAVE Code:
#include <Wire.h>
void setup() {
Wire.begin(8); // join i2c bus with address #8
Wire.onReceive(receiveEvent); // register event
Serial.begin(9600); // start serial for output
pinMode(2, OUTPUT);
pinMode(5, OUTPUT);
pinMode(8, OUTPUT);
pinMode(9, OUTPUT);
}
void loop() {
delay(10);
}
// function that executes whenever data is received from master
// this function is registered as an event, see setup()
void receiveEvent(int howMany) {
int x = Wire.read(); // receive byte as an integer
int y = Wire.read(); // receive byte as an integer
int z = Wire.read(); // receive byte as an integer
int zz = Wire.read(); // receive byte as an integer
digitalWrite(2, HIGH);
digitalWrite(5, HIGH);
digitalWrite(8, HIGH);
digitalWrite(9, HIGH);
if (x < 200){
digitalWrite(2, HIGH);
digitalWrite(5, LOW);
digitalWrite(8, LOW);
digitalWrite(9, LOW);
}
if (y < 200){
digitalWrite(2, LOW);
digitalWrite(5, HIGH);
digitalWrite(8, LOW);
digitalWrite(9, LOW);
}
if (z < 200){
digitalWrite(2, LOW);
digitalWrite(5, LOW);
digitalWrite(8, HIGH);
digitalWrite(9, LOW);
}
if (zz < 200){
digitalWrite(2, LOW);
digitalWrite(5, LOW);
digitalWrite(8, LOW);
digitalWrite(9, HIGH);
}
Serial.print(x); // print the integer
Serial.print(";"); // print the integer
Serial.print(y); // print the integer
Serial.print(";"); // print the integer
Serial.print(z); // print the integer
Serial.print(";"); // print the integer
Serial.print(zz); // print the integer
Serial.println(" "); // print the integer
}
there might be some random code in there left over from testing different things...if you have any suggestions on making things more concise, it would be greatly appreciated as well =)
Please let me know if you have any questions or concerns.
Thanks,
Kirk