Hello folks!
I have desiged a custom PCB where an Attiny10 controls 2 LED with a single button. It works great, thanks to the help of some fantastic forum members that had a crack at my novice code!
The Circuit and Code is designed for a button that pulls down to ground. On the breadboard the momentary button successfully toggles through the different LED on/off options with long presses and/or double- and triple-clicks. I used the internal pull-up resistor to get the input pin to HIGH when the button is not connected to prevent a floating pin.
It was planned to wire the PCB with the LEDs attached into an old Tamagotchi (Yes, those little eggs still exist and i might have a collection problem ) to serve as a multicolor LCD-Display backlight, so you can play with the digital egg in the evening or low-light conditions.
I wanted to use one of the more seldom used buttons on the Tamagotchi to double as a switch which is detected by the attiny10, thus controlling the LED Backlight.
But when i wired it in, it did not work as intended: Both LED's came on upon inserting the battery for example. Through probing around, i found out that one side of the Tamagotchis Button is connected to VCC/3.3V. The other side of the button does not connect directly to ground as far as i can probe.
So i assume the Tamagotchis Button and the IC listening to it is wired to be HIGH when pressed and LOW when not, the opposite of my inital design. My Code would need to reflect this.
I revised my code and testing breadboard setup to work with the button connecting the input pin to VCC/3.3V when pressed. Of course i added a pull-down resistor to get the floating input pin to LOW when the button is not pressed.
I (most likely wrongly) wired it and it does not work, still kinda behaving like the input is floating; randomly turning on/off the LED, not really registering the button presses and so on.
I hope some of you more talented folks can spot an error in my approach!
Here is my code:
/*
Test setup:
Button Side 1 --> 4.7k Resistor --> PB1 --> Ground
Button Side 2 --> VCC (3,3V or 5V)
LED (+) Color 1 --> 1K resistor --> Pin 1 (PB0)
LED (+) Color 2 --> 1K resistor --> Pin 1 (PB2)
LED (-) --> (GND)
*/
#define F_CPU 1000000UL //defines CPU frequency, here 1MHZ
#define LED_toggle_0 PINB |= (1 << PINB0) // toggle PB0 (datasheet: 11.2.2)
#define LED_toggle_1 PINB |= (1 << PINB2); //toggle PB2
#include <avr/io.h>
#include <avr/interrupt.h>
#include <util/delay.h>
void setup() {
DDRB |= (1 << PB2) | (1 << PB0); // PB2 and PB0 as output
DDRB &= ~(1 << PB1); // PB1 as input
//PUEB |= (1 << PUEB1); // activate internal pull-up -> disabled for external pull-down
TCCR0B |= (1 << CS02) | (1 << CS00) // prescaler clk/1024 and turns on timer0
| (1 << ICNC0); // input capture noise canceler
}
void loop() {
bool toggledB1 = 0; // flag to avoid more than one toggle
bool toggledB0 = 0; // flag to avoid more than one toggle
bool armclick = 0;
static uint8_t multiclick = 0;
while ((PINB & (1 << PINB1))) { // stay in while as long as PINB1 remains HIGH -> Button pressed
if (TCNT0 - ICR0 > 25 && !armclick) { // 25ms have passed
multiclick++;
armclick = 1; // to avoid having more multiclick increments after one click
}
if ((TCNT0 - ICR0 > 1000) && (!toggledB1) && (!toggledB0)) { // one second has passed
PINB |= (1 << PINB2); // toggle PB2
PINB |= (1 << PINB0); // toggle PB0
toggledB0 = 1; // so the led will not toggle again
toggledB1 = 1; // so the led will not toggle again
}
}
if (TCNT0 - ICR0 > 500) {
switch (multiclick){
case 1: // it was a single long click
multiclick = 0;
break;
case 2: // doubleclick
PINB |= (1 << PINB0); // toggle PB0
multiclick = 0;
break;
case 3: // tripleclick
PINB |= (1 << PINB2); // toggle PB2
multiclick = 0;
break;
default:
multiclick = 0; // undefined # of clicks
break;
}
}
}
Here is a picture of my breadboard setup controlling two LEDs of a RGB one with an attiny10:
And here my quick doodle of the schematics as far is i understood:
I appreaciate you all reading through this wall of text and thank you for your help in advance!
All the Best and have a nice day/night/whatever
2 posts - 2 participants