- Sat Sep 09, 2017 8:39 am
#69832
Well, i am new at this site and owning an ESP8266 (wemos d1 retired) since some days.
But i have some experience with Arduino.
I think, there are two easy possibilities:
1) Poll the switches attached to input pins. This could be done within a timer-interrupt for example
10 times a second. Take in account to debounce the switches with hard- or software.
2) Let the switches do an pin interrupt (debouncing is necessary as well)
I tested 2) to check, how many interrupts this blinding fast machine could handle:
A program that toggles GPIO 5 in a timer interrupt and an interrupt routine to get this toggling as an input for a pin interrupt. Up to 100000 interrupts per second are possible. Try it.
You could learn, how to handle timer interrupts and pin interrupts. Have fun
Code: Select all/*
* ESP8266 Wemos D1 (retired) 160 Mhz
*
* 100000 pin-interrupts per second !
*
* Helmut Weber 9.9.2017
*
* GPIO 4 and GPIO 5 must be connected
*
* This programm creates pin interrupts at pin 4
* by timer interrupts toggling pin 4
* The output from pin 4 is connected to pin 5 as input.
* A pin interrupt routine at pin 5 is called by the falling edge of the signal
*
*/
// Adresses of port-register
#define PIN_DIR 0x6000030C
#define PIN_OUT_SET 0x60000304
#define PIN_OUT_CLEAR 0x60000308
unsigned int *pdir, *poset, *poclear;
const byte interruptPin = 4;
const byte timerPin = 5;
volatile unsigned long interruptCounter = 0;
volatile unsigned long numberOfInterrupts = 0;
volatile char flag=0;;
void timerhandler() {
// rearm timer
// VERSION 1
timer0_write(ESP.getCycleCount() +680L);
// VERSION 2
//timer0_write(ESP.getCycleCount() +79850L);
if (flag==0) {
flag=1;
//digitalWrite(timerPin,0);
// clear pin 5 = GPIO 5
*poclear=B100000;
}
else {
flag=0;
//digitalWrite(timerPin,1);
// set pin 5
*poset=B100000;
}
}
void setup() {
// ____________ init ports direct _____________
// define pointer to ports
pdir=(unsigned int *)PIN_DIR;
poset=(unsigned int *)PIN_OUT_SET;
poclear=(unsigned int *)PIN_OUT_CLEAR;
Serial.begin(500000);
pinMode(interruptPin, INPUT_PULLUP);
pinMode(timerPin, OUTPUT);
attachInterrupt(digitalPinToInterrupt(interruptPin), handleInterrupt, FALLING);
noInterrupts();
timer0_isr_init();
timer0_attachInterrupt(timerhandler);
timer0_write(ESP.getCycleCount() +80000000L);
interrupts();
}
unsigned long lastCounter=0;
unsigned long lastTime=0;
unsigned long thisTime=0;
void handleInterrupt() {
interruptCounter++;
}
void loop() {
if(interruptCounter>0){
thisTime=micros();
//interruptCounter--;
//numberOfInterrupts++;
Serial.printf("%d in interrupts in %d microseconds\n ",interruptCounter-lastCounter, thisTime-lastTime );
//Serial.println(interruptCounter);
lastCounter=interruptCounter;
lastTime=thisTime;
}
// VERSION 1
delayMicroseconds(1000000-3250);
// Version 2
//delayMicroseconds(1000000-2780);
}
/*
This is the output of the program VERSION 1
100099 in interrupts in 999960 microseconds
100104 in interrupts in 1000010 microseconds
100097 in interrupts in 999935 microseconds
100105 in interrupts in 1000015 microseconds
100093 in interrupts in 999892 microseconds
100100 in interrupts in 999958 microseconds
99860 in interrupts in 997572 microseconds
99869 in interrupts in 997690 microseconds
99843 in interrupts in 997351 microseconds
100112 in interrupts in 1000088 microseconds
100102 in interrupts in 999987 microseconds
100096 in interrupts in 999922 microseconds
....
*/
/*
This is the output of the rogram VERSION 2
1001 in interrupts in 999998 microseconds
1000 in interrupts in 999940 microseconds
1000 in interrupts in 999992 microseconds
1001 in interrupts in 1000009 microseconds
1000 in interrupts in 1000029 microseconds
1000 in interrupts in 999949 microseconds
1000 in interrupts in 999989 microseconds
1000 in interrupts in 999940 microseconds
1001 in interrupts in 1000014 microseconds
1000 in interrupts in 999968 microseconds
1000 in interrupts in 1000033 microseconds
*/
A timer interrupt every 10ms should do your job fine