Hi All,
This is my first post. I am new to arduino and programming in general.
I am trying to display a number on a seven segment display based on the position of an encoder. As I am only displaying one digit, I am turning on each segment individually for a given "case" as opposed to using a bcd decoder or library to try and simplify things.
The code I have below is a combination of two examples which I have attempted to modify in order to work together.
The intent of the program is that the number displayed on the serial monitor (newPos), should result in the outcome of one of the 4 cases.
The encoder part of the program appears to be working as it displays the correct position in the serial monitor, however the "case" does not seem to update the 7-segment display based on encoder position.
I have confirmed the 7-segment display is wired correctly as I can turn on the segments manually.
Any input or direction would be appreciated.
Thanks
#include <RotaryEncoder.h>
#define ROTARYSTEPS 1
#define ROTARYMIN 1
#define ROTARYMAX 4
int lastPos = -1;
RotaryEncoder encoder(A2, A3);
void setup() {
Serial.begin(57600);
pinMode(39, OUTPUT);
pinMode(41, OUTPUT);
pinMode(43, OUTPUT);
pinMode(45, OUTPUT);
pinMode(47, OUTPUT);
pinMode(49, OUTPUT);
pinMode(51, OUTPUT);
pinMode(53, OUTPUT);
Serial.println("Encoder Position");
encoder.setPosition(1 / ROTARYSTEPS); // start with the value of 1.
}
void loop() {
encoder.tick();
// get the current physical position and calc the logical position
int newPos = encoder.getPosition() * ROTARYSTEPS;
if (newPos < ROTARYMIN) {
encoder.setPosition(ROTARYMIN / ROTARYSTEPS);
newPos = ROTARYMIN;
} else if (newPos > ROTARYMAX) {
encoder.setPosition(ROTARYMAX / ROTARYSTEPS);
newPos = ROTARYMAX;
}
if (lastPos != newPos) {
Serial.print(newPos);
Serial.println();
lastPos = newPos;
}
// read encoder:
int sensorReading = newPos;
// map the sensor range to a range of four options:
int range = map(sensorReading, 1, 2, 3, 4);
// do something different depending on the range value:
switch (range) {
case 0: // your hand is on the sensor
digitalWrite(39, HIGH);
digitalWrite(41, HIGH);
digitalWrite(43, HIGH);
digitalWrite(47, HIGH);
digitalWrite(49, HIGH);
digitalWrite(51, HIGH);
digitalWrite(53, HIGH);
break;
case 1: // your hand is close to the sensor
digitalWrite(39, LOW);
digitalWrite(41, HIGH);
digitalWrite(43, HIGH);
digitalWrite(47, HIGH);
digitalWrite(49, HIGH);
digitalWrite(51, HIGH);
digitalWrite(53, HIGH);
break;
case 2: // your hand is close to the sensor
digitalWrite(39, LOW);
digitalWrite(41, LOW);
digitalWrite(43, HIGH);
digitalWrite(47, HIGH);
digitalWrite(49, HIGH);
digitalWrite(51, HIGH);
digitalWrite(53, HIGH);
break;
case 3: // your hand is close to the sensor
digitalWrite(39, LOW);
digitalWrite(41, LOW);
digitalWrite(43, LOW);
digitalWrite(47, HIGH);
digitalWrite(49, HIGH);
digitalWrite(51, HIGH);
digitalWrite(53, HIGH);
break;
}
delay(1); //
}