Hi everyone!
This is my first post and I'm very new at developing on the Arduino and haven't much experience with C or C++ languages. So please be gentle.
I'm developing a program that makes use of a rotary encoder and the digital outputs. I'm trying to build a system that will supply an analog voltage output via an 8-bit R2R network of resistors that will utilize D2 - D9 on the Arduino Uno. The DAC will supply an analog signal that will (after a few other elements) drive a constant current source.
I cobbled together some code where i use an array of floats to supply all the possible current set points and i was going to trigger a series of IF statements to match the set point and set the appropriate binary bits on D2-D7 for the given current set point. Currently, it is configured to set the current when the button on the encoder is pressed.
The way I'm writing it now seems inefficient so i was looking for guidance. Is there a faster way i could set the digital pins without defining every single current set point for the 8-bit binary sequence? The way it's written now i will have to define the binary output for an additional 30 current set points.
" if (cset == 0.25) { "
digitalWrite(2, HIGH);
digitalWrite(3, HIGH);
digitalWrite(4, HIGH);
digitalWrite(5, HIGH);
digitalWrite(6, HIGH);
digitalWrite(7, HIGH);
digitalWrite(8, HIGH);
digitalWrite(9, HIGH);
}
// Rotary Encoder Inputs
#define CLK 13
#define DT 12
#define SW 11
int counter = 0;
int currentStateCLK;
int lastStateCLK;
String currentDir = "";
unsigned long lastButtonPress = 0;
int csele;
float cset;
float cur_opt[] = {0, 0.25, 0.5, 0.75,
1, 1.25, 1.5, 1.75,
2, 2.25, 2.5, 2.75,
3, 3.25, 3.5, 3.75,
4, 4.25, 4.5, 4.75,
5, 5.25, 5.5, 5.75,
6, 6.25, 6.5, 6.75,
7, 7.25, 7.5, 7.75, 8
};
void setup() {
//SET R2R BITS
pinMode(2, OUTPUT);
pinMode(3, OUTPUT);
pinMode(4, OUTPUT);
pinMode(5, OUTPUT);
pinMode(6, OUTPUT);
pinMode(7, OUTPUT);
pinMode(8, OUTPUT);
pinMode(9, OUTPUT);
// Set encoder pins as inputs
pinMode(CLK, INPUT);
pinMode(DT, INPUT);
pinMode(SW, INPUT_PULLUP);
// Setup Serial Monitor
Serial.begin(9600);
// Read the initial state of CLK
lastStateCLK = digitalRead(CLK);
}
void loop() {
// Read the current state of CLK
currentStateCLK = digitalRead(CLK);
// If last and current state of CLK are different, then pulse occurred
// React to only 1 state change to avoid double count
if (currentStateCLK != lastStateCLK && currentStateCLK == 1) {
// If the DT state is different than the CLK state then
// the encoder is rotating CCW so decrement
if (digitalRead(DT) != currentStateCLK) {
counter ++;
currentDir = "CW";
} else {
// Encoder is rotating CW so increment
counter --;
currentDir = "CCW";
}
csele = counter;
cset = cur_opt[csele];
if (counter < 0) {
counter = 0, cset = 0, csele = 0;
}
if (counter > 31) {
counter = 31, cset = 8.00, csele = 32;
delay (100);
}
Serial.print("Direction: ");
Serial.print(currentDir);
Serial.print(" | Counter: ");
Serial.println(counter);
Serial.print("C_Set:");
Serial.println(cset);
Serial.print("Csele:");
Serial.println(csele);
}
// Remember last CLK state
lastStateCLK = currentStateCLK;
// Read the button state
int btnState = digitalRead(SW);
//Set binary sequence for given current setpoint on button press.
//If we detect LOW signal, button is pressed
if (btnState == LOW) {
//if 50ms have passed since last LOW pulse, it means that the
//button has been pressed, released and pressed again
if (millis() - lastButtonPress > 50) {
Serial.println("Button pressed!");
//Example: R2R input of "11111111"
if (cset == 0.25) {
digitalWrite(2, HIGH);
digitalWrite(3, HIGH);
digitalWrite(4, HIGH);
digitalWrite(5, HIGH);
digitalWrite(6, HIGH);
digitalWrite(7, HIGH);
digitalWrite(8, HIGH);
digitalWrite(9, HIGH);
}
if (cset < 0.25 or cset > 0.25) {
digitalWrite(2, LOW);
digitalWrite(3, LOW);
digitalWrite(4, LOW);
digitalWrite(5, LOW);
digitalWrite(6, LOW);
digitalWrite(7, LOW);
digitalWrite(8, LOW);
digitalWrite(9, LOW);
}
}
// Remember last button press event
lastButtonPress = millis();
}
// Put in a slight delay to help debounce the reading
delay(1);
}