Hello everyone i have a problem.
I am building a Clock with Alarm and i want to use 3 of the Max Matrix to show the time, date and scrolling text messages or bigger information.
The problem is, the functions for scrolling messages i have coppied from the MAXMATRIX examples are using the delay() function to simulate a text floating one pixel to the left every amount of milliseconds u have declared. (Functions are at bottom of the code)
It works fine but as soon as i build in a push button to switch between the modes of static time, date and scrolling message, there is this delay of the function which forces you to press the button till the interval of one full scroll is finished. You all know the problem with the delay function...
I tried and errored probably 100 times and i still cant get the function to work with millis(), i dont know what i am doing wrong
and another thing that i cant get to work is a rotating double dot animation for the static time, youll see in the code what crap i wrote there maybe someone has a solution for this problem too, thanks
i hope someone is out there who can help me because i have to finish this project for school in three weeks.
kind regards johnny
#include <Arduino.h>
#include <DS3231.h>
#include <Wire.h>
#include <MaxMatrix.h>
const byte PROGMEM CH[] = { TOO BIG TO PUT IN THIS POST;
I POST IT IN THE FIRST ANSWER TO THIS POST}
int dataPIN = 8; // DIN pin of MAX7219 module
int loadPIN = 9; // CS pin of MAX7219 module
int clockPIN = 10; // CLK pin of MAX7219 module
MaxMatrix m(dataPIN, loadPIN, clockPIN, 3);
byte buffer[100];
DS3231 clock;
RTCDateTime now;
int mode = 0;
unsigned long prevMillis;
const int interval = 75;
int time1, time2, time3, time4, time5, time6, time7, time8;
char actTime[6];
char actDatum[6];
char actDatum2[9];
int val;
void setup() {
Serial.begin(9600);
pinMode(7, INPUT);
pinMode(13, OUTPUT);
clock.begin();
clock.setDateTime(__DATE__, __TIME__);
m.init();
m.setIntensity(15);
}
void loop() {
if(Serial.available()){
val = Serial.read();
}
if(val == 1){
digitalWrite(13, HIGH);
}else{
digitalWrite(13, LOW);
}
now = clock.getDateTime();
if(digitalRead(7)){
m.clear();
delay(300);
if(mode == 0){
mode = 1;
for(int i = 0; i < 8; i++){
m.setDot(23, i, 0);
}
}else if(mode == 1){
mode = 2;
}else if(mode == 2){
mode = 0;
}else if(mode == 3){
mode = 0;
}
}
if(mode == 0){
createTime(false);
printString(actTime);
rotateDots(50);
}else if(mode == 1){
createDate();
printString(actDatum);
}else if(mode == 2){
createExtendedDate();
printStringWithShift(actDatum2, 200);
}
double test = round(now.second/7.5);
if(test < 1 && mode == 0){
for(int i = 0; i < 8; i++){
m.setDot(23, i, 0);
}
}else if(mode == 0){
for(int i = 0; i <= test; i++){
m.setDot(23, i, 1);
}
}
}
void createTime(bool dots) {
actTime[0] = (now.hour / 10) + '0';
actTime[1] = (now.hour % 10) + '0';
if(dots){
actTime[2] = ':';
}else{
actTime[2] = ' ';
}
actTime[3] = (now.minute / 10) + '0';
actTime[4] = (now.minute % 10) + '0';
actTime[5] = 0;
}
void createDate() {
actDatum[0] = (now.day / 10) + '0';
actDatum[1] = (now.day % 10) + '0';
actDatum[2] = '.';
actDatum[3] = (now.month / 10) + '0';
actDatum[4] = (now.month % 10) + '0';
actDatum[5] = 0;
}
void createExtendedDate() {
actDatum2[0] = (now.day / 10) + '0';
actDatum2[1] = (now.day % 10) + '0';
actDatum2[2] = '.';
actDatum2[3] = (now.month / 10) + '0';
actDatum2[4] = (now.month % 10) + '0';
actDatum2[5] = '.';
actDatum2[6] = '2';
actDatum2[7] = '0';
actDatum2[8] = '1';
actDatum2[9] = '9';
actDatum2[10] = ' ';
actDatum2[11] = ' ';
actDatum2[12] = 0;
}
bool milliDelay(int inter, int timeInt){
if(millis() - timeInt >= inter){
return true;
}
}
void blinkDots(int speed){
if(millis()-prevMillis >= 1000){
prevMillis = millis();/*
m.setDot(10, 3, i);
m.setDot(10, 2, i);
m.setDot(11, 2, i);
m.setDot(11, 3, i);
m.setDot(11, 5, i);
m.setDot(11, 6, i);
m.setDot(11, 5, i);
m.setDot(11, 6, i);*/
}
}
void rotateDots(int speed){ // THIS IS THE SECOND PROBLEM I DECLARED IN THE POST
// this is NOT working like this, just the last attempt from me
if (milliDelay(speed, time1)) {
time1 = millis();
m.setDot(10, 3, 1);
}
if (milliDelay(speed, time2)) {
time2 = millis();
m.setDot(10, 2, 1);
}
if (milliDelay(speed, time3)) {
time3 = millis();
m.setDot(11, 2, 1);
}
if (milliDelay(speed, time4)) {
time4 = millis();
m.setDot(11, 3, 1);
}
if (milliDelay(speed, time5)) {
m.setDot(11, 5, 1);
time5 = millis();
}
if (milliDelay(speed, time6)) {
m.setDot(11, 6, 1);
time6 = millis();
}
if (milliDelay(speed, time7)) {
m.setDot(10, 6, 1);
time7 = millis();
}
if (milliDelay(speed, time8)) {
m.setDot(10, 5, 1);
time8 = millis();
}
if (milliDelay(speed, time1)) {
time1 = millis();
m.setDot(10, 3, 1);
}
if (milliDelay(speed, time2)) {
time2 = millis();
m.setDot(10, 2, 1);
}
if (milliDelay(speed, time3)) {
time3 = millis();
m.setDot(11, 2, 1);
}
if (milliDelay(speed, time4)) {
time4 = millis();
m.setDot(11, 3, 1);
}
if (milliDelay(speed, time5)) {
m.setDot(11, 5, 1);
time5 = millis();
}
if (milliDelay(speed, time6)) {
m.setDot(11, 6, 1);
time6 = millis();
}
if (milliDelay(speed, time7)) {
m.setDot(10, 6, 1);
time7 = millis();
}
if (milliDelay(speed, time8)) {
m.setDot(10, 5, 1);
time8 = millis();
}
}
void printCharWithShift(char c, int shift_speed){
if (c < 32) return;
c -= 32;
memcpy_P(buffer, CH + 7*c, 7);
m.writeSprite(32, 0, buffer);
m.setColumn(32 + buffer[0], 0);
for (int i=0; i<buffer[0]+1; i++)
{
delay(shift_speed); // THIS IS WHAT I WANNA GET OUT AND REPLACE WITH MILLIS
m.shiftLeft(false, false);
}
}
/**********************************************************************************************************/
void printStringWithShift(char* s, int shift_speed){
while (*s != 0){
printCharWithShift(*s, shift_speed);
s++;
}
}
/**********************************************************************************************************/
void printString(char* s)
{
int col = 0;
while (*s != 0)
{
if (*s < 32) continue;
char c = *s - 32;
memcpy_P(buffer, CH + 7*c, 7);
m.writeSprite(col, 0, buffer);
m.setColumn(col + buffer[0], 0);
col += buffer[0] + 1;
s++;
}
}