Go Down

Topic: having trouble programming the solenoids (Read 1 time) previous topic - next topic

llumen

Hi again,

The solenoids work! The circuit provided was ok and works!

Now it's the programming that bugs me. I made a patch in max to control the solenoids but it's apparently waaaayy to slow to get it running (don't know why though, it shouldn't be)
I'm trying to program the solenoids in the arduino software.

What I'm trying to do is have a basic duration of a movement of the solenoid (which is just an adaptation of the blinking led thing with solenoid1 == HIGH and delay(24) solenoid1 == LOW and delay (38)) This loop has to be called until the basic duration (t1) is over. t1 is random(0.1-80)/10*60000 and needs to be called again when the last solenoid has finished it's cycle.

solenoid2 needs to begin somewhere during the second half of solenoid1's cycle and solenoid3 needs to begin somewhere in the cycle of solenoid2.

The whole movement of the solenoids doesn't need to start over again as soon as solenoid3 cycle is over but has to wait a random time between 1 and 5 minutes.

I have no id where to start here and would really be thankfull to anyone pointing me in the right direction. I know it's maybe a bit much to ask but I'm getting stressed since I need to have it finished next week.

pieter

bigengineer

While you are in a delay() loop you can't do anything else. You could put the switching of the solenoids in the loop() function. Measure time with millis(), when you are at the right time do something, switch solenoid on or off. Depending on what you want exactly you could use a switch/case statement. So, look up in the reference: millis and switch and see if it is of any use.

llumen

this may be the worst approach ever but is there a way that I can send a value from max to arduino like a specific number that triggers a loop that I already encoded on the board.
In that way I could write some loops in if statements that would control the three solenoids independitly and only have to send 4 diffrent numbers to arduino board which will be faster?

thx

pieter

bigengineer

You can send the numbers over the serial port. Reading and writing over the serial port is something you can find in the examples. But when you are in a delay() loop nothing else happens.

llumen

ok, I'm totally lost now.

the thing is it's not that I need to just turn the solenoid off and on I need to make them on-off-ing really fast. I looked at the millis() and switch case things and think this is the way to go. But I still have delays in my program because I don't understand how to do this diffrently. I know it won't work but I hope that by providing the code someone could see what exactly it is that I'm trying to do, I still can't have the speed I need from within max, which is very strange to say the least but that's another story.

I want to send a number from within max to arduino to adress the diffrent cases but also don't know how to change the var in the code from within max. I really don't find the reference or examples of great use, maybe I'm just too stupid?

ANyway hope someone can help me out here?

Thx

pieter

// I want to implement variable rates also that it isn't to mechanical but that's something else I don't understand
// Solenoid4 need to just have short HIGH and random long LOW hits from time to time,
int var;

int Solenoid1 = 13;                 // LED connected to digital pin 13
int Solenoid2 = 12;
int Solenoid3 = 11;

int Solenoid4 = 10;

int time;
long randNumber;
int time4;
long randNumber4;

void setup(){
  var==0; // how can I change this from within max?
}


void loop(){
  switch (1) {
   case 1:
     //solenoid 1 starts to vibrate and hits a large drum
       digitalWrite(Solenoid1, HIGH);   // sets the LED on
       delay(24);                       // waits for a while
       digitalWrite(Solenoid1, LOW);    // sets the LED off
       delay(38);
       break;

   case 2:
     //solenoid 1 and 2 vibrate after some time
       digitalWrite(Solenoid1,HIGH);
       digitalWrite(Solenoid2,HIGH);
       delay(24);
       digitalWrite(Solenoid1,LOW);
       digitalWrite(Solenoid2,LOW);
       delay(38);
     break;
     
    case 3:
    //solenoid 1, 2 and 3 vibrate
   
       digitalWrite(Solenoid1,HIGH);
       digitalWrite(Solenoid2,HIGH);
       digitalWrite(Solenoid3, HIGH);
       delay(24);
       digitalWrite(Solenoid1,HIGH);
       digitalWrite(Solenoid2,HIGH);
       digitalWrite(Solenoid3,LOW);
       delay(38);
       break;
       
    case 4:
    // solenoid 2 and 3 vibrate
       digitalWrite(Solenoid2,HIGH);
       digitalWrite(Solenoid3,HIGH);
       delay(24);
       digitalWrite(Solenoid2,LOW);
       digitalWrite(Solenoid3,LOW);
       delay(38);
       break;
       
    case 5:
    // solenoid3 vibrates
       digitalWrite(Solenoid3, HIGH);
       delay(24);
       digitalWrite(Solenoid3,LOW);
       delay(28);
       break;
   
   
   default:
     // if nothing else matches, do the default
      break;
  }
}

Go Up