I'm building a ATtiny85-based control and reset-circuit for my Raspberry Pi and the attached HD44780-LCD. It's meant to turn the LCD on or off and trigger a reset to wake the Pi up from shutdown, but the reset should only be possible if the LCD is turned off. After resetting the Raspberry Pi the LCD should turn on by itself. Should be simple, but that's where I'm stuck. I tried a lot of stuff, but I just cant figure out what I'm doing wrong here. The sketch is working, but how do I get the LCD to turn on after triggering the reset?
Here's my sketch:
int push = 3; // LCD on/off button
int val; // variable for reading pin 3s status
int val2; // variable for reading pin 3s status
int buttonState;
int LCDMode = 0; // LCD on/off
int gndSwPin = 2; // transistor for LCD grounds
int ledPin = 0; // Blinkenlight
int rstBtn = 4; // button for rPi reset circuit
int rstOut = 1; // transistor for rPi reset circuit
int value, value2 ; // fade for Pin 0 LED
long time=0; // fade for Pin 0 LED
int periode = 2000; // fade for Pin 0 LED
void setup()
{
pinMode(rstOut, OUTPUT); // reset transistor
pinMode(rstBtn,INPUT); // pin 4 button
digitalWrite(rstBtn,HIGH); // internal pullup
pinMode(push,INPUT); // LCD on/off button
pinMode(gndSwPin, INPUT); // transistor for LCD grounds
pinMode(ledPin, OUTPUT); // Blinkenlight
digitalWrite(push,HIGH); // internal pullup
}
void loop()
{
int mode; // LCD on or off
mode = chkbtn(); // see below
if (mode == 0) { //LCD off
int rstState = digitalRead(rstBtn); //reset pulse
if(rstState == LOW){ //reset pulse
digitalWrite(rstOut,HIGH); //reset pulse
delay(40); //reset pulse
}
else{
digitalWrite(rstOut,LOW); // don't reset
digitalWrite(gndSwPin, LOW); // LCD off
digitalWrite(ledPin, LOW); // LED off
}
}
if (mode == 1) {
digitalWrite(gndSwPin, HIGH); //LCD on
int ledVal; //LED fade
ledVal=fade(); //LED fade
analogWrite(ledPin,ledVal); //LED fade
}
}
int chkbtn(){ // state for LCD
val = digitalRead(push); // read input value and store it in val
delay(10); // 10 milliseconds is a good amount of time
val2 = digitalRead(push); // read the input again to check for bounces
if (val == val2) { // make sure we got 2 consistant readings!
if (val != buttonState) { // the button state has changed!
if (val == LOW) { // check if the button is pressed
if (LCDMode == 0) { // if its off
LCDMode = 1; // turn LCD on
}
else {
if (LCDMode == 1) { // if it's on
LCDMode = 0; // turn LCD off
}
}
}
}
}
buttonState = val; // save the new state in variable
return LCDMode;
}
int fade(){ // LED fading stuff
unsigned long currentMillis = millis();
time = millis();
value = 128+127*cos(2*PI/periode*time);
value2 = 128+127*cos(2*PI/periode*(0-time));
analogWrite(ledPin, value);
return value;
}