Pages: [1]   Go Down
Author Topic: Rookie trying to put Shrinkified Arduino to Sleep....  (Read 507 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 7
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello everyone! Forgive my poor skills, as I still try to educate myself in programming.  I am trying to utilize an ATTINY85 with a push button switch (B3F-1000) to Start and Stop a looped program by putting the chip to Sleep and by Waking it up with the push of the same button.  I am trying to integrate Nick Gammon's sketch from his website with my preexisting looped sketch, but in addition to butchering Nick's code, I am failing at successfully merging the two into one.  I am getting an error with the ATTINY (that I would not get with a regular Arduino Uno); it says,

 "Gammon_Sleep.cpp: In function 'void loop()':
Gammon_Sleep:42: error: 'BODS' was not declared in this scope
Gammon_Sleep:42: error: 'BODSE' was not declared in this scope"

 Below, I'll post both scripts I'm trying to merge, in case someone is willing and able to provide some assistance.

Code:
int led = 0;

// the setup routine runs once when you press reset:
void setup() {               
  // initialize the digital pin as an output.
  pinMode(led, OUTPUT);     
}

// the loop routine runs over and over again forever:
void loop() {
  digitalWrite(0, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(750);               // wait for a second
  digitalWrite(0, LOW);    // turn the LED off by making the voltage LOW
  delay(750);               // wait for a second
  digitalWrite(0, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(750);               // wait for a second
  digitalWrite(0, LOW);    // turn the LED off by making the voltage LOW
  delay(750);               // wait for a second
  digitalWrite(0, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(750);               // wait for a second
  digitalWrite(0, LOW);    // turn the LED off by making the voltage LOW
  delay(2000);               // wait for a second or two
}

Code:
#include <avr/sleep.h>

const byte LED = 9;
 
void wake ()
{
  // cancel sleep as a precaution
  sleep_disable();
  // must do this as the pin will probably stay low for a while
  detachInterrupt (0);
}  // end of wake

void setup ()
  {
  digitalWrite (2, HIGH);  // enable pull-up
  }  // end of setup

void loop ()
{
 
  pinMode (LED, OUTPUT);
  digitalWrite (LED, HIGH);
  delay (50);
  digitalWrite (LED, LOW);
  delay (50);
  pinMode (LED, INPUT);
 
  // disable ADC
  ADCSRA = 0; 
 
  set_sleep_mode (SLEEP_MODE_PWR_DOWN); 
  sleep_enable();

  // Do not interrupt before we go to sleep, or the
  // ISR will detach interrupts and we won't wake.
  noInterrupts ();
 
  // will be called when pin D2 goes low 
  attachInterrupt (0, wake, LOW);
 
  // turn off brown-out enable in software
  // BODS must be set to one and BODSE must be set to zero within four clock cycles
  MCUCR = _BV (BODS) | _BV (BODSE);
  // The BODS bit is automatically cleared after three clock cycles
  MCUCR = _BV (BODS);
 
  // We are guaranteed that the sleep_cpu call will be done
  // as the processor executes the next instruction after
  // interrupts are turned on.
  interrupts ();  // one cycle
  sleep_cpu ();   // one cycle

  } // end of loop

Thanks in advance for any help; the camaraderie and support on this forum always amazes me.
-Michael
Logged

USA, FL
Offline Offline
God Member
*****
Karma: 14
Posts: 688
A life? Where can I download one of those?
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I am thinking that it's the delays in your first sketch.  Look at the "Blink Without Delay" example in the IDE to get a method of using a sate machine.  This will get rid of the delays.

I don't think that Gammon's library is compatible with the ATTiny.
Logged

//LiNK

Offline Offline
Newbie
*
Karma: 0
Posts: 7
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks Codlink.  The only thing is that my first sketch does work on the attiny.  Nick's code work on the standard Arduino Uno without a problem, it's just trying to put the attiny to sleep with the second code where I am having a problem when verifying the code (and I get the aforementioned error). Can the attiny be put into some kind of sleep mode like its big brother?

Michael
Logged

texas
Offline Offline
God Member
*****
Karma: 27
Posts: 862
old, but not dead
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

It can be put to sleep, but it looks like there are some subtle register name differences causing your compile errors.  You should be able to resolve the new name by looking in the attiny's datasheet.
Logged

Experience, it's what you get when you were expecting something else.

Offline Offline
Newbie
*
Karma: 0
Posts: 7
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks Afremont. Any examples of what to look for/convert. I'm happy to put in the work, but need to know what direction to head in as far as a few key words....

Thanks!

Logged

texas
Offline Offline
God Member
*****
Karma: 27
Posts: 862
old, but not dead
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

A quick look at the attiny85 datasheet shows that they do the brown out stuff way different than the mega chips.  It looks like the SUT (start up time) selection determines whether the BOD (brown out detection) is active.  The attiny85 has a register that you configure the brown out voltage level with, BODLEVEL fuses.  It appears that they don't have the same functionality when it comes to SLEEP mode that the mega chips have with BODS and BODSE.  Not sure what to tell you at this point without spending way too much time figuring it all out.  smiley-wink

Logged

Experience, it's what you get when you were expecting something else.

East Anglia (UK)
Offline Offline
Faraday Member
**
Karma: 108
Posts: 4018
May all of your blinks be without delay()
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Whatever the problem is I was amused by these 2 lines of code and the comments
Code:
delay(750);               // wait for a second
and
Code:
delay(2000);               // wait for a second or two
I particularly liked the second one although, of course it should have been
Code:
delay(1000 || 2000);               // wait for a second or two

 smiley
Logged

Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

Offline Offline
Newbie
*
Karma: 0
Posts: 7
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks Afremont, I'll have to keep searching...maybe someone else already solved this...I'm sure I'm not the first to put a Tiny to lssleep, I appreciate your help.

Helibob, glad to provide you with some amusement, I'm here all week.  In actuality, I just threw a quick comment in there as I am still playing with the delays in milliseconds.

Logged

Pages: [1]   Go Up
Jump to: