Change rate at which vacuum pumps activate based on sensor

Hi everyone,

I am very new to both Arduino and the forums. Please let me know if I am committing any forum faux pas and i will amend my post right away.

On to the project...

I am trying to change the rate at which two vacuum pumps alternate based on the information being sent from an IR range sensor.

Here is my current code:

/*************************************************************
Motor Shield 1-Channel DC Motor Demo
by Randy Sarafan

For more information see:
http://www.instructables.com/id/Arduino-Motor-Shield-Tutorial/


Function pins per Ch. A pins per Ch. B
Direction          D12           D13
PWM                  D3           D11
Brake                  D9           D8
Current Sensing          A0           A1

Note: delay= lowest value 700
            highest value 2064
*************************************************************/

void setup() {
  Serial.begin(9600);
 //Setup Channel A
 pinMode(12, OUTPUT); //Initiates Motor Channel A pin
 pinMode(13, OUTPUT); //Initiates Motor Channel B pin
 pinMode(9, OUTPUT); //Initiates Brake Channel A pin
 pinMode(8, OUTPUT); //Initiates Brake Channel B pin
}

void loop(){
  int sensorValue = map(analogRead(3), 0,600, 0,255);
//int sensorValue = 500;
 //forward @ full speed
 digitalWrite(9, HIGH);//Brake motor A
 digitalWrite(13, HIGH); //Establishes forward direction of Channel B
 digitalWrite(8, LOW);   //Disengage the Brake for Channel B
 analogWrite(11, sensorValue);   //Spins the motor on Channel B
 delay(2064 - sensorValue);
 digitalWrite(8, HIGH);//Brake motor B
 digitalWrite(12, HIGH); //Establishes forward direction of Channel A
 digitalWrite(9, LOW);   //Disengage the Brake for Channel A
 analogWrite(3, sensorValue);   //Spins the motor on Channel A
 delay(2064 - sensorValue);
}

This code is functional but instead of changing the rate of the pumps based on proximity to the sensor it actually changes the strength of the vacuum pumps. This means that the further away you are from the sensor the lower the power of the pumps. The pumps still alternate at a set duration.

Unfortunately my understand of code is not at a high enough level for me to proceed with this project alone. I'd be really grateful for any advice as to how I should proceed.

Thank you in advance!

The pumps still alternate at a set duration.

No. This delay() calls change the duration that each pumps runs (at variable speed) based on the distance, too.

That the interval only varies between 2064 milliseconds and 1809 milliseconds may be the problem.

What do you want the interval range to be?

Hi PaulS,
Thank you for the reply.

again apologies if my terminology is incorrect, if I understand correctly I wish for the shortest interval to be 700 and the longest to be 2064

Thank you again for your time.

Welcome to the Forum. Please read these posts:

How to use this forum - please read.
and
Read this before posting a programming question … and
Planning and Implementing an Arduino Program

You have posted code without using code tags. The code tags make the code look

like this

when posting source code files. It makes it easier to read, and can be copied with a single mouse click. Also, if you don’t do it, some of the character sequences in the code can be misinterpred by the forum code as italics or funny emoticons.
If you have already posted without using code tags, open your message and select “modify” from the pull down menu labelled, “More”, at the lower left corner of the message. Highlight your code by selecting it (it turns blue), and then click on the “</>” icon at the upper left hand corner. Click on the “Save” button. Code tags can also be inserted manually in the forum text using the code and /code metatags.

Unless the sketch is too large, it’s better if you post your code, rather than attach it. When it’s attached, we have to download it, create a folder then open your code in our IDE. And afterwards, the folder remains unless we navigate to the “Temp” folder and manually remove it. It’s much easier to just view the code in your post.

Many questions can be answered by reading the documentation which is provided with the IDE, available under the help tab, or online here.

There are many other things that programmers do to make their code understandable. Please do them, as a courtesy to the members who volunteer their time to help you here. One is to use a standard indentation to clearly show the code blocks. Never put more than one statement per line. Place any brackets by themselves on a separate line. Before posting the code, use Ctrl-T in the IDE to reformat the code in a standard format, which makes it easier for us to read. Another is to give things descriptive names. You can name numerical constants, pin numbers, variables and many other things in this way. For example, you can refer to a pin and an output level by number, like digitalWrite(3,0). But such a statement doesn’t reveal anything about the purpose. digitalWrite(hornRelayPin, LOW) does. You can do that by declaring const byte hornRelayPin = 3; before setup() in your program. Many such names are already defined for you by the compiler and the Arduino IDE. Here are some:

#define HIGH 0x1
#define LOW  0x0
#define PI 3.1415926535897932384626433832795

Use them. There are many more. Use compiler math to compute values so you can see where they came from (or at least document them). For example, if you see the number 73, you would be hard put to explain the significance of it. But if you see “daysPerYear/5”, it is obvious. One more thing. When you work on program continuously, you become familiar with it. So many things seem obvious even if they are not spelled out explicitly. But try looking at your own code six months later. It will be as if a stranger wrote it. So write for strangers, not yourself.

Edit - Oh, wait. It’s an instructable. That explains everything.

I wish for the shortest interval to be 700 and the longest to be 2064

That is a difference of 1364, which is about 5 times the value stored in sensorValue, so try:

delay(2064 - 5*sensorValue);

That will give you a range of 789 to 2064.

If that’s not low enough, change the scale factor (5) to something somewhat larger.

Thank you again PaulS, that is fantastic advice and is very clear.

One issue with this is that when I step away from the sensor the pumps stop all together. Is it possible to set the pumps to always alternate at a specific rate until the sensor activates? (at which point the rate will increase depending on proximity)

Thank you again for your time!

until the sensor activates?

The sensor is always active. You are reading it every time through loop().

I suspect that your question is really "Can I make the motors run at a constant speed when the distance reading exceeds some value?". The answer is yes.

You need to decide what that constant speed/interval is, and what the distance threshold will be. Then, a simple if statement with an else block is all that is needed.