This week's brief:
Group assignment:
Rotary encoder is a a type of position sensor that translates the rotational movement of a shaft into an electrical signal, providing feedback about position, speed, and direction.
It is
So if we count the steps each time the signal changes, from High to Low or from Low to High, we can notice at that
time the two output signals have opposite values. Vice versa, if the encoder is rotating counter clockwise, the
output signals have equal values. So considering this, we can easily program our controller to read
the encoder position and the rotation direction.
Capacitive sensors detect objects by measuring changes in capacitance, which occurs when an object enters the sensor's electrostatic field, without needing physical contact
A basic capacitor takes time to fully charge when subjected to a specific voltage and can discharge when the
voltage source is removed and the capacitor is connected to a sink. The duration of this charge and discharge
process remains relatively constant when the circuit parameters are unchanged.
However, this duration varies when the circuit's capacitance changes. This principle forms the basis of capacitive
touch screens.
When a human finger touches the screen, it introduces an additional capacitor into the circuit, increasing the
overall capacitance. Human skin acts as a dielectric material, altering the circuit's charging and discharging
times.
This change in charge-discharge duration is used to detect user interactions.
A dedicated microcontroller typically manages this process, charging the capacitive screen and monitoring changes
in the circuit's charge-discharge times.
When deviations from the norm are detected, the microcontroller signals the main controller to register the user's
touch.
(*To have a deeper understanding about how this works, you can refer to the materials I also took notes from which could be found at the end of this page*)


#define ENCODER_PIN_A 14 // Pin connected to encoder A output
#define ENCODER_PIN_B 15 // Pin connected to encoder B output
#define ENCODER_BUTTON_PIN 13
#define LED_RED_PIN 12 // the number of the LED pin
volatile int encoderCount = 0;
bool ledState = false; // the current state of the LED
int buttonState; // the current reading from the input pin
bool lastButtonState = false; // the previous reading from the input pin
// the following variables are long because the time, measured in milliseconds,
// will quickly become a bigger number than can be stored in an int.
unsigned long lastDebounceTime = 0; // the last time the output pin was toggled
unsigned long debounceDelay = 50; // the debounce time; increase if the output flickers
void setup() {
pinMode(ENCODER_BUTTON_PIN, INPUT_PULLUP);
pinMode(LED_RED_PIN, OUTPUT);
pinMode(ENCODER_PIN_A, INPUT);
pinMode(ENCODER_PIN_B, INPUT);
attachInterrupt(digitalPinToInterrupt(ENCODER_PIN_A), encoderISR, CHANGE);
Serial.begin(9600);
}
void loop() {
// Print the encoder count
Serial.print(" Encoder Count: ");
Serial.println(encoderCount);
delay(100); // Update every second
// read the state of the switch into a local variable:
int reading = digitalRead(ENCODER_BUTTON_PIN);
// check to see if you just pressed the button
// (i.e., the input went from LOW to HIGH), and you've waited long enough
// since the last press to ignore any noise:
// If the switch changed, due to noise or pressing:
if (reading != lastButtonState) {
// reset the debouncing timer
lastDebounceTime = millis();
}
if ((millis() - lastDebounceTime) > debounceDelay) {
// whatever the reading is at, it's been there for longer than the debounce
// delay, so take it as the actual current state:
// if the button state has changed:
if (reading != buttonState) {
buttonState = reading;
// only toggle the LED if the new button state is HIGH
if (buttonState == HIGH) {
encoderCount = 0;
ledState = !ledState;
}
}
}
// set the LED:
digitalWrite(LED_RED_PIN, ledState);
// save the reading. Next time through the loop, it'll be the lastButtonState:
lastButtonState = reading;
}
void encoderISR() {
// Read the state of pin B
if (digitalRead(ENCODER_PIN_A) == digitalRead(ENCODER_PIN_B)) {
encoderCount++;
} else {
encoderCount--;
}
// Serial.print(" Encoder Count: ");
// Serial.println(encoderCount);
}
#define sense_pin PIN_PA1 // sense pin
#define settle 100 // settling time
#define samples 1000 // number of samples to accumulate
void setup() {
Serial.begin(115200); // start serial
analogSampleDuration(10); // set ADC sampling rate
analogReadResolution(10); // set ADC resolution
}
void loop() {
int32_t count;
count = 0;
noInterrupts(); // disable interrupts while measuring
for (int i = 0; i < samples; ++i) {
pinMode(sense_pin,OUTPUT); // set sense pin to output
digitalWriteFast(sense_pin,HIGH); // charge up
delayMicroseconds(settle); //settle
pinMode(sense_pin,INPUT); // set sense pin to input
count += analogRead(sense_pin); // read discharge
}
interrupts(); // enable interrupts after measuring
Serial.println(count); // send count
Serial.flush(); // finish communicating before measuring
}
#define sense_pin1 PIN_PA7 // sense pin
#define sense_pin2 PIN_PB5
#define sense_pin3 PIN_PB4
#define sense_pin4 PIN_PA6
#define settle 100 // settling time
#define samples 1000 // number of samples to accumulate
int d[4] = { 0, 0, 0, 0 }; // array for cheching the directional change
int level = 0; // value to be increased or decreased according to the direction
void setup() {
Serial.begin(115200); // start serial
analogSampleDuration(10); // set ADC sampling rate
analogReadResolution(10); // set ADC resolution
}
void loop() {
dial();
}
void dial() {
int32_t count1; // for checking the threshhold value
int32_t count2;
int32_t count3;
int32_t count4;
count1 = 0;
count2 = 0;
count3 = 0;
count4 = 0;
noInterrupts(); // disable interrupts while measuring
for (int i = 0; i < samples; ++i) {
pinMode(sense_pin2, OUTPUT); // set sense pin2 to output
digitalWriteFast(sense_pin2, HIGH); // charge up
pinMode(sense_pin1, OUTPUT); // set sense pin1 to output
digitalWriteFast(sense_pin1, HIGH); // charge up
pinMode(sense_pin3, OUTPUT); // set sense pin3 to output
digitalWriteFast(sense_pin3, HIGH); // charge up
pinMode(sense_pin4, OUTPUT); // set sense pin4 to output
digitalWriteFast(sense_pin4, HIGH); // charge up
delayMicroseconds(settle); //settle
pinMode(sense_pin1, INPUT); // set sense pin1 to input
count1 += analogRead(sense_pin1); // read discharge
pinMode(sense_pin2, INPUT); // set sense pin2 to input
count2 += analogRead(sense_pin2); // read discharge
pinMode(sense_pin3, INPUT); // set sense pin3 to input
count3 += analogRead(sense_pin3); // read discharge
pinMode(sense_pin4, INPUT); // set sense pin4 to input
count4 += analogRead(sense_pin4); // read discharge
}
interrupts(); // enable interrupts after measuring
Serial.print(count1); // send count
Serial.print(" ");
Serial.print(count2);
Serial.print(" ");
Serial.print(count3);
Serial.print(" ");
Serial.println(count4);
if (count1 > 500000) { //for pad1
if (d[3] == 1) {
level++;
} else if (d[1] == 1) {
level--;
}
d[0] = 1;
d[1] = 0;
d[2] = 0;
d[3] = 0;
}
if (count2 > 300000) { // for pad2
if (d[0] == 1) {
level++;
} else if (d[2] == 1) {
level--;
}
d[0] = 0;
d[1] = 1;
d[2] = 0;
d[3] = 0;
}
if (count3 > 300000) { //for pad3
if (d[1] == 1) {
level++;
} else if (d[3] == 1) {
level--;
}
d[0] = 0;
d[1] = 0;
d[2] = 1;
d[3] = 0;
}
if (count4 > 450000) { //for pad4
if (d[2] == 1) {
level++;
} else if (d[0] == 1) {
level--;
}
d[0] = 0;
d[1] = 0;
d[2] = 0;
d[3] = 1;
}
Serial.println(level);
Serial.flush(); // finish communicating before measuring
}
I would like to further write the codes for changing the brightness levels as well as changing the colour of the RGB led using the rotary encoder and the scrollpad
-->About rotary Encoder.
About Capacitive sensing