Arduino Nano: Digital Input Reading Time too long

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");
    }
  }
}

for (int i = 0; i < sizeof(pins); i++)

What is the value of sizeof(pins) in this for loop ?

2 Likes

Please provide a schematic.

Is there a reason why you're setting the DAC voltage each time loop() executes, and not only when the active input changes?

How does this work with float values?

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

Absolutely not. Makes perfect sense what you are saying

Honestly, no idea. I removed that line now because it didnt really have a purpose

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

Alright thats good, unfortunately that did not fix the problem :confused:

Please post the sketch i(n a new reply) as it is now after any changes that you have made as a result of comments

I updated the sketch and explained my circuit above. Thanks

You were asked, by a form moderator, to

By doing that you have made a nonsense of any comments made about the original sketch which is very bad manners

1 Like

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");
  }
}
1 Like

Thanks for posting the updated code.

But...

Implies that the code you posted has been uploaded and tested

You seem to have posted something else.

Sorry forgot a bracket there. It is correct in my code though

for (int i = 0; i < sizeof(pins); i++)

Are you sure that you incorporated all of the suggested changes ?

@freerangeclown this change needs to be applied also in setup()

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.

1 Like

Which of the connections depicted here do your buttons follow:
image

With kudos to @LarryD, posted in this thread:Share tips you have come across