Attiny85 control driver led

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

Hi everyone, I hope a lot in your help, I have a circuit with attiny85 that does on off with sleep, etc., I would like you to control a led driver in pwm but I don't know how to do it.

https://www.flashled.com.ua/imag...

 

#include <avr/sleep.h>
#include <avr/power.h>

const byte LED = 1;  // D1 / pin 2
const byte SWITCH = 2; // D2 / pin 6 / PCINT2
volatile byte last_sw_state = HIGH;
volatile byte led_state = LOW;
volatile long last_sw_change_millis = 0;
byte sw_state = HIGH;
long sw_read_millis = 0;

 // Interrupt Service Request. Executed when the state of the pin changes
 ISR(PCINT0_vect)
 {
 }
 
void setup()
{
  pinMode (LED, OUTPUT);
  pinMode (SWITCH, INPUT_PULLUP);

  cli(); // Disable interrupt
  // Pin change interrupt
  PCMSK  |= bit (PCINT2);  // D2 / pin 7
  GIFR   |= bit (PCIF);    // Clear any outstanding interrupts
  GIMSK  |= bit (PCIE);    // Enable pin change interrupts
  sei(); // Enable interrupt
}

void loop()
{
  sw_state = digitalRead(SWITCH); // or faster: (PINB >> PINB2) & 1
  sw_read_millis = millis();
  if (last_sw_state==HIGH && sw_state==LOW) { // Falling edge
      last_sw_change_millis = sw_read_millis;
  }
  if (last_sw_state==LOW && sw_state==LOW &&
      sw_read_millis-last_sw_change_millis>1000) {
    led_state = !led_state;    
    digitalWrite (LED, led_state);
    enter_sleep ();
  }
  last_sw_state = sw_state; 
}

void enter_sleep ()
{
  // Enter sleep
  set_sleep_mode(SLEEP_MODE_PWR_DOWN);
  ADCSRA = 0; // Turn off ADC
  power_all_disable (); // Power off ADC, Timer 0 and 1, serial interface
  sleep_enable();
  sleep_cpu();

 // …zzz

 // Wake up
  sleep_disable();
  power_all_enable(); // Power everything back on
}

 

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

Ever sleep mode but idle mode turns off the clock to the timers, so no PWM while sleeping!!!

 

Jim

 

 

(Possum Lodge oath) Quando omni flunkus, moritati.

"I thought growing old would take longer"

 

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

but I also need sleep, what solution do you tell me to do, I don't understand anything

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

ki0bk wrote:

Ever sleep mode but idle mode turns off the clock to the timers, so no PWM while sleeping!!!

 

Jim

 

it seems strange to think that it can't be done, I realized that when it is in sleep there is no pwm, let's only go to sleep when it is off

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

Lidas wrote:

it seems strange to think that it can't be done

Well it would help to know what "it" is, so if you could let us know what your trying to accomplish, in other words, what is the overall goal?

We only know at this point you want to PWM (control the brightness?) of some LEDs, but not why the mpu needs to sleep while doing this.

As said, in all sleep modes but idle mode, all PWM outputs stop(in all sleep modes but idle) in what ever state they are in at the time the sleep mode is entered.

As the power required by the mpu is small compared to the power needed to drive the LED's, why is sleep mode required, what are you trying to accomplish?

Jim

 

 

(Possum Lodge oath) Quando omni flunkus, moritati.

"I thought growing old would take longer"

 

Last Edited: Wed. Jan 22, 2020 - 04:03 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

ki0bk wrote:

Lidas wrote:

 

it seems strange to think that it can't be done

Well it would help to know what "it" is, so if you could let us know what your trying to accomplish, in other words, what is the overall goal?

.....Jim

 

Yes you are right I have not explained much, I have to turn on a power LED on an underwater torch via a piezo button, the torch is on batteries which is why sleep, sleep can only intervene when the torch is off, since I have space problems, who gave me the driver told me that it can be controlled directly in pwm,
I press the On button - 100%
I still press 50%
I still press OFF (and all in sleep)

there must also be a debounce or delay to avoid accidental ignition

I hope I explained it

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

no help, come on make an effort try to help me

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

Lidas wrote:
no help, come on make an effort try to help me

 

This really isn't an effective way to ask for help. It's rude.

 

The ATTiny85 does not have an asynchronous timer so if you want PWM, the MCU must be running.

 

What you *can* do is use the connected button to fire an interrupt. The ATTiny85 can sleep when the torch is off and be woken from sleep by an interrupt triggered by the button.

 

You could then use a variable to store the brightness state.

 

When the button is pressed first time, the interrupt is fired which wakes the MCU. It checks the brightness variable and sets it to HIGH.

Second press, MCU is already awake, so sets brightness to LOW

Third press, set brightness OFF, got to sleep.

 

The ATTiny85 can't power an LED when it is sleeping, but this shouldn't be a problem because the LED is likely the largest current drain in this application even when the MCU is running.

 

This *seems* to be what your code is doing, although I can't see where it is setting the PWM for the LED, so maybe get that bit working first? there are lots of resources for LED PWM.

 

Last Edited: Fri. Jan 24, 2020 - 03:49 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Excuse me so much but it is not my intention to be rude, for someone like me who does not understand us much, your help is precious, I just have to thank

 

 

what you tell me to do for me is very incomprehensible, come on I just started to understand it, and frankly I don't understand much about it, the skech that I posted was given to me by another user, I would need someone to slip what to do ,

 

I kindly ask you for your help

 

sorry again

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

The Arduino tutorial documents are a good place to start:

 

https://www.arduino.cc/en/tutorial/fade

 

Once you have the LED brightness working, get the switch to turn it on and off:

 

https://www.arduino.cc/en/tutorial/button

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

Lidas wrote:
sleep can only intervene when the torch is off

So the Sleep and the PWM are mutually exclusive.

 

So you can get to grips with the PWM first - without any Sleep to complicate the issue.

 

have you studied the description of the PWM in the datasheet?

 

have you checked out the Application Note on using the timers?

 

They're all available on the Product Page:

 

https://www.microchip.com/wwwproducts/en/ATtiny85#datasheet-toggle

 

Googling "Attiny85 pwm" gives many hits - have you checked them out?

 

 

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...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Lidas wrote:
come on I just started

Remember that we know nothing about you - other than what you state in your post.

 

Lidas wrote:
the skech that I posted was given to me by another user

have you asked that user to explain it?

 

perhaps you would be better starting with some basics - see Tip #6 in my signature (below; may not be visible on mobile)

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: Fri. Jan 24, 2020 - 03:59 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Easy to say complicated to do, anyway thanks the same, but let me say that at least I,

if I get to ask on the forums is because I can't solve a thing and no because I don't feel like it,

anyway thanks