my program on arduino 1 rev 3 works fine. Pin 2 is pullupped internally when i put at low level pin 2 it resume from sleep and do his job . if i leave it open it goes sleeping.
But i should like to have it sleeping when pin 2 goes down , but arduino uno not works with attachinterrupt triggered HIGH in the istruction attachInterrupt(0, wake, LOW);
so i tryed to put it FALLING or CHANGE but it not resume from sleeping. ( i changed also digitalWrite (sveglia, HIGH) to eliminate pull up having the pin low at program start)
help pleaseeeee... thanks ( sorry for previous topic)
#include <avr/sleep.h>
#define LED 13
#define txrx 6
#define sveglia 2
#define impulsi 5
//======================================================================================================================================
// Variabili
unsigned long counts = 0; // Contatore unsigned long delgi impulsi fatti
int numimpulsi = 0; // impulsi da fare dal contatore
String sknum = "SK:0A5B44"; // identificativo scheda
long intervallo = 1000; // 3 secondi di ritardo prima che definisca che gli impulsi sono finiti
int temperatura = 0 ; // variabile temperatura
int tensione = 0; // variabile tensione carica
int reading = 0;
int flag=0;
unsigned long tempo;
unsigned long tempoprima;
//fine variabili
//=======================================================================================================================================
void setup() {
Serial.begin(9600);
// disable ADC
ADCSRA = 0;
digitalWrite (sveglia, HIGH); // pull-up il pin 2 e' quello che si sveglia se messo a massa
pinMode (LED, OUTPUT);
digitalWrite (LED, HIGH); // awake
pinMode (txrx, OUTPUT);
digitalWrite (txrx, LOW); // abilito trasmettitore
Serial.println ("INIZIO LED ACCESO");
//setup contatore impulsi
pinMode(impulsi, INPUT); digitalWrite(5, HIGH); // pulses on pin-5 = PD5 = T1 pull up pin 5
// TCCR0A=0; // setup timer-0
// TCCR0B=0;
// TIMSK0=0;
TCCR2A=0; // setup timer-2
TCCR2B=0;
TIMSK2=0;
TCCR1A=0; // setup timer-1
TCCR1C=0;
TIMSK1=0;
GTCCR=0;
Serial.println("PARTENZA CONTEGGIO QUANDO la scheda si sveglia per il pin 2 e ARRIVANO IMPULSI ");
delay(1000);
digitalWrite (txrx, HIGH); // disabilito trasmettitore
TCNT1=0; //azzero contatore
} // fine setup
//====================================================================================================================================================
void loop() {
if (digitalRead (2)== HIGH) // se il pin 2 e' a 1 fisso come sempre perche' e' pullup allora sleep (%%%%%% da cambiare deve andare a 0 fisso)
{ sleepNow();}
else { // se il pin 2 e' a 0 fisso perche' ce lo metto allora parte la routine di wakeup e continua il void loop passando qui
// ROUTINE CONTROLLO IMPULSI
tempo=millis();
reading = digitalRead(5); //se il pin degli impusi e' visto ogni tanto a 1 parte il reading high
// IL CONTATORE E' PARTITO
if (reading == HIGH) {
tempoprima=tempo;
digitalWrite(LED, HIGH); // ACCENDE LED sto contando
TCCR1B=0; // stop count devo fermare per leggere
counts = TCNT1; // read count PUNTO ESSENZIALE LETTURA DEL CONTEGGIO
TCCR1B=7; // start count riparte il contatore
flag=1;
}// chiusura if reading high
// FINE ROUTINE CHE LEGGE IMPULSI ****************************
// IL CONTATORE E' FERMO O HA FINITO DI CONTARE PERCHE' SONO FINITI GLI IMPULSI
if (reading == LOW) {
// SE IL TEMPO DI ATTESA E' FINITO
if(flag==1 && (tempo-tempoprima) > intervallo) {
digitalWrite(LED , LOW); //LED SPENTO conteggio disattivato
flag=0;
TCCR1B=0; // stop count
TCNT1=0; //azzero contatore
// CONTROLLO tensione e temperatura
temperatura = analogRead(0); // controlla la temperatura pin 0
tensione = analogRead(1); // controlla la tensione pin 1
// FINE CONTROLLO
digitalWrite (txrx, LOW); // abilito trasmettitore
delay(5);
Serial.println (sknum);
Serial.print ("IMP=");
Serial.println(counts);
Serial.print ("GRADI ");
Serial.println(temperatura);
Serial.print ("VOLT ");
Serial.println(tensione);
delay(60);
digitalWrite (txrx, HIGH); // disabilito trasmettirore
// AGGIUNGI se tensione e' minore di tot manda allarme e si ferma
}//chiusura if flag
}// chiusura if reading
}// chiusura if digitalread 2
}//chiusura void loop
// END VOID LOOP
//===================================================================================================================================================================
// interrupt service routine
void wake()
{
sleep_disable(); // first thing after waking from sleep:
detachInterrupt (0); // stop many many interrupts
}
// fine interrupt service routine
//=================================================================================================================================================================
// sleep routine
void sleepNow()
{
set_sleep_mode(SLEEP_MODE_PWR_DOWN); // sleep mode is set here
sleep_enable(); // enables the sleep bit in the mcucr register
// turn off brown-out enable in software
MCUCR = _BV (BODS) | _BV (BODSE);
MCUCR = _BV (BODS);
digitalWrite (txrx, LOW); // abilito trasmettitore
delay(5);
Serial.println("ENTRO IN SLEEP LED SPENTO");
delay(60);
digitalWrite (txrx, HIGH); // disabilito trasmettirore
digitalWrite (LED, LOW); // asleep
attachInterrupt(0, wake, LOW);
sleep_mode(); //IN QUESTO MOMENTO E' IN SLEEP FERMO QUI
// PUNTO DI RIENTRO DELLA ROUTINE AL WAKE UP quando parte interrupt 0 cioe'pin 2 basso
digitalWrite (LED, HIGH); // RISVEGLIO ACCENDO LED
digitalWrite (txrx, LOW); // abilito trasmettitore
delay(1000);
Serial.println("SONO TORNATO! LED ACCESO");
delay(60);
digitalWrite (txrx, HIGH); // disabilito trasmettirore
}
// end of sleepNow
//=============================================================================================================================================================