Skip to content

Instantly share code, notes, and snippets.

@davedarko
Created October 24, 2018 23:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save davedarko/5b69b6ddfbb7080bd03b3bd9651c1aa4 to your computer and use it in GitHub Desktop.
Save davedarko/5b69b6ddfbb7080bd03b3bd9651c1aa4 to your computer and use it in GitHub Desktop.
/*
Based on https://github.com/maltesemanTS1/Charlieplexing-the-Arduino
http://arduino.cc/en/Tutorial/BitMask
*/
int16_t shifter = 0;
uint8_t shifter_factor = 8; // also speed influence
bool toggle = true;
const uint8_t max_leds = 20;
uint8_t pwm_value;
uint8_t pwm_values[20] = { 0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0, 23,15,8,1,1 };
void setup()
{
}
void loop() {
buffer_write();
}
void buffer_write()
{
for (uint8_t p = 0; p < 24; p++)
{
for (int8_t i = 0; i < max_leds; i++)
{
int16_t led_index = i + shifter/shifter_factor;
if (led_index > max_leds || led_index < 0)
{
pwm_value = 0;
}
else
{
pwm_value = pwm_values[led_index];
}
if (p < pwm_value)
{
if (toggle) charlie(i);
else charlie(max_leds - i -1); // -1 to get to zero
}
else
{
charlie(-1);
}
}
}
shifter++;
if (shifter == shifter_factor * max_leds)
{
toggle = !toggle;
shifter = -(shifter_factor * 5);
if (toggle) delay(2000);
}
}
void charlie_combo(uint8_t a, uint8_t b)
{
// output
DDRB |= (1 << a);
// output
DDRB |= (1 << b);
// high
PORTB |= (1<<a);
// low
PORTB &= ~(1<<b);
}
void charlie (uint8_t var)
{
// DDRB |= (0x1F);
DDRB &= ~(0x1F);
PORTB &= ~(0x1F);
switch (var) {
case 0: charlie_combo(0, 1); break;
case 1: charlie_combo(1, 0); break;
case 2: charlie_combo(0, 2); break;
case 3: charlie_combo(2, 0); break;
case 4: charlie_combo(0, 3); break;
case 5: charlie_combo(3, 0); break;
case 6: charlie_combo(0, 4); break;
case 7: charlie_combo(4, 0); break;
case 8: charlie_combo(1, 2); break;
case 9: charlie_combo(2, 1); break;
case 10: charlie_combo(1, 3); break;
case 11: charlie_combo(3, 1); break;
case 12: charlie_combo(1, 4); break;
case 13: charlie_combo(4, 1); break;
case 14: charlie_combo(2, 3); break;
case 15: charlie_combo(3, 2); break;
case 16: charlie_combo(2, 4); break;
case 17: charlie_combo(4, 2); break;
case 18: charlie_combo(3, 4); break;
case 19: charlie_combo(4, 3); break;
default: ;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment