Hi!
I picked up Arduino again after some years, now i hooked up a bunch of things as you can see to see if i can use them again?
Now i have a problem, every time i press the button, the green LED and the state of the button starts going crazy. The serial monitor confirms that the button state changes every X interval what i set before. For eg. when i set 100ms interval, the serial monitor be like:
Button was pressed, state is: 0
LED has been changed, state is: 0
Current Millis: 579000
Button was pressed, state is: 1
LED has been changed, state is: 1
Current Millis: 579100
Button was pressed, state is: 0
LED has been changed, state is: 0
Current Millis: 579200
But this continues for like 15-30 seconds even if i completely unplug everything from the UNO in the meantime.
I am using the new UNO R4 WiFi, maybe this is just another bug?
Source code:
#include "Arduino_LED_Matrix.h"
#include "animation.h"
ArduinoLEDMatrix matrix;
const int buttonPin = 7;
const int potmeterPin = A1;
const int lightSensorPin = A0;
const int rgbRPin = 3; //PWM
const int rgbGPin = 5; //PWM
const int rgbBPin = 6; //PWM
const int irSensorPin = 4;
const int greenLedPin = 2;
const int buzzerPin = 8;
const int greenLedInterval = 500;
const int buttonInterval = 100;
unsigned long currentMillis = 0;
unsigned long previousGreenLedMillis = 0;
unsigned long previousButtonMillis = 0;
byte greenLedState = LOW;
byte buttonState = LOW;
void setup() {
Serial.begin(9600);
pinMode(buttonPin, INPUT);
pinMode(potmeterPin, INPUT);
pinMode(lightSensorPin, INPUT);
pinMode(rgbRPin, OUTPUT);
pinMode(rgbGPin, OUTPUT);
pinMode(rgbBPin, OUTPUT);
pinMode(irSensorPin, INPUT);
pinMode(greenLedPin, OUTPUT);
pinMode(buzzerPin, OUTPUT);
matrix.loadSequence(dinosaur);
matrix.begin();
matrix.play(true);
}
void loop() {
currentMillis = millis();
//greenLedBlinking();
buttonRead();
switchLED();
}
void greenLedBlinking() {
if (greenLedState == LOW) {
if(currentMillis - previousGreenLedMillis >= greenLedInterval) {
greenLedState = HIGH;
previousGreenLedMillis += greenLedInterval;
}
} else {
if(currentMillis - previousGreenLedMillis >= greenLedInterval) {
greenLedState = LOW;
previousGreenLedMillis += greenLedInterval;
}
}
}
void switchLED() {
digitalWrite(greenLedPin, greenLedState);
}
void buttonRead() {
if (currentMillis - previousButtonMillis >= buttonInterval) {
if(digitalRead(buttonPin) == HIGH) {
buttonState = !buttonState;
Serial.print("Button was pressed, state is: ");
Serial.println(buttonState);
greenLedState = !greenLedState;
Serial.print("LED has been changed, state is: ");
Serial.println(greenLedState);
Serial.print("Current Millis: ");
Serial.println(currentMillis);
Serial.println();
}
previousButtonMillis += buttonInterval;
}
}
Also i tried using hardvare debounce with 10k resistor and a 0.1uF capacitor, no luck. I tried only touching the 5V rail with the wire for the corresponding pin, no luck. I tried switching out the button but this is irrelevant because it still freaks out even when i only touch it to the 5V rail manually.
I also have no electric shorts.