#include <avr/sleep.h>
#include <avr/power.h>
//This sketch is a metronome, in which each led pin shows one beat.
const int speaker = 13; //speaker at pin 13
int ts = 6; //highest led pin
int count = 3; //"beat 1 pin"
const int keyPin = 11; //button to change time signature
const int keyPinR = 10; //button to change rhythm
const int keySwitch = 2; //sleep mode/wake up button
//All pins below use a pull-up resistor, so the states are inverted
int buttonState = LOW;
int buttonState2 = LOW;
int switchState = LOW;
int silentState = LOW;
int bpm = 120; //you do not need to know this,
//it's just a variable for the potentiometer to change the tempo
int rthm = 1; //rhythm
volatile int pressed = 0;
long silent = p;
void setup()
{
Serial.begin(9600);
for(int thisPin = 3;thisPin <= ts;thisPin++)
{
pinMode(thisPin,OUTPUT); //initialize thisPin as an output
pinMode(8, INPUT); //potentialmeter for changing temp
//Pin 9 to 11, along with pin 2, are all push buttons with pull-up resistors
for(int i = 9; i<=11; i++)
{
pinMode(i, INPUT);
digitalWrite(i, HIGH);
}
pinMode(2, INPUT);
digitalWrite(2,HIGH);
}
}
void loop()
{
turnOn();
}
void turnOn()
{
buttonState = digitalRead(keyPin);//key for
buttonState2 = digitalRead(keyPinR);
switchState = digitalRead(keySwitch);
silentState = digitalRead(9);
//Serial.println(switchState);
if(switchState==LOW&&pressed==0)
{
sleep_enable();
set_sleep_mode(SLEEP_MODE_PWR_DOWN);
attachInterrupt(digitalPinToInterrupt(0), interrupt, LOW);
sleep_cpu();
//Serial.println(switchState);
}
else {
//The beat starts at pin 3, runs in consecutive pins,
//and ends at pin 8 at maximum
if(buttonState==LOW)
{ //changing time signature - up to 5 beats
ts++;
if(ts==8)
{
ts = 3;
//8 - 3 = 5 beats max
}
}
if(buttonState2==LOW)
{ //changing rhythm - quarter note to eighth, then
//triplets and sixteenths, back to quarter notes
rthm++;
if(rthm==5)
rthm = 1;
}
if(silentState==LOW){//if silent key pressed, go to silence (only LED's showing)
silent++;
}
//The RUN code
for(int thisPin = 3;thisPin <= ts;thisPin++)
//LED's blinking beats (thisPin - LED pin (beat number + 2))
{
for(int r = 1; r<=rthm; r++)
{//nested loop for the rhythm
bpm = pulseIn(8, HIGH);//pin 8 - tempo potentialmeter
digitalWrite(thisPin,HIGH);//each LED blinks
pinMode(13,OUTPUT); //pin 13 - speaker
if(silent%2==0)//Press once, tone, press again, no tone
noTone(13);
else
if(count == 3&&r==1). //first beat is higher
tone(13,4400,80);
else if(r==1). //downbeats slightly lower
tone(13,2500,80);
else
tone(13,2200,60); //off beats even lower
delay((bpm-52)/rthm);
digitalWrite(thisPin,LOW); //turn each off as we proceed to the next beat (LED)
pinMode(13,INPUT); //speaker input
}
count++; //beat++
if(count >ts){//go back to count
count = 3;
}
}
}
}
void noSound()//silence the speaker
{
noTone(13);
}
void interrupt()//this is where the sleep interrupt starts - //how can i press it to wake up???
{
sleep_disable();
detachInterrupt(0);
if(switchState==HIGH){
pressed = 1;
Serial.println(switchState);
turnOn(); //go back to the main functional method (?????)
}
}
Once the keySwitch button is pressed (pin 2), the arduino goes to sleep; however, the only way to turn it on is by resetting everything. How can I just wake up the Arduino, keeping every state as before the sleep interrupt?
dinotom
3602 gold badges6 silver badges18 bronze badges
asked Apr 22, 2016 at 17:08
1 Answer 1
attachInterrupt(digitalPinToInterrupt(0), interrupt, LOW);
should be either
attachInterrupt(0, interrupt, LOW);
or
attachInterrupt(digitalPinToInterrupt(2), interrupt, LOW);
As pin 0 is not an interrupt pin. Also the button is connected to pin 2, not pin 0. digitalPinToInterrupt(0)
would actually return -1
.
answered Apr 22, 2016 at 17:29
-
It would be a lot more useful if you explained your apparent theory that the wrong pin is being monitored rather than just proposing an unexplained change to the code.Chris Stratton– Chris Stratton2016年04月22日 17:52:05 +00:00Commented Apr 22, 2016 at 17:52
-
@ChrisStratton thanks for the suggestion. MikeZhou; you are welcome.Gerben– Gerben2016年04月22日 18:15:04 +00:00Commented Apr 22, 2016 at 18:15
lang-cpp