hi all, I have a baffling problem on a vast synthesizer I'm working on.
I have a switch matrix set up to scan 4 columns and 8 rows of switches, to populate an array of 4 bytes.
every 100ms I scan the switches and update the values; this initiates in Loop() just fine.
This switchScan code works 100% fine with delay(1); it does not work AT ALL with the (currentTime - lastSwitchScan > switchScanDelay), no matter the value of switchScanDelay...
(and it doesn't work right WITHOUT the delay either, I'm assuming I need some kind of settling time; the switch values seem to overflow into neighboring bytes)
here's the switch scan code with delay REMmed out, this returns all zeroes...
here's the switch scan code with delay(); this works 100% fine...
and here's the parser code which faithfully prints out the full value of the bytes:
Since this is also running the audio library, as well as LED controls for 7 segment displays and switch feedback, and fastLED to animate 32 LEDs, I need to avoid delay() wherever possible.
What am I doing wrong here?
here's the loop(), which works fine:
Sample Serial Monitor output:
without delay:
0: 00000000 1: 00000000 2: 00000000 3: 00000000
0: 00000000 1: 00000000 2: 00000000 3: 00000000
0: 00000000 1: 00000000 2: 00000000 3: 00000000
0: 00000000 1: 00000000 2: 00000000 3: 00000000
0: 00000000 1: 00000000 2: 00000000 3: 00000000
with delay:
0: 11100101 1: 11111111 2: 11011001 3: 11100110
0: 11100101 1: 11111111 2: 11011001 3: 11100110
0: 11100101 1: 11111111 2: 11011001 3: 11100110
0: 11100101 1: 11111111 2: 11011001 3: 11100110
...updated every 100ms per switchScanPeriod being set to 100
any ideas as to how I can sort this out will be greatly appreciated.
I have a switch matrix set up to scan 4 columns and 8 rows of switches, to populate an array of 4 bytes.
every 100ms I scan the switches and update the values; this initiates in Loop() just fine.
This switchScan code works 100% fine with delay(1); it does not work AT ALL with the (currentTime - lastSwitchScan > switchScanDelay), no matter the value of switchScanDelay...
(and it doesn't work right WITHOUT the delay either, I'm assuming I need some kind of settling time; the switch values seem to overflow into neighboring bytes)
here's the switch scan code with delay REMmed out, this returns all zeroes...
Code:
void switchScan()
{
for (byte i = 0; i < 4; i++)
{
digitalWrite(switchCols[i], LOW);
// delay(1);
if (currentTime - lastSwitchScan > switchScanDelay)
{
lastSwitchScan = currentTime;
for (byte j = 0; j < 8; j++)
{
bitWrite(switchSettings[i], j, digitalRead(switchRows[j]));
}
}
digitalWrite(switchCols[i], HIGH);
}
}
here's the switch scan code with delay(); this works 100% fine...
Code:
void switchScan()
{
for (byte i = 0; i < 4; i++)
{
digitalWrite(switchCols[i], LOW);
delay(1);
// if (currentTime - lastSwitchScan > switchScanDelay)
// {
lastSwitchScan = currentTime;
for (byte j = 0; j < 8; j++)
{
bitWrite(switchSettings[i], j, digitalRead(switchRows[j]));
}
// }
digitalWrite(switchCols[i], HIGH);
}
}
and here's the parser code which faithfully prints out the full value of the bytes:
Code:
void parseData()
{
for (byte i = 0; i < 4; i++)
{
Serial.print(" ");
Serial.print(i);
Serial.print(": ");
for (byte j = 0; j < 8; j++)
{
Serial.print(bitRead(switchSettings[i], j), BIN);
}
if (i == 3)
{
Serial.println();
}
}
}
Since this is also running the audio library, as well as LED controls for 7 segment displays and switch feedback, and fastLED to animate 32 LEDs, I need to avoid delay() wherever possible.
What am I doing wrong here?
here's the loop(), which works fine:
Code:
void loop()
{
currentTime = millis();
if (currentTime - lastSwitchScan > switchScanPeriod)
{
lastSwitchScan = currentTime;
switchScan();
parseData();
}
}
Sample Serial Monitor output:
without delay:
0: 00000000 1: 00000000 2: 00000000 3: 00000000
0: 00000000 1: 00000000 2: 00000000 3: 00000000
0: 00000000 1: 00000000 2: 00000000 3: 00000000
0: 00000000 1: 00000000 2: 00000000 3: 00000000
0: 00000000 1: 00000000 2: 00000000 3: 00000000
with delay:
0: 11100101 1: 11111111 2: 11011001 3: 11100110
0: 11100101 1: 11111111 2: 11011001 3: 11100110
0: 11100101 1: 11111111 2: 11011001 3: 11100110
0: 11100101 1: 11111111 2: 11011001 3: 11100110
...updated every 100ms per switchScanPeriod being set to 100
any ideas as to how I can sort this out will be greatly appreciated.