Go Down

Topic: Arduino giving weird voltages (Read 646 times) previous topic - next topic


Jun 22, 2012, 01:42 pm Last Edit: Jun 22, 2012, 01:46 pm by Pebbles Reason: 1

I am trying to get an set of valves to switch automatically using the Arduino and some MOSFETs.

I have the following code:
Code: [Select]
const int valvePin =  2;
const int valvePin2 =  4;

// Variables will change:
int valveState = LOW;
int valveState2 = LOW;
long previousMillis = 0;
long valveMillis2 = 2000;

long interval = 6000;
long interval2 = 8000;

void setup() {
 // set the digital pin as output:
 pinMode(valvePin, OUTPUT);
 pinMode(valvePin2, OUTPUT);  

void loop()
 unsigned long currentMillis = millis();

 if(currentMillis - previousMillis > interval) {

   previousMillis = currentMillis;  

   if (valveState == LOW)
     valveState = HIGH;
     valveState = LOW;

   digitalWrite(valvePin, valveState);

 if(currentMillis - previousMillis - valveMillis2 > interval2) {

   if (valveState2 == LOW)
     valveState2 = HIGH;
     valveState2 = LOW;

   digitalWrite(valvePin2, valveState2);

What happens is when Valvepin goes HIGH, i get 5V on Valvepin, but I read 2,5V on Valvepin2.
When Valvepin2 goes HIGH then both are 5V. When Valvepin goes down, Valvepin2 drops to 2,5V again.

Im not sure if the cause is in my code or in my circuit.

Im not using any resistors in the circuit only the MOSFETs.

Both Valvepin and Valvepin2 are each connected to a separate MOSFET gate and are connected to PSU ground and Arduino ground.

Any ideas?

PS: This even happens without anything connect, just measuring between pin4 and gnd shows the 2,5V


Code: [Select]
if(currentMillis - previousMillis > interval) {

    previousMillis = currentMillis;    // <<<<-------- Change in 6 sec

    if (valveState == LOW)
      valveState = HIGH;
      valveState = LOW;

    digitalWrite(valvePin, valveState);

  if(currentMillis - previousMillis - valveMillis2 > interval2) {  //<<<<<---- using previousMillis that may be overwrittent

No wander, there is a conflict two "if" against one previousMillis.

Go Up

Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

via Egeo 16
Torino, 10131