//https://forum.arduino.cc/t/adjusting-brightness-of-1-digit-7-segment-indicator-with-74hc595/1262131
/*
10 SRCLR MR SRCLR (Shift Register Clear) SRCLR/MR (active LOW)
11 SRCLK SHCP 10 SRCLK (Shift Register Clock) SRCLK/SHCP (storage register clock pin, SPI clock)
12 RCLK STCP 9 RCLK (Register Clock / Latch) RCLK/STCP (shift register clock input, latch pin)
13 OE OE 6 OE (Output Enable) OE (output enable, active LOW)
14 SER DS 7 SER (Serial Input) SER/DS (serial data input, SPI data)
*/
#include <IRremote.h>
int RECV_PIN = 12;
int data = 7;
int OE = 6;
int latch = 9;
int clock = 10;
//int reset = 11;
unsigned long offTime = 0;
boolean changed = false;
int input;
unsigned long debounce_delay = 2000;
unsigned long last_press = 0;
boolean flag = false;
/*
const int button0 = 82;
const int button1 = 22;
const int button2 = 25;
const int button3 = 13;
const int button4 = 12;
const int button5 = 24;
const int button6 = 94;
const int button7 = 8;
const int button8 = 28;
const int button9 = 90;
const int down = 21;
const int up = 70;
*/
const int button0 = 104;
const int button1 = 48;
const int down = 2;
const int up = 152;
//1 - dp, 2 - e, 3 - d, 4 - c
//5 - g, 6 - f, 7 - a, 8 - b
void clearDisplay() {
digitalWrite(latch, LOW);
shiftOut(data, clock, LSBFIRST, 0b00000000);
digitalWrite(latch, HIGH);
}
void print_0()
{
//a,b,c,d,e,f = 1 g,dp = 0
digitalWrite(latch, LOW);
//digitalWrite(OE, LOW);
shiftOut(data, clock, LSBFIRST, 0b01110111);
digitalWrite(latch, HIGH);
}
void print_1()
{
//b,c = 1 g,dp,a,e,f,d = 0
digitalWrite(latch, LOW);
//digitalWrite(OE, LOW);
shiftOut(data, clock, LSBFIRST, 0b00010001);
digitalWrite(latch, HIGH);
}
void setup()
{
Serial.begin(9600);
pinMode(RECV_PIN, INPUT);
IrReceiver.begin(RECV_PIN, DISABLE_LED_FEEDBACK);
pinMode(data, OUTPUT);
pinMode(clock, OUTPUT);
pinMode(latch, OUTPUT);
pinMode(OE, OUTPUT);
analogWrite(OE, 0);
clearDisplay();
//pinMode(OE, OUTPUT);
//digitalWrite(OE, LOW);
/*digitalWrite(latch, LOW);
shiftOut(data, clock, LSBFIRST, 0b01110111);
digitalWrite(latch, HIGH);*/
}
void loop() {
if (IrReceiver.decode() && millis() - last_press > debounce_delay) {
input = IrReceiver.decodedIRData.command;
Serial.print("Received command: ");
Serial.println(input);
last_press = millis();
switch (input) {
case button0:
print_0();
break;
case button1:
print_1();
break;
case up:
if (offTime >= 51) {
offTime -= 51;
} else {
offTime = 0;
}
changed = true;
break;
case down:
if (offTime <= 204) {
offTime += 51;
} else {
offTime = 255;
}
changed = true;
break;
}
if (changed) {
Serial.print("Updating OE to: ");
Serial.println(offTime);
analogWrite(OE, offTime);
changed = false;
}
IrReceiver.resume();
}
}
/*
int data = 7;
int OE = 6;
int latch = 9;
int clock = 10;
//int reset = 11;
unsigned long offTime = 0;
//1 - dp, 2 - e, 3 - d, 4 - c
//5 - g, 6 - f, 7 - a, 8 - b
//----------------------------------------------------------------------
void clearDisplay() {
digitalWrite(latch, LOW);
shiftOut(data, clock, LSBFIRST, 0b00000000);
digitalWrite(latch, HIGH);
}
//----------------------------------------------------------------------
void print_0() {
//a,b,c,d,e,f = 1 g,dp = 0
digitalWrite(latch, LOW);
shiftOut(data, clock, LSBFIRST, 0b01110111);
digitalWrite(latch, HIGH);
}
//----------------------------------------------------------------------
void print_1() {
//b,c = 1 g,dp,a,e,f,d = 0
digitalWrite(latch, LOW);
shiftOut(data, clock, LSBFIRST, 0b00010001);
digitalWrite(latch, HIGH);
}
//----------------------------------------------------------------------
void setup() {
Serial.begin(115200);
pinMode(data, OUTPUT);
pinMode(clock, OUTPUT);
pinMode(latch, OUTPUT);
pinMode(OE, OUTPUT);
analogWrite(OE, 0);
clearDisplay();
}
//----------------------------------------------------------------------
void loop() {
print_0();
Serial.print("Updating OE to: ");
Serial.println(offTime);
analogWrite(OE, offTime);
delay(1000);
print_1();
delay(1000);
}
*/