【发布时间】:2021-05-06 01:26:57
【问题描述】:
你好,当我不使用我的 Arduino 时,我希望它进入睡眠状态,这样我可以节省电池电量。这是我现在的输出 我要醒来 我要醒来 我要睡觉了
当它进入睡眠状态时,它不会在我拍手时再次醒来。
我正在使用 arduino uno 和这个传感器 https://www.auselectronicsdirect.com.au/audio-microphone-module-for-arduino-projects?gclid=CjwKCAjwhMmEBhBwEiwAXwFoEVAz-xxw7jaHoXDWixiTV1IHz8TF5i4tKr_1jjTK8s3JvowUDUDnCBoCxdgQAvD_BwE
这是我的代码
#include <Servo.h>
#include <avr/sleep.h>
#define interruptPin 2
Servo myservo;
int pos = 0;
int soundSensor = 2;
boolean LEDStatus = false;
void setup() {
Serial.begin(9600);
pinMode(interruptPin, INPUT_PULLUP);
myservo.attach(7);
pinMode(soundSensor, INPUT);
}
void Goint_to_Sleep() {
Serial.println(" i am going to sleep");
delay(5000);
sleep_enable();
attachInterrupt(digitalPinToInterrupt(interruptPin), wakeUp, LOW);
set_sleep_mode(SLEEP_MODE_PWR_DOWN);
sleep_cpu();
}
void wakeUp() {
sleep_disable();
detachInterrupt(0);
}
void loop() {
int SensorData = digitalRead(soundSensor);
if (SensorData == 1) {
wakeUp();
Serial.println(" i am going to to wake up");
if (LEDStatus == false) {
LEDStatus = true;
for (pos = 180; pos >= 0; pos -= 40) { // goes from 180 degrees to 0 degrees
// in steps of 1 degree
myservo.write(pos);
delay(15); // waits 15ms for the servo to reach the position
}
} else {
LEDStatus = false;
for (pos = 0; pos <= 180; pos += 20) { // goes from 0 degrees to 180 degrees
myservo.write(pos);
delay(15); // waits 15ms for the servo to reach the position
Goint_to_Sleep();
}
}
}
}
【问题讨论】:
标签: c# arduino-uno