Problem is that; when I wanna detect a button press in my loop() it never gets detected. However if I get rid of the OLED_Init() method in Setup() the button press gets detected!
So the error is not in the code you posted.
Does the OLED_init(); code ever return?
Otherwise loop() will never be called, so your code never reaches the buttonpress part
Eberhard
I know void loop() gets called because my code within void loop(), which draws a line on the LCD, works!!! But not the button press detection! That was the whole problem to start with.
Why is it that the button press never gets detected?
Please do what other posters suggested; post your exact sketch copy-and-pasted. Don't type it in!
By only posting hand-copied material from where you think that the problem is, you are forcing us to essentially look at your conclusions and not at the source material. It is unlikely that we will find the issue by looking at your conclusions since you cannot find it.
Worse, you are wasting all these people's time which is why people have stopped trying to help you.
But here's a wild guess: press the button and HOLD IT DOWN for many seconds and tell us if that works. AND COPY_PASTE YOUR SKETCH!!!
int OLED_RESETPIN = 53;
int OLED_INITDELAYMS = 3000;
const int buttonPin = 2; // the pin that the pushbutton is attached to
const int ledPin = 12; // the pin that the LED is attached to
int buttonState = 0; // current state of the button
int lastButtonState = 0; // previous state of the button
int ledState = 0; // remember current led state
void setup() {
Serial.begin(19200);
pinMode(buttonPin, INPUT); // initialize the button pin as a input
pinMode(ledPin, OUTPUT);
pinMode(OLED_RESETPIN, OUTPUT); // sets the digital pin as output
//Using pin53 to control reset of the OLED
OLED_Init();
//clear screen
OLED_Clear();
}
void loop()
{
// read the pushbutton input pin
buttonState = digitalRead(buttonPin);
You have the onboard led on pin 13. Use it to verify that your loop() function is called by blinking it, the same way i did it with the led on pin 12.
put this into your loop() function
Yeah, Eberhard is smart to take a divide and conquer approach.
We cannot guarantee that the code is getting past the while loop that is waiting to recv data. His comments will prove or disprove that and therefore point us to a problem in the OLED code or a problem with the button.
I'm going to jump ahead tho and ask you to describe how the button is connected... did you connect pin 2 to one side of the button and the other side to ground? (That will not work erratically...)
When I do the above in loop(); it works if I get rid of the "OLED_Init();" but doesn't if "OLED_Init();" stays there.
The button is conneted the correct way. Reason why I am 100% sure is that when I so a very simple button push program it works perfectly. When I mix it with OLED code it doesn't work! This is what the problem has been from the begining.
And this is what I sugessted from the beginning:
OLED_init() does never returns therefore setup() never returns and in the end loop() is never called, so the button is never checked.
My last suggestion to this topic is to remove the call to
OLED_GetResponse(); in setup() and see what happens.
"The button is conneted the correct way. Reason why I am 100% sure is that when I so a very simple button push program it works perfectly"
I'd recommend that you learn how to present data not your opinion. That you are 100% sure is not relevant. You are asking for help. If you can't be bothered to take the time to state what you think the correct way is, why should we be spending all this time thinking abt your proglem. In this case, if the pin is "floating" proximity to a driven pin could affect the float level. Inclusion of the serial drives pins and could influence proximal pins. This is why I asked the question.
Regardless, there's no point chasing this unlikely case when in fact this bug is pretty obviously what Eberhard 1st suggested. I just got fooled by your obstinacy. Loop() is never being run.