LED control using ATTINY13A

Go To Last Post
8 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0


Hello everyone

I am trying to implement a code that is associated with a project

It is basically a LED flashlight, that has different modes

This is how it works

Single press- Turns on the first LED

Single press- Turns off the first LED

There are different modes in the first led. The modes are 50% brightness and flasher mode. Those both are achieved when LED 1 is on and we press and hold the button for 2 seconds

When both LEDs are off, pressing and holding the button for 3seconds turns on the second LED and when we press the button, that second LED is turned off.

I need suggestions on how I can implement this project. What is the approach? Should I start with timers/counters, or anything else?

Here is my code so far (It is for first led and its modes)

#define F_CPU 9800000UL // telling controller crystal frequency (1 MHz AVR ATMega328P)
#include <avr/io.h> // header to enable data flow control over pins. Defines pins, ports, etc.
#include  <util/delay.h> // header to enable delay function in program
void init_ports_mcu()
{   DDRB=0xFF; // Set PIN 0,1,2 of the PORTB as output.
	DDRB &= ~(1<<PB4); // Makes first pin of PORTB as Input
	PORTB = 0XF0; // Set all pins of the PORTB as LOW AND KEEP PULL-UP RESISTOR,
	// also the internal Pull Up resistor of first pin PORTB is enable.

}
unsigned char longpress()
{ //the button is pressed when BUTTON1 bit is clear */
if (!(PINB & (1<<PB4)))
	{_delay_ms(400);
		if (!(PINB & (1<<PB4)))
		return 1;
	}
	return 0;
}
unsigned char press()
{ //the button is pressed when BUTTON1 bit is clear */
if (!(PINB & (1<<PB4)))
{_delay_ms(5);
if (!(PINB & (1<<PB4)))
return 1;
}
return 0;
}
int main (void)
{   unsigned char n_led = 1; // initially  LED number is on now<br>
	init_ports_mcu();
	while (1)
	{if (press()) // If the button is pressed, toggle the LED's state
		{
			switch(n_led)
			{
				case 1:
				PORTB |= (1<<PB0);
				if (longpress())
				{
					PORTB &=~(1<<PB0);
					PORTB |= (1<<PB1);
					PORTB &=~(1<<PB2);
				}
				if (longpress())
				{
					PORTB &=~(1<<PB1);
					do {PORTB |= (1<<PB0);
						_delay_ms(20);
						PORTB &= ~(1<<PB0);
						_delay_ms(30);
					} while((PINB & (1<<PB4)));
				}
				break;
				case 2:
				PORTB &=~(1<<PB0);
				PORTB &=~(1<<PB1);
				PORTB &=~(1<<PB2);
				n_led=0;
				break;

			}
			n_led++; // next LED is turn on
			_delay_ms(LOCK_INPUT_TIME);

		}
	}
	return (0);
}

My LEDs are connected to PB0, and PB2. The push button is connected to PB4. 

For 50% brightness, I am using PB1. There is a resistor connected on PB1 to lower the current for 50% brightness

 

 

 

Attachment(s): 

Last Edited: Sun. Dec 12, 2021 - 12:05 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Start with Battery Voltage. Do you know Min/Max values?

Welcome to AVRFreaks!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I have a few comments for the circuit.

Blue LEDs typically conducts at appr. 4 volts, and voltage across the LED is near constant when current increases.

I suppose you supply 5V to the circuit, at lower supply none of the LEDs will turn on.

D3 shortcuts PB2 and both the Tiny and D3 is likely to blow because nothing limits the current. You must have a current limiting resistor in series with any LED!

D2 will likewise blow D1 and the Tiny too. What is the purpose of D1 anyway?

R1 is useless because of D1.

R1, D1 and D2 is not needed at all. Add a resistor in series with D3 to limit current to acceptable value (20mA?) and dim it with PWM when you choose. Much simpler and easier to control.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

For 50% brightness, I am using PB1. There is a resistor connected on PB1 to lower the current for 50% brightness

That is rather poor way to do so, though it may giv e you some sense of bright vs dim...but don't your leds require resistors to avoid burnout (99% of leds DO require a resistor).

With more experience, you should look at using PWM  for the brightness control 

 

Don't forget your power supply cap right at the AVR.

5 ms is way too fast for any debounce...use at least 20ms, and 50 is better

 

 

 

 

When in the dark remember-the future looks brighter than ever.   I look forward to being able to predict the future!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hamza1997 wrote:
Single press- Turns on the first LED

Single press- Turns off the first LED

There are different modes in the first led. The modes are 50% brightness and flasher mode. Those both are achieved when LED 1 is on and we press and hold the button for 2 seconds

When both LEDs are off, pressing and holding the button for 3seconds turns on the second LED and when we press the button, that second LED is turned off.

I need suggestions on how I can implement this project. What is the approach?

Sounds like a classic case for a State Machine - aka "Finite State Machine" (FSM) or "Finite State Automaton"

 

https://www.avrfreaks.net/commen...

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
Last Edited: Sun. Dec 12, 2021 - 07:47 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Without messing into your project, I feel very confusing press this much seconds to do this or that.

If you have two LEDs to turn on/off, much simpler, just press once within half a second will turn on the first LED.

Press twice within half a second, will turn on the second LED.

Press and hold for more than half a second, will turn off what is on.

 

The switch can cause an interrupt OR NOT.

Once the switch is sensed, or its interrupt is called, start timer and just wait the switch to be released, if when released the timer counted more than 500ms, turn off everything.

If the switch is released before 500ms, see is a second press happens within the 500ms, if yes, turn on the second LED.

If the 500ms time lapse expired with just the first press, turn on the first LED.

 

That is simpler.

 

You can implement the flasher by holding the switch pressed for 3 seconds or more.  As the flasher WILL NEVER BE USED, trust me, the 3 seconds control will be forgotten.

I have a bunch of flashlights with flasher and even SOS flashing, I would love to disable that pain in the neck sequence when it starts without my own desire.

 

Your schematic shows very weak LED light, directly driven by the uC... I thought you will be using a 3W/5W LED driven by a MosFET with PWM for brightness control.

That 10k resistor will make the blue LED turn on with shame to exist.  You said 50% brightness with 10k resistor? I doubt it very much.

 

Wagner Lipnharski
Orlando Florida USA

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

[edit]

Hey wait - Who the hell woke this thread up ?

 

 

Hamza1997 wrote:

This is how it works

Single press- Turns on the first LED

Single press- Turns off the first LED

There are different modes in the first led. The modes are 50% brightness and flasher mode. Those both are achieved when LED 1 is on and we press and hold the button for 2 seconds

When both LEDs are off, pressing and holding the button for 3seconds turns on the second LED and when we press the button, that second LED is turned off.

 

That's got to be the very worst UI I've seen in a long time.

 

Proper LED torches/flashlights have special latching mechanical switches and are very easy to use. You can get very near that ease-of-use with a simple pushbutton by this method:

 

  • A long holding press to switch the entire thing ON or OFF.
  • A short button click (press & release) cycles through the LED driving modes.

 

Last Edited: Sun. Sep 18, 2022 - 09:43 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

N.Winterbottom wrote:

[edit]

Hey wait - Who the hell woke this thread up ?

 

 

 

 

A spammer, who may have done it gain.  I'll lock this if it happens again

 

Jim

I would rather attempt something great and fail, than attempt nothing and succeed - Fortune Cookie

 

"The critical shortage here is not stuff, but time." - Johan Ekdahl

 

"Step N is required before you can do step N+1!" - ka7ehk

 

"If you want a career with a known path - become an undertaker. Dead people don't sue!" - Kartman

"Why is there a "Highway to Hell" and only a "Stairway to Heaven"? A prediction of the expected traffic load?"  - Lee "theusch"

 

Speak sweetly. It makes your words easier to digest when at a later date you have to eat them ;-)  - Source Unknown

Please Read: Code-of-Conduct

Atmel Studio6.2/AS7, DipTrace, Quartus, MPLAB, RSLogix user