Grabs wrong input pin

What's going on here is I combined two working sketches.

One sketch put out a variable duty cycle dependent on A0v.

The other put out constant duty cycle on pins 4, 5, and 6. They each had a v threshold to start sequentially.

THE problem is 4, 5, and 6 are outputing even when voltages are applied to A0.

For simplification it should be:
Sketch 1, A0
Sketch 2, A1

#define INPUT_PIN_A0 A0
#define INPUT_PIN_A1 A1
#define OUTPUT_PIN_PWM 3
#define PIN4 4
#define PIN5 5
#define PIN6 6

// Voltage thresholds for Sketch 2
#define VOLTAGE_THRESHOLD_PIN4 1.7
#define VOLTAGE_THRESHOLD_PIN5 2.6
#define VOLTAGE_THRESHOLD_PIN6 3.5

// Analog reference voltage (for Arduino Uno, Nano, etc., it's typically 5V)
#define REF_VOLTAGE 5.0

// Frequency and period for Sketch 2
#define FREQUENCY 100
#define PERIOD (1000000L / FREQUENCY) // Period in microseconds

void setup() {
  // Setup for Sketch 1
  Serial.begin(9600); // Initialize serial communication at 9600 bits per second
  pinMode(OUTPUT_PIN_PWM, OUTPUT); // Configure the OUTPUT_PIN as an output

  // Setup for Sketch 2
  pinMode(PIN4, OUTPUT);
  pinMode(PIN5, OUTPUT);
  pinMode(PIN6, OUTPUT);
  pinMode(INPUT_PIN_A1, INPUT);
}

void loop() {
  // Loop for Sketch 1
  int sensorValue = analogRead(INPUT_PIN_A0); // Read the input on analog pin A0
  float voltage = sensorValue * (REF_VOLTAGE / 1023.0); // Convert the analog reading to a voltage
  int dutyCycle;
  if (voltage >= 2.5) {
    dutyCycle = 255; // 100% duty cycle if voltage is 2.5V or above
  } else {
    // Linear interpolation between 0.35V (20% duty cycle) and 2.5V (100% duty cycle)
    float slope = (255 - 51) / (2.5 - 0.35);
    dutyCycle = (int)(51 + slope * (voltage - 0.35));
  }
  analogWrite(OUTPUT_PIN_PWM, dutyCycle); // Output the PWM signal

  Serial.print("Voltage: ");
  Serial.print(voltage);
  Serial.print(" V, Duty Cycle: ");
  Serial.print(dutyCycle * 100 / 255);
  Serial.println("%");

  // Loop for Sketch 2
  unsigned long currentTime = micros();
  static unsigned long lastToggleTime[3] = {0, 0, 0}; // Separate last toggle time for each pin
  float voltageA1 = analogRead(INPUT_PIN_A1) * (REF_VOLTAGE / 1023.0); // Convert analog reading to voltage

  // Toggle pins based on voltage and ensure 100Hz frequency
  if (currentTime - lastToggleTime[0] >= PERIOD) {
    lastToggleTime[0] = currentTime;
    if (voltageA1 > VOLTAGE_THRESHOLD_PIN4) {
      digitalWrite(PIN4, !digitalRead(PIN4));
    } else {
      digitalWrite(PIN4, LOW);
    }
  }

  if (currentTime - lastToggleTime[1] >= PERIOD) {
    lastToggleTime[1] = currentTime;
    if (voltageA1 > VOLTAGE_THRESHOLD_PIN5) {
      digitalWrite(PIN5, !digitalRead(PIN5));
    } else {
      digitalWrite(PIN5, LOW);
    }
  }

  if (currentTime - lastToggleTime[2] >= PERIOD) {
    lastToggleTime[2] = currentTime;
    if (voltageA1 > VOLTAGE_THRESHOLD_PIN6) {
      digitalWrite(PIN6, !digitalRead(PIN6));
    } else {
      digitalWrite(PIN6, LOW);
    }
  }

  // Delay to avoid spamming the serial monitor for Sketch 1
  delay(1);
}

Any clues?
(I don't need serial monitor. I want to delete those parts.)

Where to connect the A1 when the parasitic 4,5,6 outputs are happened?

Could we know which Arduino you are using?

I tried it on 3 different Nanos.
BTW Pin 3 outputs correctly; It properly, only reads A0 pin.

Yeah, I have it to AO, then A1 from bench power source.
Only three wires, A0 or A1, grn, and 4,5 or6 with oscope.
Oscope and power supply share ground.

But not sur if that answers your question.

Exactly. That's what I thought. But on 3 nanos, for 4, 5, and 6, It outputs exactly the same when variable input is put on A0 or A1.

Looks fine in Wokwi, so yeah, wiring or crosstalk or user error.

Wow, I like the way you did that!
Thanks. I will use that in the future.
So the code is. Good.
Unbelievable. I'll get to the bottom of this.

You know what. That might be it.
Why didn't I think of that, Ive seen that before.

You can cut and past back and forth between the IDE and Wokwi. Both have useful editing features - IDE has ctrl-T, Wokwi can change ALL references to a variable name at the same time, etc. etc.
If we could have the best features of both...I bet it would look like some other tool already out there.

1 Like

Maybe you didn't learn electronics in a city with a dozen super-power AM stations and a dozen more TV transmitters filling the airwaves, then?

1 Like

So can the IDE

How, haven't tripped over it. Or are you talking IDE 2.x?
nm, you're talking about find/replace, I suppose. I like the inplace version in Wokwi.

You can do it in IDE 1.x and 2.x

1.x has a checkbox to search all tabs
image

2.x hides it behind the magnifying glass icon
image

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.