I am testing a code to read digital pin PB3 and turn on the relay on the ATtiny10. At the hardware level, I observe 0V and 5V when the switch is closed and open, respectively. However, I am unable to get the LED to blink when using the digital input. The LED blinks correctly when the digital input is not involved. Can you provide guidance on how to debug this issue?
I am using arduino version 1.8.9
Even i tried digtialRead
kindly note i want use PB3 only as digtial input. It has special function associated with it.
// Digital pin definitions
#define DIP_PIN 3 // Example digital pin (PB3)
uint8_t dip4State = (PINB & (1 << DIP_PIN)) ? 1 : 0;
then try to readstatus of the pin
sample code .
#include <avr/io.h>
#include <util/delay.h>
#define F_CPU 1000000UL // Define CPU frequency as 1MHz
void InitIO() {
DDRB |= (1 << PB2); // Set PB2 as output (Relay)
DDRB &= ~(1 << PB3); // Set PB3 as input (Digital input)
PORTB |= (1 << PB3); // Enable pull-up resistor on PB3
}
void RelayOn() {
PORTB |= (1 << PB2); // Set PB2 high (Relay on)
}
void RelayOff() {
PORTB &= ~(1 << PB2); // Set PB2 low (Relay off)
}
void RelayToggle() {
PORTB ^= (1 << PB2); // Toggle PB2 (Relay on/off)
}
void DelayTime(uint32_t time_ms) {
while (time_ms >= 1000) {
_delay_ms(1000); // Delay for 1000 milliseconds
time_ms -= 1000;
}
while (time_ms > 0) {
_delay_ms(1); // Delay for remaining milliseconds
time_ms--;
}
}
int main() {
InitIO();
DelayTime(100);
uint8_t buttonStatus = bit_is_clear(PINB, PB3); // Store status in variable
DelayTime(100);
while (1) {
if (buttonStatus) { // Check if PB3 is low (button pressed)
RelayToggle(); // Toggle relay state if button is pressed
DelayTime(100); // Wait for 100 milliseconds to debounce
} else {
RelayOff(); // Ensure relay is off if button is not pressed
}
_delay_ms(10); // Small delay to debounce input
}
return 0;
}