Go Down

Topic: Multiplexing LED only works with serial.print() but not with delay() (Read 426 times) previous topic - next topic

chuckyx

I want to light up different LEDs via multiplexing. The following code works as expected:

Code: [Select]
...
for (int i = 0; i < Day; i++) {
    Serial.println("");
    //delay(20);
    rst();
    digitalWrite(GNDrows[row[i]], LOW);
    digitalWrite(VCCcols[col[i]], HIGH);
    delay(2000);
  }
}

  void rst() {
    for (int i = 0; i <= 3; i++) {
      digitalWrite(GNDrows[i], HIGH);
    }
    for (int i = 0; i <= 5; i++) {
      digitalWrite(VCCcols[i], LOW);
    }
  }


this does not:
Code: [Select]
...
for (int i = 0; i < Day; i++) {
    //Serial.println("");
    delay(20);
    rst();
    digitalWrite(GNDrows[row[i]], LOW);
    digitalWrite(VCCcols[col[i]], HIGH);
    delay(2000);
  }
}

  void rst() {
    for (int i = 0; i <= 3; i++) {
      digitalWrite(GNDrows[i], HIGH);
    }
    for (int i = 0; i <= 5; i++) {
      digitalWrite(VCCcols[i], LOW);
    }
  }


I understand that Serial.println(""); will take some time to execute, however replacing it with a delay doesn't work. From my understanding the following should happen:

for (int i = 0; i < Day; i++) {
 //Serial.println("");
 delay(20);
 rst(); Turn all LEDs off
 digitalWrite(GNDrows[row], LOW);set LED i GND to 0V
 digitalWrite(VCCcols[col], HIGH); set LED i VCC to 5V
 delay(2000); keep the state for 2s, hence LED should stay on for 2s
}

I neither understand why I would need a delay before resetting nor why a manual delay won't work but Serial.print does. Baud Rate is set to 9600.

Hopefully someone can point out what inherent logic I'm missing.

best regards
still learning myself; feel free to correct me on my postings :)

pert

Please describe what happens in the non-working code.

Please post your full sketch. Reading or writing past the bounds of an array can cause strange behavior but we can't see whether that's happening without looking at the rest of the code.

chuckyx

#2
Dec 09, 2018, 11:30 am Last Edit: Dec 09, 2018, 02:37 pm by chuckyx
Oh I Must have been sleepy already, with not working I mean that the LEDs wont light up at all.

Here's the complete code:

Code: [Select]
// Date and time functions using a DS3231 RTC connected via I2C and Wire lib
#include <Wire.h>
#include "RTClib.h"
RTC_DS3231 rtc;
int GNDrows[] = {8, 9 , 10 , 11};
int VCCcols[] = {2, 3, 4, 5, 6, 7};
int row[24];
int col[24];
int DayArray[4][6] = {

 {24,  20, 25, 23 , 19, 22},

 {15, 14, 16, 18, 13, 17},

 {10, 9, 8, 7, 12, 11},

 {6, 5, 4, 3, 2, 1},

};
int OldDay = 0;
void setup () {

Serial.begin(9600);

 if (! rtc.begin()) {
   //Serial.println("Couldn't find RTC");
   while (1);
 }

 for (int i = 1; i <= 4; i++) {
   pinMode(GNDrows[i], OUTPUT);
   digitalWrite(GNDrows[i], HIGH);
 }

 for (int i = 1; i <= 6; i++) {
   pinMode(VCCcols[i], OUTPUT);
   digitalWrite(VCCcols[i], LOW);
 }
}

void loop () {
 DateTime now = rtc.now();
 int Day = now.day();
 if (Day != OldDay) {
   OldDay = Day;
   //Serial.println("we do the if");
   int q = 0;
   int DayVal[Day];
   for (int i = 0; i < Day; i++) {
     DayVal[i] = i+1;
     //Serial.println("DayVal");
     //Serial.println(DayVal[i]);
   }

   while (q < Day) {
     for (int i = 0; i <= 3; i++) {
       for (int j = 0; j <= 5; j++) {
         if (DayVal[q] == DayArray[i][j]) {
           row[q] = i;
           col[q] = j;
         }
       }
     }
     q++;
   }
 }


 for (int i = 0; i < Day; i++) {
   //Serial.println("");
   //delay(2);
   rst();
   digitalWrite(GNDrows[row[i]], LOW);
   digitalWrite(VCCcols[col[i]], HIGH);
   delay(2000);
 }
}

 void rst() {
   for (int i = 0; i <= 3; i++) {
     digitalWrite(GNDrows[i], HIGH);
   }
   for (int i = 0; i <= 5; i++) {
     digitalWrite(VCCcols[i], LOW);
   }
 }


I'm using an Arduino Uno for this.
still learning myself; feel free to correct me on my postings :)

Grumpy_Mike

Code: [Select]
if (! rtc.begin()) {
   //Serial.println("Couldn't find RTC");
   while (1);

Well that will stop your code from going any further and you have no indication of the fault.
Un comment out the print statement and see what happens on the serial monitor.

Pleas supply a schematic, not a Fritzing layout diagram of your circuit.

chuckyx

@grumpy_mike

I uncommented the line you suggested and saw nothing on the serial monitor. I then added

Code: [Select]
void loop () {
 Serial.println("alive");
 DateTime now = rtc.now();


and got

Code: [Select]
alive
alive
alive


and so forth.

With the portion of my code, marked as working in my first post the result is:

Embedded gif doesn't seem to work, here's a working link. Basically you see LEDs stay on for 2s and then another LED goes on.

With the other code (no serial.print but delay) all LEDs stay off.

schematic is attached, I didn't find a symbol for the RTC module so assume it's wired correclty. Today is the 9th and 9 LEDs light up. I'll wait 3 minutes and verify that indeed now 10 LEDs light up as it's the 10th. -> 10 LEDs are lit.
I used an arduino uno but only had the symbol for an atmega328.

still learning myself; feel free to correct me on my postings :)

chuckyx

Okay, so it seems changing

Code: [Select]
if (! rtc.begin()) {
  //Serial.println("Couldn't find RTC");
  while (1);


to

Code: [Select]
rtc.begin()

and removing Serial.begin as well as all Serial.print lines resolved the issue. I still can't really wrap my head around it though. I'm not using Pin 0 or 1.
still learning myself; feel free to correct me on my postings :)

Rintin

Code: [Select]

void setup () {

 for (int i = 1; i <= 4; i++) {
   pinMode(GNDrows[i], OUTPUT);
   digitalWrite(GNDrows[i], HIGH);
 }


}


 void rst() {
   for (int i = 0; i <= 3; i++) {
     digitalWrite(GNDrows[i], HIGH);
   }

 }

Shouldn't it i=0;i<=3 in both loops?
Also similar issue to VCCcols.

Rintin

int DayArray[4][6]  -> did you miss the 21 and add an extra 25?

Not sure what it will do on the 21. and I expect these to overflow a few days later:
int row[24];
int col[24];

chuckyx

Shouldn't it i=0;i<=3 in both loops?
Also similar issue to VCCcols.
You're right. I already corrected that in the rst() function but not there.

int DayArray[4][6]  -> did you miss the 21 and add an extra 25?
Not sure what it will do on the 21. and I expect these to overflow a few days later:
int row[24];
int col[24];
Oh right, I still need to handle everything above 24. 25 should be 21. It should act as an advent calendar, basically I just need to make sure for everything > 24 that 24 will remain the limit value for day.
still learning myself; feel free to correct me on my postings :)

Go Up