0

Heyo everyone !

I'm in a project where I need to send a signal if I detect vaccuum. I'm sending the signal on PIN 9 but I soon realized that I was only getting 1V.

Tought I was alternating between signal/no_signal but the log provided me with constant and consistent values.

Then I started doubting about my board so I made PIN 10 always output. Under the same load as PIN 9 (I switch the cable connecting from PIN 9 to the circuit, to PIN 10 to the circuit ), I get a good 5V. Thus, I exclude any load problem.

Regarding the PIN output issue, you can all see that it's not missing.

My last hypothesis is that there's some sort of sorcery with my code that I don't know and couln't find on the internet...

Any idea on what might be going on ?

// Constant
const int BL1 = 9;// BINARY LED 1 pin
const int TEST = 10;// BINARY LED 1 pin
const int AP = A0;// ANALOG PRESSOSTAT pin
const int P = 85;// TRIGGER PRESSURE value


// Variables
int LS1 = LOW;  // LED 1 STATE 
float RPV = 1; // Raw Pressostat Voltage (0-1024)
float PV = 1; // Pressostat Voltage (V)
float PDP = 20; // Pressostat Deduced Pressure (kPa)


// Initialization of pins at startup
void setup() {
  pinMode(AP, INPUT); // ANALOG PRESSOSTAT pin set as INPUT
  pinMode(BL1, OUTPUT);// BINARY LED 1 pin set as OUTPUT
  pinMode(TEST, OUTPUT);// BINARY LED 1 pin set as OUTPUT
  pinMode(LED_BUILTIN, OUTPUT);// LED_BUILTIN pin set as OUTPUT
  Serial.begin(9600);
}

void loop() {
    digitalWrite(TEST, HIGH);
    RPV = analogRead(AP);
    PV = (RPV/1024)*5;
    PDP = (-1)*(PV-1)*(-101/4);
    Serial.print("Pression: ");
    Serial.print(PDP);
    if ( PDP > P ){
      int LS1 = HIGH;  // LED 1 STATE   
      digitalWrite(BL1, HIGH);
      digitalWrite(LED_BUILTIN, HIGH);
      Serial.print(" | LED: 1");
      }
    else {
      int LS1 = LOW;  // LED 1 STATE 
      digitalWrite(BL1, LOW);
      digitalWrite(LED_BUILTIN, LOW);
      Serial.print(" | LED: 0"); 
    }
    digitalWrite(BL1, LS1);
    Serial.print(" | Trigger: ");
    Serial.println(P);

  }
8
  • What device is connected to the output pin? How much current does it draw from the pin?
    – chrisl
    May 17 at 10:15
  • How are you measuring the voltage on pin 9 ? A digital multimeter will tend to integrate and give you an average reading so say 5 volts at a 20% duty cycle will be measured as 1 volt. An oscilloscope, on the other hand, will show a true voltage for the complete waveform.
    – 6v6gt
    May 17 at 10:16
  • @6v6gt That's what I (think I) eliminated with my second paragraph. Due to the size of the entiere project (it's only a small part of it), I can't use an oscilloscope...
    – O'Schell
    May 17 at 11:51
  • I don't know but the "always on" pin can hold it... So that's not the issue
    – O'Schell
    May 17 at 11:53
  • 1
    Local variable declarations -- See @kiiv's answer: arduino.stackexchange.com/a/93195/6628
    – Dave X
    May 17 at 16:22

2 Answers 2

3

I'll reduce it a little

void loop() {
    // ... doesn't matter
    if ( PDP > P ){
      int LS1 = HIGH;            // shadows global variable (creates local variable that won't exist after })
      digitalWrite(BL1, HIGH);
    } else {
      int LS1 = LOW;             // again shadows global variable
      digitalWrite(BL1, LOW);
    }
    digitalWrite(BL1, LS1);      // LS1 is still LOW

  }

Mistery solved

2
  • Yep, the local variables don't persist outside their scope {}, so the global variable with the same name remains unchanged outside of those scopes. It could seem like PWM because the internal variables could toggle the pin.
    – Dave X
    May 17 at 16:18
  • Oh god, how didn't I realized that ?! Thank you for you enlightening answer ! That's why even my own answer (which was to change to a non PWM pin) revealed itself to not work...
    – O'Schell
    May 22 at 12:26
0

Here's a working script (I went back to PWM pin, if that matter at all)

void loop() {
  ...// unrelated code
  if (PDP > P) {
    LS1 = HIGH;                               // LED 1 HIGH
    digitalWrite(BL1, LS1);
    digitalWrite(LED_BUILTIN, LS1);
    Serial.print(LS1);
  }
  else {
    LS1 = LOW;                                // LED 1 STATE
    digitalWrite(BL1, LS1);
    digitalWrite(LED_BUILTIN, LS1);
    Serial.print(LS1);
  }
  ...// more unrelated code
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.