Skip to content

Instantly share code, notes, and snippets.

@skandragon
Last active June 1, 2018 23:19
Show Gist options
  • Save skandragon/4a3a77d4f87fe25c5ba647cbd1c28199 to your computer and use it in GitHub Desktop.
Save skandragon/4a3a77d4f87fe25c5ba647cbd1c28199 to your computer and use it in GitHub Desktop.
Arduino controlled color changing table with a lot of sensors
#include <FastLED.h>
// The number of chips connected in series.
#define NUMBER_OF_SHIFT_CHIPS 7
// Directly modify pins, this only works for specific chips, and uses hard-coded pin numbers
#define USE_PIN_DIRECT
// Display serial data. This is slow, so once working, this should be disabled.
#define DEBUG
// Actually update the LEDs
//#define UPDATE_LEDS
#ifdef DEBUG
#define DPRINT(x) Serial.print(x)
#else
#define DPRINT(x)
#endif
#ifdef USE_PIN_DIRECT
#ifndef __AVR_ATmega2560__
#error "This code will only compile on a Mega 2560 board"
#endif
#endif // USE_PIN_DIRECT
#define clockEnablePin 2 // Connects to Clock Enable (CE) pin on the 74hc165
#define clockPin 3 // Connects to the Clock (CLK) pin on the 74hc165
#define dataPin 5 // Connects to the (SER_OUT) pin on the 74hc165
#define ploadPin 4 // Connects to Parallel (SH/LO) load pin on the 74hc165
#define DATA_PIN 8 // out pin for Data -- check
#define NUM_LEDS 49 // TOTAL number of LEDS <<<<<<<< change
#define COLOR_ORDER GRB // colour order
#define CHIPSET WS2812B // LED chip type
#define BRIGHTNESS 150
#define FRAMES_PER_SECOND 120
//#define OnState 8
//#define OffState 9
CRGB leds[NUM_LEDS];
#define DATA_WIDTH NUMBER_OF_SHIFT_CHIPS * 8
#define PULSE_WIDTH_USEC 5
#define POLL_DELAY_MSEC 1
unsigned char pinValues[NUMBER_OF_SHIFT_CHIPS];
unsigned char oldPinValues[NUMBER_OF_SHIFT_CHIPS];
unsigned char getPinValue(unsigned char *values, int i)
{
auto chip = i / 8;
auto bit = i % 8;
return (values[chip] >> bit) & 0x01;
}
void storePinValue(unsigned char *values, unsigned char chip, unsigned char bitnum, unsigned char value)
{
if (value) {
values[chip] |= (1 << bitnum);
} else {
values[chip] &= ~(1 << bitnum);
}
}
void
read_shift_regs(unsigned char *values)
{
unsigned char bitVal;
digitalWrite(clockEnablePin, LOW);
digitalWrite(ploadPin, LOW);
delayMicroseconds(PULSE_WIDTH_USEC);
digitalWrite(ploadPin, HIGH);
delayMicroseconds(PULSE_WIDTH_USEC);
for (int chip = 0 ; chip < NUMBER_OF_SHIFT_CHIPS ; chip++) {
for (int i = 0; i < 8; i++) {
#ifdef USE_PIN_DIRECT
bitVal = PINE & (1 << 3);
PORTE |= (1 << 5);
PORTE &= ~(1 << 5);
#else
bitVal = digitalRead(dataPin);
digitalWrite(clockPin, HIGH);
digitalWrite(clockPin, LOW);
#endif
storePinValue(values, chip, i, bitVal);
}
}
digitalWrite(clockEnablePin, HIGH);
}
void
update_leds(unsigned char *values)
{
for (int i = 0 ; i < DATA_WIDTH ; i++) {
if (getPinValue(values, i)) {
leds[i] = CRGB::Red; // OFF
} else {
leds[i] = CRGB::Blue; // ON
}
}
FastLED.show();
}
#ifdef DEBUG
void
print_pin_values(unsigned char *values)
{
for (int bitnum = 0 ; bitnum < 8 ; bitnum++) {
for (int chip = 0 ; chip < NUMBER_OF_SHIFT_CHIPS ; chip++) {
auto value = getPinValue(values, chip * 8 + bitnum);
if (value) {
Serial.print("HIGH ");
} else {
Serial.print("LOW ");
}
}
Serial.println();
}
}
#endif
void setup()
{
#ifdef DEBUG
Serial.begin(115200);
#endif
delay(1000);
DPRINT("Arduino booted.\n");
FastLED.addLeds<CHIPSET, DATA_PIN, COLOR_ORDER>(leds, NUM_LEDS);
LEDS.setBrightness(BRIGHTNESS); // Set initial brightness
pinMode(ploadPin, OUTPUT);
pinMode(clockEnablePin, OUTPUT);
pinMode(clockPin, OUTPUT);
pinMode(dataPin, INPUT);
digitalWrite(clockPin, LOW);
digitalWrite(ploadPin, HIGH);
memcpy(oldPinValues, pinValues, sizeof pinValues);
pinValues[0] = 1; // ensure we have a difference to start with the first scan in loop()
oldPinValues[0] = 2;
}
void
loop()
{
read_shift_regs(pinValues);
if (memcmp(pinValues, oldPinValues, sizeof pinValues) != 0) {
DPRINT("*Pin value change detected*\r\n");
#ifdef DEBUG
print_pin_values(pinValues);
#endif
#ifdef UPDATE_LEDS
update_leds(pinValues);
#endif
memcpy(oldPinValues, pinValues, sizeof pinValues);
}
delay(POLL_DELAY_MSEC);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment