Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

can you do the following... Change your program to decrement encoderPosition eve

ID: 3589091 • Letter: C

Question

can you do the following...

Change your program to decrement encoderPosition every time the push-button is pressed.

unsigned long ButtonTimer;

long encoderPosition;

int A;

int B;

#include <LiquidCrystal.h>

LiquidCrystal LcdDriver(11, 9, 5, 6, 7, 8 );

void MonitorA()

{

if (A == B) {

encoderPosition++;

}

else

{

encoderPosition--;

}

}

void MonitorB() {

if (A == B)

{

encoderPosition--;

}

else

{

encoderPosition++;

}

}

//SetUP

void setup() {

LcdDriver.clear();

LcdDriver.begin(16, 2);

LcdDriver.setCursor(0, 0);

ButtonTimer = millis();

pinMode(2, INPUT);

pinMode(3, INPUT);

attachInterrupt ( digitalPinToInterrupt(2), MonitorA , CHANGE);

attachInterrupt (digitalPinToInterrupt(3), MonitorB , CHANGE);

}

//Loop

void loop() {

A = digitalRead(2);

B = digitalRead(3);

if (millis() - ButtonTimer >= 100) {

LcdDriver.clear();

LcdDriver.print(encoderPosition);

ButtonTimer += 100;

}

}

Explanation / Answer

unsigned long ButtonTimer;
long encoderPosition;
int A;
int B;
#include <LiquidCrystal.h>
LiquidCrystal LcdDriver(11, 9, 5, 6, 7, 8 );
void MonitorA()
{
if (A == B) {
encoderPosition++;
}
else
{
encoderPosition--;
}
}
void MonitorB() {
if (A == B)
{
encoderPosition--;
}
else
{
encoderPosition++;
}
}
//SetUP
void setup() {
LcdDriver.clear();
LcdDriver.begin(16, 2);
LcdDriver.setCursor(0, 0);
ButtonTimer = millis();
pinMode(2, INPUT);
pinMode(3, INPUT);
attachInterrupt ( digitalPinToInterrupt(2), MonitorA , CHANGE);
attachInterrupt (digitalPinToInterrupt(3), MonitorB , CHANGE);

pinMode(4, INPUT); //connect the button to pin 4
}
//Loop
void loop() {
A = digitalRead(2);
B = digitalRead(3);

int button = digitalRead(4);

if (button == 1) {
encoderPosition--;
}
if (millis() - ButtonTimer >= 100) {
LcdDriver.clear();
LcdDriver.print(encoderPosition);
ButtonTimer += 100;
}
}

---------------------------------------------------------------------------------------------------------

This code calls MonitorA function that decreases the encoder position if either A or B is pressed. If both are pressed or none are pressed encoder position increases (property of monitor A)

unsigned long ButtonTimer;
long encoderPosition;
int A;
int B;
#include <LiquidCrystal.h>
LiquidCrystal LcdDriver(11, 9, 5, 6, 7, 8 );
void MonitorA()
{
if (A == B) {
encoderPosition++;
}
else
{
encoderPosition--;
}
}
void MonitorB() {
if (A == B)
{
encoderPosition--;
}
else
{
encoderPosition++;
}
}
//SetUP
void setup() {
LcdDriver.clear();
LcdDriver.begin(16, 2);
LcdDriver.setCursor(0, 0);
ButtonTimer = millis();
pinMode(2, INPUT);
pinMode(3, INPUT);
attachInterrupt ( digitalPinToInterrupt(2), MonitorA , CHANGE);
attachInterrupt (digitalPinToInterrupt(3), MonitorB , CHANGE);

pinMode(4, INPUT); //connect the button to pin 4
}
//Loop
void loop() {
A = digitalRead(2);
B = digitalRead(3);

MonitorA();

if (millis() - ButtonTimer >= 100) {
LcdDriver.clear();
LcdDriver.print(encoderPosition);
ButtonTimer += 100;
}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote