I am building a mechanism to output voltage depending on which digital input is currently pressed. I have got 8 Inputs that each need to output a different voltage to a MCP4725. There is always one input active and I change between them ascending / descending.
My Problem: For example: Going from D2 to D3 is very fast, however going from D3 to D2 takes nearly 6 seconds.
My circuit explained:
I have 8 toggle buttons where only 1 can be pressed simultaneously. Those buttons each send ground with 10k Ohms resistance into D2,D3, etc. when turned off. Once the button is turned on +5V gets sent into the digital input
My code so far:
#include <Wire.h>
#include <Adafruit_MCP4725.h>
Adafruit_MCP4725 dac;
const bool debugMode = false;
int testPin = A0;
const int refV = 4124;
float pins[][2] = {
{2, 0.3}, // P
{3, 1.0}, // R
{4, 1.7}, // N
{5, 2.4}, // D
{6, 3.1}, // S
{7, 3.8}, // M
{8, 4.5}, // +
{9, 5.0} // -
};
float currentPin[2];
#define DAC_RESOLUTION 9 // Adjust resolution as needed (8, 7, 6, 5)
void setup() {
Serial.begin(9600);
Wire.begin();
for (int i = 0; i < sizeof(pins); i++) {
pinMode(pins[i][0], INPUT);
}
Serial.println("---- Shifter Online ----");
if (dac.begin(0x60) || dac.begin(0x61) || dac.begin(0x62) || dac.begin(0x63) || dac.begin(0x64) || dac.begin(0x65) || dac.begin(0x66)) {
Serial.println("DAC Initialized");
for (int i = 0; i < 5; i++) {
digitalWrite(LED_BUILTIN, HIGH);
delay(300);
digitalWrite(LED_BUILTIN, LOW);
delay(300);
}
} else {
Serial.println("Error: Failed to Initialize MCP4725.");
for (int i = 0; i < 3; i++) {
digitalWrite(LED_BUILTIN, HIGH);
delay(100);
digitalWrite(LED_BUILTIN, LOW);
delay(100);
}
}
}
void loop() {
for (int i = 0; i < sizeof(pins); i++) {
if (digitalRead(pins[i][0]) == HIGH) {
Serial.println(pins[i][0]);
currentPin[0] = pins[i][0];
currentPin[1] = pins[i][1];
break; // Exit the loop once an input is detected
}
}
float dacValue = currentPin[1] * refV / 5.0;
dac.setVoltage(dacValue, false);
if(debugMode == true) {
if(currentPin[0]) {
Serial.println(currentPin[0]);
Serial.print(" with assigned Voltage ");
Serial.print(currentPin[1] * refV / 5.0);
Serial.println("V");
}
}
}
It is 64 for some reason? Do I need to write it like this: sizeof(pins) / sizeof(pins[0]?
I am pretty new to Arduinos as you can probably tell
Thanks for your help
As you have probably found out, sizeof() returns the number of bytes used by the variable. So if you want to know the number of rows in the array then you divide by the size of one row, ie pins[0], as you suggest
Please excuse me, this is my first question here. Did really not mean to cause anything negative.
I changed it to the previous code.
This is my current code:
#include <Wire.h>
#include <Adafruit_MCP4725.h>
Adafruit_MCP4725 dac;
const bool debugMode = false;
int testPin = A0;
const int refV = 4124;
float pins[][2] = {
{2, 0.3}, // P
{3, 1.0}, // R
{4, 1.7}, // N
{5, 2.4}, // D
{6, 3.1}, // S
{7, 3.8}, // M
{8, 4.5}, // +
{9, 5.0} // -
};
float currentPin[2];
#define DAC_RESOLUTION 9 // Adjust resolution as needed (8, 7, 6, 5)
void setup() {
Serial.begin(9600);
Wire.begin();
for (int i = 0; i < sizeof(pins); i++) {
pinMode(pins[i][0], INPUT);
}
Serial.println("---- Shifter Online ----");
if (dac.begin(0x60) || dac.begin(0x61) || dac.begin(0x62) || dac.begin(0x63) || dac.begin(0x64) || dac.begin(0x65) || dac.begin(0x66)) {
Serial.println("DAC Initialized");
for (int i = 0; i < 5; i++) {
digitalWrite(LED_BUILTIN, HIGH);
delay(300);
digitalWrite(LED_BUILTIN, LOW);
delay(300);
}
} else {
Serial.println("Error: Failed to Initialize MCP4725.");
for (int i = 0; i < 3; i++) {
digitalWrite(LED_BUILTIN, HIGH);
delay(100);
digitalWrite(LED_BUILTIN, LOW);
delay(100);
}
}
}
void loop() {
for (int i = 0; i < sizeof(pins) / sizeof(pins[0]; i++) {
if (digitalRead(pins[i][0]) == HIGH) {
Serial.println(pins[i][0]);
currentPin[0] = pins[i][0];
currentPin[1] = pins[i][1];
float dacValue = currentPin[1] * refV / 5.0;
dac.setVoltage(dacValue, false);
break; // Exit the loop once an input is detected
}
}
if(debugMode == true) {
Serial.println(currentPin[0]);
Serial.print(" with assigned Voltage ");
Serial.print(currentPin[1] * refV / 5.0);
Serial.println("V");
}
}
Please always copy & paste your updated code from the IDE to avoid that kind of mistake.
How is that enforced?
We need to see a diagram explaining that unambiguously. My suspicion at this point is that the Arduino input is floating when the switch is in one position or the other.