Skip to content

Commit

Permalink
Examples updated
Browse files Browse the repository at this point in the history
  • Loading branch information
igorantolic committed Feb 27, 2021
1 parent 3bc7cc7 commit 68e0deb
Show file tree
Hide file tree
Showing 8 changed files with 320 additions and 98 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
#include "AiEsp32RotaryEncoder.h"
#include "Arduino.h"

/*
connecting Rotary encoder
Rotary encoder side MICROCONTROLLER side
------------------- ---------------------------------------------------------------------
CLK (A pin) any microcontroler intput pin with interrupt -> in this example pin 32
DT (B pin) any microcontroler intput pin with interrupt -> in this example pin 21
SW (button pin) any microcontroler intput pin with interrupt -> in this example pin 25
GND - to microcontroler GND
VCC microcontroler VCC (then set ROTARY_ENCODER_VCC_PIN -1)
***OR in case VCC pin is not free you can cheat and connect:***
VCC any microcontroler output pin - but set also ROTARY_ENCODER_VCC_PIN 25
in this example pin 25
*/
#define ROTARY_ENCODER_A_PIN 32
#define ROTARY_ENCODER_B_PIN 21
#define ROTARY_ENCODER_BUTTON_PIN 25
#define ROTARY_ENCODER_VCC_PIN -1 /* 27 put -1 of Rotary encoder Vcc is connected directly to 3,3V; else you can use declared output pin for powering rotary encoder */

//depending on your encoder - try 1,2 or 4 to get expected behaviour
//#define ROTARY_ENCODER_STEPS 1
//#define ROTARY_ENCODER_STEPS 2
#define ROTARY_ENCODER_STEPS 4

//instead of changing here, rather change numbers above
AiEsp32RotaryEncoder rotaryEncoder = AiEsp32RotaryEncoder(ROTARY_ENCODER_A_PIN, ROTARY_ENCODER_B_PIN, ROTARY_ENCODER_BUTTON_PIN, ROTARY_ENCODER_VCC_PIN, ROTARY_ENCODER_STEPS);

void rotary_onButtonClick()
{
static unsigned long lastTimePressed = 0;
if (millis() - lastTimePressed < 500)
return; //ignore multiple press in that time milliseconds
lastTimePressed = millis();

unsigned long acceletation = rotaryEncoder.getAcceleration() + 50;
if (acceletation > 400)
acceletation = 0;
rotaryEncoder.setAcceleration(acceletation);

Serial.print("new acceleration is ");
Serial.println(acceletation);
Serial.print("Try to set value: ");
Serial.println(random(-999, 999));
Serial.println("Set as fast as you can. If it is too hard or you suceeded, press the button again.");
}

void rotary_loop()
{
if (rotaryEncoder.encoderChanged())
{
Serial.print("Value: ");
Serial.println(rotaryEncoder.readEncoder());
}
}

void setup()
{
Serial.begin(115200);
rotaryEncoder.begin();

rotaryEncoder.setup(
[] { rotaryEncoder.readEncoder_ISR(); },
[] { rotary_onButtonClick(); });
bool circleValues = false;
rotaryEncoder.setBoundaries(-1000, 1000, circleValues); //minValue, maxValue, circleValues true|false (when max go to min and vice versa)
rotaryEncoder.disableAcceleration(); //acceleration is now enabled by default - disable if you dont need it
Serial.println("Ready");
Serial.print("Try to set value: ");
Serial.println(752);
Serial.println("If it is too hard press the button.");
}

void loop()
{
rotary_loop();
delay(50);
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ VCC any microcontroler output pin - but set also ROTARY_ENCOD
//instead of changing here, rather change numbers above
AiEsp32RotaryEncoder rotaryEncoder = AiEsp32RotaryEncoder(ROTARY_ENCODER_A_PIN, ROTARY_ENCODER_B_PIN, ROTARY_ENCODER_BUTTON_PIN, ROTARY_ENCODER_VCC_PIN, ROTARY_ENCODER_STEPS);

int test_limits = 2;
int16_t test_limits = 2;

void rotary_onButtonClick()
{
Expand All @@ -40,10 +40,16 @@ void rotary_onButtonClick()
lastTimePressed = millis();
//rotaryEncoder.reset();
//rotaryEncoder.disable();
/*rotaryEncoder.setBoundaries(-test_limits, test_limits, false);
test_limits *= 2;*/
Serial.print("button pressed at ");
Serial.println(millis());
rotaryEncoder.setBoundaries(-test_limits, test_limits, false);
Serial.print("new boundaries are between minimumn value ");
Serial.print(-test_limits);
Serial.print(" and maximum value");
Serial.println(-test_limits);
rotaryEncoder.reset();

if (test_limits >= 2048)
test_limits = 2;
test_limits *= 2;
}

void rotary_loop()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#include "AiEsp32RotaryEncoder.h"
#define ROTARY_ENCODER_A_PIN 32
#define ROTARY_ENCODER_B_PIN 21
#define ROTARY_ENCODER_BUTTON_PIN 25
#define ROTARY_ENCODER_STEPS 4
AiEsp32RotaryEncoder rotaryEncoder = AiEsp32RotaryEncoder(ROTARY_ENCODER_A_PIN, ROTARY_ENCODER_B_PIN, ROTARY_ENCODER_BUTTON_PIN, -1, ROTARY_ENCODER_STEPS);

void rotary_onButtonClick()
{
Serial.println("button pressed");
}

void setup()
{
Serial.begin(115200);
rotaryEncoder.begin();
rotaryEncoder.setup(
[] { rotaryEncoder.readEncoder_ISR(); },
[] { rotary_onButtonClick(); });
rotaryEncoder.setBoundaries(0, 1000, false); //minValue, maxValue, circleValues true|false (when max go to min and vice versa)
rotaryEncoder.setAcceleration(250);
}

void loop()
{
if (rotaryEncoder.encoderChanged())
{
Serial.println(rotaryEncoder.readEncoder());
}
}
54 changes: 54 additions & 0 deletions examples/FM-radio-tuner/FM-radio-tuner.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#include "AiEsp32RotaryEncoder.h"
#define ROTARY_ENCODER_A_PIN 32
#define ROTARY_ENCODER_B_PIN 21
#define ROTARY_ENCODER_BUTTON_PIN 25
#define ROTARY_ENCODER_STEPS 4
AiEsp32RotaryEncoder rotaryEncoder = AiEsp32RotaryEncoder(ROTARY_ENCODER_A_PIN, ROTARY_ENCODER_B_PIN, ROTARY_ENCODER_BUTTON_PIN, -1, ROTARY_ENCODER_STEPS);

/*
FM radio tunner is suposed to set frequency between 88.0 MHz and 104.0 MHz by 0.1MHz steps
Rotary encoder works with integers so we will map 88.0 to 166 and then divide by 10 to get 0.1 steps
frequency = rotaryValue / 2;
*/

float getFrequency()
{
return (float)rotaryEncoder.readEncoder() / 10.0;
}

void rotary_onButtonClick()
{
static unsigned long lastTimePressed = 0;
if (millis() - lastTimePressed < 200)
return;
lastTimePressed = millis();

Serial.print("Radio station set to ");
Serial.print(getFrequency());
Serial.println(" MHz ");
}

void setup()
{
Serial.begin(115200);
rotaryEncoder.begin();
rotaryEncoder.setup(
[] { rotaryEncoder.readEncoder_ISR(); },
[] { rotary_onButtonClick(); });
rotaryEncoder.setBoundaries(88 * 10, 104 * 10, true); //minValue, maxValue, circleValues true|false (when max go to min and vice versa)
rotaryEncoder.setAcceleration(50);
rotaryEncoder.setEncoderValue(92.1 * 10); //set default to 92.1 MHz
Serial.println("FM Radio");
Serial.print("Radio station initially set to ");
Serial.print(getFrequency());
Serial.println(" MHz. Tune to some other station like 103.2... and press button ");
}

void loop()
{
if (rotaryEncoder.encoderChanged())
{
Serial.print(getFrequency(), 1);
Serial.println(" MHz ");
}
}
Loading

0 comments on commit 68e0deb

Please sign in to comment.