Pot value two different outs without delays

Hi.

I am working on a project that requires a single line of product boxes irregularly spaced to be equally spaced then diverted left and right sequentially.

So far I have the spacing part sorted as the boxes come down a conveyor into a tunnel with a pnuematic ram on the top that goes up and down depending on pot value mapped to avoid it firing 100s time a second.

so no i need to work out how to move another ram left/right via a 5 port valve on left off right.

I have mapped the pot value and times the output value by 10 for timing issues.
now for the divert arm i assumed multiplying by 20 would move the arm once for every two times the spacing ram operates?

code below
I cant get anything to happen on the divert output.

it compiles so i dont know what im missing. thanks

//use potentiometer output to set spacingram rate
int potentiometer = A1;
int outputValue = 0;
int potRead = 0;
int spacingram = 8;
int divert = 9;
int isOn = 1;
long now = 0;

void setup(){
  pinMode(spacingram,OUTPUT);
  pinMode(divert,OUTPUT);
  pinMode(potentiometer,INPUT);
  digitalWrite(spacingram,HIGH);
  
}

void loop(){
  potRead = analogRead(potentiometer);
  outputValue = map(potRead, 0, 1023, 20, 150);
  if(millis() > now +  outputValue *10){
    digitalWrite(spacingram,isOn);
    isOn = 1 - isOn;
    now = millis();
    
  potRead = analogRead(potentiometer);
  outputValue = map(potRead, 0, 1023, 20, 150);
     if(millis() > now +  outputValue *20){
    digitalWrite(divert,isOn);
    isOn = 1 - isOn;
    now = millis();
  }
  }
}

your second if() statement is inside the {} block of the first if() statement - should it be?
possibly use two different counters for the two if()s?
put some print statements in the code so you can see the flow of control?

Your problem is difficult to understand as we do not know your system/implementation, and everything is explained, in short, quick statements.

Could you explain in more detail the circuit/system flow and then the problem with perhaps a picture for clear understanding?

horace:
your second if() statement is inside the {} block of the first if() statement - should it be?
possibly use two different counters for the two if()s?
put some print statements in the code so you can see the flow of control?

Hi Thanks for your reply I need the two to work simulaneously not dependant on each other. Does that make sense? Its like the arduino has operating two outputs from one input but different math used to vary output times.

Any clearer? O.0

  1. I would only read and map the pot once.

  2. You need a separate state variable isOn for each ram.

EDIT:
3) You also need separate variables for "now".

craskie1805:
Hi Thanks for your reply I need the two to work simulaneously not dependant on each other. Does that make sense? Its like the arduino has operating two outputs from one input but different math used to vary output times.

Any clearer? O.0

Yes, but you need to explain what's happening right now with your code, and what do you expect? Don't talk in terms of arm/ram etc, talk in terms of what values of digital outputs(high/low at what times?) do you expect for the pot input.

Your loop does the same thing twice, which I would call "bad practice". You should not use timing to activate the divert ram, it can be done much simpler.

Try to consider this:

//Add these to your other global declarations
unsigned long lastSpacing = 0; //Add this instead of "now" - is has to be unsigned
unsigned long loopMillis; //Se comment below
int divertOn = 0; //State of the divert ram


void loop() {
  //potRead: remove this variable, you do not need it
  outputValue = map(analogRead(potentiometer), 0, 1023, 20, 150);

  //Store the millis for the loop in order to ensure better timing
  //since some milliseconds may pass between calls to millis()
  //causing minor inaccuracy
  loopMillis = millis(); 
  
  
  if(loopMillis > lastSpacing + (outputValue * 10)) {
    digitalWrite(spacingram, isOn);
    isOn ^= 1; //This means xor assign, it will flip bit 1 and is faster than subtracting
    lastSpacing = loopMillis;
    if (isOn == 0) {
      digitalWrite(divert, divertOn);
      divertOn ^= 1;
    }
  }
}

This code should change the position of the diverting ram for each time the spacing arm completes a cycle:

  • spacing arm down
  • spacing arm up
  • divert ram left
  • spacing arm down
  • spacing arm up
  • divert ram right

Danois90:
Your loop does the same thing twice, which I would call "bad practice". You should not use timing to activate the divert ram, it can be done much simpler.

Try to consider this:

//Add these to your other global declarations

unsigned long lastSpacing = 0; //Add this instead of "now" - is has to be unsigned
unsigned long loopMillis; //Se comment below
int divertOn = 0; //State of the divert ram

void loop() {
  //potRead: remove this variable, you do not need it
  outputValue = map(analogRead(potentiometer), 0, 1023, 20, 150);

//Store the millis for the loop in order to ensure better timing
  //since some milliseconds may pass between calls to millis()
  //causing minor inaccuracy
  loopMillis = millis();
 
 
  if(loopMillis > lastSpacing + (outputValue * 10)) {
    digitalWrite(spacingram, isOn);
    isOn ^= 1; //This means xor assign, it will flip bit 1 and is faster than subtracting
    lastSpacing = loopMillis;
    if (isOn == 0) {
      digitalWrite(divert, divertOn);
      divertOn ^= 1;
    }
  }
}




This code should change the position of the diverting ram for each time the spacing arm completes a cycle:

- spacing arm down
- spacing arm up
- divert ram left
- spacing arm down
- spacing arm up
- divert ram right

Danios90

thank you this works to a closer degree than i got :slight_smile: i will connect it all up tomorrow and see how it works,

I will try to explain clearly for others to get understand my problem.

Scope: regularly space and divide 700g plastic boxes of peas before they go into a spiral freezer.

From the start.

Peas are weighed by operators on a moving line and boxes are then lidded and date stamped at approx 28-30 per minute.

after they have been date stamped they exit that conveyor onto a transport one into the freezer, an operator stands here and does this manually.

I offered to build a machine that takes the boxes lines them up and spaces them equally apart and then sends one left and one right etc. this enables the -32 deg to circulate between them better.

so as it stands the boxes go down a guide tunnel. a ram above stops the box for appx 2 secs and then lets it pass.

as soon as it passes the ram goes down to stop the next box.

now there is a ram that slide left to right to divert the pots.

im using an ardubox industrial shield as it was 24v compatible, has relays, isolated inputs/outputs and is DIN mountable.

I have a regulated 24v PSU supplying this and a row of din terminals. The solenoid valves are 24V dc the supply comes from the din rail through the NO relay and then to the coil.

oh and there is a pot to adjust timings for now untill I can fit a tacho that will get the belt speed although this doesnt really change.

this is basically it

@craskie1805, can you post a diagram of the machine to help us visualize the problem?

...R