Hello - I am working on a project where I am working with air pumps, valves, H bridges. I got to run the pumps running in the code. Now I am trying to control the air pumps using flex sensor as a trigger. I know this code I have now wouldn't work. Where do i change to make the pumps running? Also, I am unsure if I connected the flex sensors on breadboard and arduino correctly. I will be using 12V power supply instead of 5V and it gets confusing because of that. Thank you
//Code by Reichenstein7 (thejamerson.com)
// Declare L298N Dual H-Bridge Motor Controller directly since there is not a library to load.
const int flexPin = A0;
// valve 1 used in this example
int dir1PinA = 12;
int dir2PinA = 11;
int speedPinA = 13; // Needs to be a PWM pin to be able to control motor speed
// valve 2 - used in this example
int dir1PinB = 8;
int dir2PinB = 10;
int speedPinB = 9; // Needs to be a PWM pin to be able to control motor speed
// pump 1 used in this example
int dir1PinC = 2;
int dir2PinC = 4;
int speedPinC = 3; // Needs to be a PWM pin to be able to control motor speed
// pump 2 - used in this example
int dir1PinD = 7;
int dir2PinD = 5;
int speedPinD = 6; // Needs to be a PWM pin to be able to control motor speed
int valve1 =255;
int valve2 =0;
int pump1 =0; //max value of 95 to limit to 4.5V rather than 12v which would break the pump
int pump2 =95;
// keep track of time
int count=0;
void setup() { // Setup runs once per reset
// initialize serial communication @ 9600 baud:
Serial.begin(9600);
//Define L298N Dual H-Bridge Motor Controller Pins for motor 1 - not used in this example
pinMode(dir1PinA,OUTPUT);
pinMode(dir2PinA,OUTPUT);
pinMode(speedPinA,OUTPUT);
//using motor2 outputs here only - these pins
pinMode(dir1PinB,OUTPUT);
pinMode(dir2PinB,OUTPUT);
pinMode(speedPinB,OUTPUT);
// sets the polarity of the valve ooutputs keep fixed
//valve 1
digitalWrite(dir1PinA, LOW);
digitalWrite(dir2PinA, HIGH);
// valve2
digitalWrite(dir1PinB, LOW);
digitalWrite(dir2PinB, HIGH);
// pump pins settings
pinMode(dir1PinC,OUTPUT);
pinMode(dir2PinC,OUTPUT);
pinMode(speedPinC,OUTPUT);
//using motor2 outputs here only - these pins
pinMode(dir1PinD,OUTPUT);
pinMode(dir2PinD,OUTPUT);
pinMode(speedPinD,OUTPUT);
// sets the polarity of the valve ooutputs keep fixed
// pump 1
digitalWrite(dir1PinC, LOW);
digitalWrite(dir2PinC, HIGH);
// pump 2
digitalWrite(dir1PinD, LOW);
digitalWrite(dir2PinD, HIGH);
}
void loop() {
int flexValue;
flexValue = analogRead(flexPin);
Serial.print("sensor: ");
Serial.println(flexValue);
// Motor 2 Forward
//Serial.println("Motor 2 Forward");
// Serial.println(pump1);
// write the valve variables to turn on or off valve
analogWrite(speedPinA,valve1);
analogWrite(speedPinB,valve2);
// write the pump variables to turn on or off valve
analogWrite(speedPinC,pump1);
analogWrite(speedPinD,pump2);
// this setup is for using a 4.5V DC motor with a 12V powersupply directly connected to the H-Bridge - one then has to use the speedtest variable
// to reduce the outgoing voltage to the range acceptable to the motor - so in this case speed range is 0-255 which would be 0V-12V but we want only
// 4.5v max so we need to calcuate 255/12*4.5 = 95 - so 0-95 should be the range we drive the motor in through the speedtest variable - below about
//55 the motor stalls so can adjust range to 0-95 for the pumps - but not for the valves which need 12V to activate - for valves it is 0 off and 255 on
// keep track of how many times directions has switched to know which way to turn
count++;
// every 30 loops that if statement is true and inverts between 0 and 255 depending on the current value of vale1 or vale2
if(flexValue>890)
//if ((count%1000)==0) {
if (valve1==0) valve1=255;
else valve1=0;
if (valve2==255) valve2=0;
else valve2=255;
// switch one and off alternatively the pumps to push air in or suck air out in sync with the valves
if (pump1==0) pump1=95;
else pump1=0;
if (pump2==95) pump2=0;
else pump2=95;
delay(20);
}