Arduino Forum

Using Arduino => Programming Questions => Topic started by: zoutepopcorn on Jul 18, 2014, 02:37 pm

Title: Wake up pin attiny85 sleep mode.
Post by: zoutepopcorn on Jul 18, 2014, 02:37 pm
Hello,

I have a ATtiny85 and I can put it in sleeping code with my code and I can do a wake up at pin 4 (hardware 3).

I had the SoftwareSerial connection for testing. So now I want to remove the SoftwareSerial connection to let the tiny run on 1mhz. I notice that when I removed the RX from pin 4, the wake up pin isn't working anymore.

When I dont call  "mySerial.begin(9600);" the wake up pin stops working. So I tried to make pin 4, input / output, HIGH, LOW. But I can't get it working without the Serial part. I looked in to the SoftwareSerial code, but I didn't see a solution..

When I removed

Code: [Select]
#include <SoftwareSerial.h>
SoftwareSerial mySerial(4, 3);    // RX(not connected), TX
#include <avr/sleep.h>
#include <avr/power.h>

const int audio = 0;      // pin 5
const int play = 1;       // pin 6
const int rec = 3;        // pin 2
const int sensor = 2;     // pin 7


void setup() {
  digitalWrite(audio, HIGH);
  mySerial.begin(9600);   // <<< When I remove it, it stop working
  wakeSetup();
}

int count = 0;
int sleepCounter = 0;
int shakeWaarde = 0;
int tmpWaarde = 0;
void loop() {
    delay(30);
    count++;
    boolean waarde = digitalRead(sensor);
     if(waarde != tmpWaarde) {
      shakeWaarde++;
    }
    if(count > 25){
      if(shakeWaarde > 7) {
        //mySerial.println("Record");
        record();
        count = shakeWaarde = sleepCounter = 0;
      }
      sleepCounter++;
      count = 0;
      if(sleepCounter > 3) {  // GOTO SLEEP
        //mySerial.println("Going to sleep");
        sleepCounter = 0;
        sleepSetup();
        enterSleep();
      }
    }
    tmpWaarde = waarde;
}

void record() {
  digitalWrite(audio, HIGH);
  delay(350);
  digitalWrite(rec, HIGH);
  delay(4000);
  digitalWrite(rec, LOW);
  delay(350);
  digitalWrite(play, HIGH);
  delay(4000);
  digitalWrite(play, LOW);
  digitalWrite(audio, LOW);
  delay(350);
}


void pin2Interrupt(void) {
  detachInterrupt(0);
}

void enterSleep(void){
  /* Setup pin2 as an interrupt and attach handler. */
  attachInterrupt(0, pin2Interrupt, CHANGE);
  delay(100);
  set_sleep_mode(SLEEP_MODE_PWR_DOWN);
  sleep_enable();
  sleep_mode();
  /* The program will continue from here. */
  /* First thing to do is disable sleep. */
  sleep_disable();
  wakeSetup();
  delay(500);
  //mySerial.println("Wake up");
}

void sleepSetup() {
  pinMode(audio,INPUT);       
  pinMode(play,INPUT);       
  pinMode(sensor,INPUT);
  pinMode(rec,INPUT);
}

void wakeSetup() {
  delay(50);
  pinMode(rec,OUTPUT);      // 
  pinMode(play,OUTPUT);      // 
  pinMode(audio,OUTPUT);
  pinMode(sensor,INPUT);
  digitalWrite(rec, LOW);
  digitalWrite(sensor, HIGH);
  digitalWrite(audio, HIGH);
}
Title: Re: Wake up pin attiny85 sleep mode.
Post by: BijendraSingh on Jul 18, 2014, 02:47 pm
Quote
I notice that when I removed the RX from pin 4

because receive pin generate an interrupt when it receive some data.
one method to wake up from sleep mode is generate a interrupt.
Quote
When I dont call  "mySerial.begin(9600);" the wake up pin stops working

because your myserial object can not initialize without running begin(baud rate).
Title: Re: Wake up pin attiny85 sleep mode.
Post by: zoutepopcorn on Jul 18, 2014, 04:29 pm
Aha.

I see. I think now I can the code with the option:

- Goto sleep mode (1/4 second)
- Compare pin state with prev. pin state.
- Goto sleep if pinstate is the same

I will try
Title: Re: Wake up pin attiny85 sleep mode.
Post by: zoutepopcorn on Jul 18, 2014, 05:03 pm
I rewrote my code. I can make it now. The program work like this now:

- Goto sleep mode (250ms)
- Check if we have to wake up
- Goto sleep again..

Thanks for the help


Code: [Select]
#include <SoftwareSerial.h>
#include <avr/sleep.h>
#include <avr/power.h>

const int audio = 0;      // pin 5
const int play = 1;       // pin 6
const int rec = 3;        // pin 2
const int sensor = 2;     // pin 7

volatile boolean f_wdt = 1;


void setup() {
 
  wakeSetup();
  pinMode(4, INPUT);
  digitalWrite(4, HIGH);
  digitalWrite(audio, HIGH);
  digitalWrite(play, HIGH);
  digitalWrite(rec, HIGH);
  delay(1500);
  digitalWrite(audio, LOW);
  digitalWrite(play, LOW);
  digitalWrite(rec, LOW);
}

int count = 0;
int sleepCounter = 0;
int shakeWaarde = 0;
int tmpWaarde = 0;
int tmpWaarde2 = 0;

void loop() {
    delay(30);
    count++;
    boolean waarde = digitalRead(sensor);
     if(waarde != tmpWaarde) {
      shakeWaarde++;
    }
    if(count > 25){
      if(shakeWaarde > 7) {
        record();
        count = shakeWaarde = sleepCounter = 0;
      }
      sleepCounter++;
      count = 0;
      if(sleepCounter > 3) {  // GOTO SLEEP
         delay(2000);
        sleepCounter = 0;
        sleepSetup();
        enterSleep();
      }
    }
    tmpWaarde = waarde;
}

void record() {
  digitalWrite(audio, HIGH);
  delay(350);
  digitalWrite(rec, HIGH);
  delay(4000);
  digitalWrite(rec, LOW);
  delay(350);
  digitalWrite(play, HIGH);
  delay(4000);
  digitalWrite(play, LOW);
  digitalWrite(audio, LOW);
  delay(350);
}


void pin2Interrupt(void) {
  detachInterrupt(4);
}

void enterSleep(void){
  digitalWrite(audio, HIGH);
  delay(300);
  setup_watchdog(4);
  set_sleep_mode(SLEEP_MODE_PWR_DOWN);
  sleep_enable();
  sleep_mode();
  /* The program will continue from here. */
  /* First thing to do is disable sleep. */
  sleep_disable();

  sleepAgain();
}

void sleepAgain() {
  boolean waarde = digitalRead(sensor);
  if(waarde != tmpWaarde || waarde != tmpWaarde2) {
    pinMode(rec,OUTPUT); 
    pinMode(play,OUTPUT);
    digitalWrite(rec, HIGH);
    digitalWrite(play, HIGH);
    delay(1000);
    // wake up 
     //wakeSetup();
    // coninue here...

  } else {
  tmpWaarde = waarde;
  tmpWaarde2 = tmpWaarde;
  sleepSetup();
  enterSleep();
}
}


void sleepSetup() {
  pinMode(audio,INPUT);       
  pinMode(play,INPUT);       
  pinMode(sensor,INPUT);
  pinMode(rec,INPUT);
}

void wakeSetup() {
  delay(50);
  pinMode(rec,OUTPUT);      // 
  pinMode(play,OUTPUT);      // 
  pinMode(audio,OUTPUT);
  pinMode(sensor,INPUT);
  //digitalWrite(audio, HIGH);
}

// 0=16ms, 1=32ms,2=64ms,3=128ms,4=250ms,5=500ms
// 6=1 sec,7=2 sec, 8=4 sec, 9= 8sec
void setup_watchdog(int ii) {
  byte bb;
  int ww;
  if (ii > 9 ) ii=9;
    bb=ii & 7;
  if (ii > 7) bb|= (1<<5);
    bb|= (1<<WDCE);
  ww=bb;

  MCUSR &= ~(1<<WDRF);
  // start timed sequence
  WDTCR |= (1<<WDCE) | (1<<WDE);
  // set new watchdog timeout value
  WDTCR = bb;
  WDTCR |= _BV(WDIE);
}
 
// Watchdog Interrupt Service / is executed when watchdog timed out
ISR(WDT_vect) {
  f_wdt=1;  // set global flag
}