Pages: [1]   Go Down
Author Topic: Arduino giving weird voltages  (Read 377 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 22
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello,

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

I have the following code:
Code:
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;
    else
      valveState = LOW;

    digitalWrite(valvePin, valveState);
  }

  if(currentMillis - previousMillis - valveMillis2 > interval2) {

    if (valveState2 == LOW)
      valveState2 = HIGH;
    else
      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
« Last Edit: June 22, 2012, 06:46:25 am by Pebbles » Logged

Montreal
Offline Offline
Faraday Member
**
Karma: 29
Posts: 2590
Per aspera ad astra.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
if(currentMillis - previousMillis > interval) {

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

    if (valveState == LOW)
      valveState = HIGH;
    else
      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.
Logged

Pages: [1]   Go Up
Jump to: