Pages: [1]   Go Down
Author Topic: Akting a motor and a relais simultaniously  (Read 183 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 1
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi people,

I'm working on a arduino-based espresso-machine.

For this I use a DC-Motor (with a H-bridge) and a solenoid valve (controlled by a relay).

the motor should turn up, open the valve while turning up, close the valve and than turn down.

everything works fine, buuut I'd like that the valve turns on while the motor turns in one direction.
I know this doesn't work with delay and unfortunately I programmed with delay ...

I read that it's possible to change and to work like in the "blink without led"-example.

Unfortunately it's hard to understand and I really don't know how to continue ...

any help?

here is the actual code:

Code:
// pins
int dirPin = 4;
int pwmPin = 10;
int pushButton = 2;
int pressed = digitalRead(pushButton);

// motor up
int powerOnH = 240; // speed of the motor
int durationMovementH = 4000; // time of the rotation
int directionMovementH = HIGH; //direction

// motor down
int powerOnR= 240; // speed of the motor
int durationMovementR = 3785; // ime of the rotation
int directionMovementR = LOW; //direction

int powerOff = 0;

void setup(){
  // pin modes
  pinMode(dirPin,OUTPUT);
  pinMode(pwmPin,OUTPUT);
  pinMode(pushButton,INPUT);
  digitalWrite(pushButton,HIGH);
  pinMode(6,OUTPUT);
}
void loop(){

  // push the button


  int pressed = digitalRead(pushButton);
  if (pressed == HIGH)
  {
    // up
    digitalWrite(dirPin,directionMovementH);

    // start
    analogWrite(pwmPin,powerOnH);

    // time
    delay(durationMovementH);

    // stop
    analogWrite(pwmPin,powerOff);


    //valve
    digitalWrite(6,HIGH);
    delay(2000);
    digitalWrite(6,LOW);


    //down
    digitalWrite(dirPin,directionMovementR);

    // start
    analogWrite(pwmPin,powerOnR);

    // time
    delay(durationMovementR);

    // stop
    analogWrite(pwmPin,powerOff);
  }
}

Moderator edit: CODE TAGS!



« Last Edit: January 27, 2012, 02:28:30 am by AWOL » Logged

Massachusetts, USA
Offline Offline
Tesla Member
***
Karma: 180
Posts: 8103
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

If you want to minimize changes you could split the long delays into several smaller delays.

For example if you wanted to have the valve open for 2 second starting half a second after the motor starts down:

Code:
    digitalWrite(dirPin,directionMovementR);  // Motor direction down
    analogWrite(pwmPin,powerOnR);  // Motor start

    delay(500);  // Run motor for 1/2 second before opening valve

    digitalWrite(6,HIGH);  // Valve open
    delay(2000);  // For two seconds
    digitalWrite(6,LOW);  // and then closed

    delay(durationMovementR - 2500);  // The rest of the motor down time
    analogWrite(pwmPin,powerOff); // Stop the motor
Logged

Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

Pages: [1]   Go Up
Jump to: