The goal of the project is to display numbers from 0 to 9 using seven segment indicator, 74HC595 and pushing remote control buttons. It is assumed that brightness of the numbers, that are displayed, have to be changed by toggling OE pin to LOW and HIGH. I built the scheme physically. Numbers are displayed correct, but when I press "down" button offTime is changing to 51 (increasing HIGH level pulse length, but it means less brightness cause OE is inverted). After that program gets hung up and Arduino not responds. I assumed further brightness adjustment or starting to display other number in this time. How to fix this problem?
#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 = 200;
boolean changed = false;
int input;
int debounce_delay = 1000;
unsigned long last_press = 0;
boolean flag = false;
boolean result = 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;
//1 - dp, 2 - e, 3 - d, 4 - c
//5 - g, 6 - f, 7 - a, 8 - b
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 print_2()
{
//a,b,g,e,d = 1 f,c,dp = 0
digitalWrite(latch, LOW);
//digitalWrite(OE, LOW);
shiftOut(data, clock, LSBFIRST, 0b01101011);
digitalWrite(latch, HIGH);
}
void print_3()
{
//a,b,g,c,d = 1 f,e,dp = 0
digitalWrite(latch, LOW);
//digitalWrite(OE, LOW);
shiftOut(data, clock, LSBFIRST, 0b00111011);
digitalWrite(latch, HIGH);
}
void print_4()
{
//f,g,b,c = 1 a,e,d,dp = 0
digitalWrite(latch, LOW);
//digitalWrite(OE, LOW);
shiftOut(data, clock, LSBFIRST, 0b00011101);
digitalWrite(latch, HIGH);
}
void print_5()
{
//a,f,g,c,d = 1 e,b,dp = 0
digitalWrite(latch, LOW);
//digitalWrite(OE, LOW);
shiftOut(data, clock, LSBFIRST, 0b00111110);
digitalWrite(latch, HIGH);
}
void print_6()
{
//a,f,g,e,d,c = 1 b,dp = 0
digitalWrite(latch, LOW);
//digitalWrite(OE, LOW);
shiftOut(data, clock, LSBFIRST, 0b01111110);
digitalWrite(latch, HIGH);
}
void print_7()
{
//a,b,c = 1 f,g,e,d,dp = 0
digitalWrite(latch, LOW);
//digitalWrite(OE, LOW);
shiftOut(data, clock, LSBFIRST, 0b00010011);
digitalWrite(latch, HIGH);
}
void print_8()
{
//a,b,c,f,g,e,d = 1 dp = 0
digitalWrite(latch, LOW);
//digitalWrite(OE, LOW);
shiftOut(data, clock, LSBFIRST, 0b01111111);
digitalWrite(latch, HIGH);
}
//1 - dp, 2 - e, 3 - d, 4 - c
//5 - g, 6 - f, 7 - a, 8 - b
void print_9()
{
//a,b,c,f,g,d = 1 dp,e = 0
digitalWrite(latch, LOW);
//digitalWrite(OE, LOW);
shiftOut(data, clock, LSBFIRST, 0b00111111);
digitalWrite(latch, HIGH);
}
void clearDisplay() {
digitalWrite(latch, LOW);
shiftOut(data, clock, LSBFIRST, 0b00000000);
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(reset, OUTPUT);
//digitalWrite(reset, HIGH);
pinMode(OE, OUTPUT);
analogWrite(OE, 0);
clearDisplay();
}
void loop() {
result = IrReceiver.decode();
if (result && flag == false && millis() - last_press > debounce_delay) {
flag = true;
last_press = millis();
input = IrReceiver.decodedIRData.command;
//Serial.print("Received command: ");
//Serial.println(input);
switch (input) {
case button0:
print_0();
break;
case button1:
print_1();
break;
case button2:
print_2();
break;
case button3:
print_3();
break;
case button4:
print_4();
break;
case button5:
print_5();
break;
case button6:
print_6();
break;
case button7:
print_7();
break;
case button8:
print_8();
break;
case button9:
print_9();
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(result == false && flag == true)
{
flag = false;
}
if (changed) {
//Serial.print("Updating OE to: ");
//Serial.println(offTime);
analogWrite(OE, offTime);
changed = false;
}
IrReceiver.resume();
}




