This commit is contained in:
2022-09-11 22:35:41 +08:00
commit 10274cbb4c
144 changed files with 36625 additions and 0 deletions
@@ -0,0 +1,48 @@
/*
Copyright 2021 Peter Harrison - Helicron
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
https://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
This example creates a single encoder instance that is updated by
running the service method directly in the main program loop. This
must be called frequently enough to avoid missing any pin changes. That
means several hundred times a second in many cases. If a change is detected
in the main loop, the current count is sent to the serial port.
You can add a delay into the loop in this eample to see just how often you
need to call the service method and still get reliable counts.
*/
#include <Arduino.h>
#include <BasicEncoder.h>
const int8_t pinA = 2;
const int8_t pinB = 3;
BasicEncoder encoder(pinA, pinB);
void setup() {
Serial.begin(115200);
Serial.println(F("Polling in loop()"));
}
void loop() {
encoder.service();
int encoder_change = encoder.get_change();
if (encoder_change) {
Serial.println(encoder.get_count());
}
// Even a short delay here will affect performance.
// Uncomment and change the delay to see what happens.
//delay(10);
}
@@ -0,0 +1,50 @@
/*
Copyright 2021 Peter Harrison - Helicron
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
https://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
This example creates two independent encoders and samples them using a
timer interrupt. If either changes, the current count for both is sent to
the serial port.
*/
#include <Arduino.h>
#include <BasicEncoder.h>
#include <TimerOne.h>
BasicEncoder encoderA(2, 3);
BasicEncoder encoderB(10, 11);
/****************************************************************/
void timer_service() {
encoderA.service();
encoderB.service();
}
void setup() {
Serial.begin(115200);
Timer1.initialize(1000);
Timer1.attachInterrupt(timer_service);
}
void loop() {
int encoder_a_change = encoderA.get_change();
int encoder_b_change = encoderB.get_change();
if (encoder_a_change || encoder_b_change) {
Serial.print(encoderA.get_count());
Serial.print(' ');
Serial.print(encoderB.get_count());
Serial.println();
}
}
@@ -0,0 +1,65 @@
/*
Copyright 2021 Peter Harrison - Helicron
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
https://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
This example creates a single encoder instance that is updated by
pin-change interrupts associated with the two pins. If a change is detected
in the main loop, the current count is sent to the serial port.
Note that if you enable pin change interrupts for other pins in the same
group, you may cause conflicts.
*/
#include <Arduino.h>
#include <BasicEncoder.h>
const int8_t pinA = 2;
const int8_t pinB = 3;
BasicEncoder encoder(pinA, pinB);
void pciSetup(byte pin) // Setup pin change interupt on pin
{
*digitalPinToPCMSK(pin) |= bit(digitalPinToPCMSKbit(pin)); // enable pin
PCIFR |= bit(digitalPinToPCICRbit(pin)); // clear outstanding interrupt
PCICR |= bit(digitalPinToPCICRbit(pin)); // enable interrupt for group
}
void setup_encoders(int a, int b) {
uint8_t old_sreg = SREG; // save the current interrupt enable flag
noInterrupts();
pciSetup(a);
pciSetup(b);
encoder.reset();
SREG = old_sreg; // restore the previous interrupt enable flag state
}
ISR(PCINT2_vect) // pin change interrupt for D0 to D7
{
encoder.service();
}
void setup() {
Serial.begin(115200);
Serial.println("Interrupts");
setup_encoders(pinA,pinB);
encoder.set_reverse();
}
void loop() {
int encoder_change = encoder.get_change();
if (encoder_change) {
Serial.println(encoder.get_count());
}
}
@@ -0,0 +1,49 @@
/*
Copyright 2021 Peter Harrison - Helicron
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
https://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
This example creates a single encoder instance that is updated by
a timer interrupt running at 1kHz. If a change is detected
in the main loop, the current count is sent to the serial port.
Be sure that the timer interrupt is frequent enough that it cannot miss
a change in pin state.
*/
#include <Arduino.h>
#include <BasicEncoder.h>
#include <TimerOne.h>
BasicEncoder encoder(2, 3);
/****************************************************************/
void timer_service() {
encoder.service();
}
void setup() {
Serial.begin(115200);
Timer1.initialize(1000);
Timer1.attachInterrupt(timer_service);
encoder.set_reverse();
}
void loop() {
int encoder_change = encoder.get_change();
if (encoder_change) {
Serial.println(encoder.get_count());
}
}