Skip to content

Instantly share code, notes, and snippets.

@nayutaya
Created March 21, 2016 14:13
Show Gist options
  • Save nayutaya/524c2787e44f896cbe5a to your computer and use it in GitHub Desktop.
Save nayutaya/524c2787e44f896cbe5a to your computer and use it in GitHub Desktop.
スピーカLEDの制御装置。フットスイッチでOFF、50%ON、100%ONを切り替え。マイコンはESP-WROOM-02。
const uint8_t PIN_PWM = 12;
const uint8_t PIN_FOOT_SWITCH = 5;
uint8_t countOfSwitchPushed = 0;
uint32_t switchCounterLastUpdatedTime = 0;
void setup() {
Serial.begin(115200);
Serial.println("ver2");
pinMode(PIN_PWM, OUTPUT);
pinMode(PIN_FOOT_SWITCH, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(PIN_FOOT_SWITCH), updateSwitchCounter, FALLING);
switchCounterLastUpdatedTime = millis();
}
void loop() {
uint16_t value = 0;
switch ( countOfSwitchPushed % 3 ) {
case 0: value = 0; break;
case 1: value = 511; break;
case 2: value = 1023; break;
}
analogWrite(PIN_PWM, value);
delay(50);
}
void updateSwitchCounter() {
const uint32_t now = millis();
if ( now - switchCounterLastUpdatedTime > 100 ) {
countOfSwitchPushed++;
}
switchCounterLastUpdatedTime = now;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment