I'm programming ATTiny13's to use on a custom PCB and have noticed an odd behaviour at times.
My ATTiny is connected to an Arduino Nano as per the diagram shown here, so pretty standard. I then use the following code on it:
#include <Bounce2.h>
#define BUTTON_PIN PB4
#define led1Pin PB0
#define led2Pin PB1
#define led3Pin PB2
#define led4Pin PB3
#define switchPin PB4
#define delayInterval 100
Bounce debouncer = Bounce(); // Instantiate a Bounce object
uint8_t val; // variable for reading the pin status
uint8_t val2; // variable for reading the delayed status
uint8_t buttonState; // variable to hold the mode switch state
int lightMode = 2;
void setup() {
PORTB |= (1 << PB4); // set PB4 as input & activate internal pull-up resistor for PB4
pinMode(led1Pin, OUTPUT);
pinMode(led2Pin, OUTPUT);
pinMode(led3Pin, OUTPUT);
pinMode(led4Pin, OUTPUT);
}
void loop() {
val = digitalRead(switchPin); // read input value and store it in val
delay(10);
if (val != buttonState) {
if (val == HIGH) { // check if the mode button is pressed
if (lightMode == 0) {
lightMode = 1; // turn LEDs on
}
else {
if (lightMode == 1) { // if LEDs on
lightMode = 2; // make it flash
}
else {
if (lightMode == 2) { // if LEDs flashing
lightMode = 3; // make it wave
}
else {
if (lightMode == 3) { // if LEDs waving,
lightMode = 0; // turn LEDs off
}
}
}
}
}
buttonState = val; // save the new state in our variable
}
// Now do whatever the lightMode indicates
if (lightMode == 0) { // LEDs-off
PORTB |= (1 << PB1); // HIGH
digitalWrite(led2Pin, LOW);
digitalWrite(led3Pin, LOW);
digitalWrite(led4Pin, LOW);
}
if (lightMode == 1) { // LEDs-on
digitalWrite(led1Pin, HIGH);
digitalWrite(led2Pin, HIGH);
digitalWrite(led3Pin, HIGH);
digitalWrite(led4Pin, HIGH);
}
if (lightMode == 2) { // LEDS-flashing
digitalWrite(led1Pin, HIGH);
digitalWrite(led2Pin, HIGH);
digitalWrite(led3Pin, HIGH);
digitalWrite(led4Pin, HIGH);
delay(100);
digitalWrite(led1Pin, LOW);
digitalWrite(led2Pin, LOW);
digitalWrite(led3Pin, LOW);
digitalWrite(led4Pin, LOW);
delay(100);
}
if (lightMode == 3) { // LEDs-waving
digitalWrite(led4Pin, LOW);
digitalWrite(led1Pin, HIGH);
delay(100);
digitalWrite(led1Pin, LOW);
digitalWrite(led2Pin, HIGH);
delay(100);
digitalWrite(led2Pin, LOW);
digitalWrite(led3Pin, HIGH);
delay(100);
digitalWrite(led3Pin, LOW);
digitalWrite(led4Pin, HIGH);
delay(100);
digitalWrite(led4Pin, LOW);
}
}
What I have noticed is that some times I program them, and the button click just doesn't work. Nothing wrong with it, but the behaviour is not the same.
These are programmed to start with lightmode
2, so it cycles through every LED. I press the button once, it turns off the LEDs. I press it again, it turns them all on. I press again and nothing happens.
The odd thing is that this happens on only a few IC's but not all of them. These all come from the same batch and are using the same programming methodology.
I am using the MicroCore library to program them.
Would appreciate any advice.
Thank you!