Good day
I am new to coding and ran into a problem. I am making a pulse generator for a agricultural fertilizer spreader. The spreader has a controller that controls linear actuators to open a gate to control the flow of fertilizer. It uses an encoder to determine shaft rotation for example: 1 RPM deposit 10kg of fertilizer and 2 RPM deposit 20kg of fertilizer. There is no direction on the encoder only speed. it uses 36 pulses per rotation. We modified the spreader to use only a gate opening and no shafts that rotate.
I want to use the position of the linear actuator to control the flow. for example: When potentiometer is at 10% it deposits 10kg of fertilizer and 50% deposits 50kg of fertilizer. So I am using an Arduino Nano to Read from the potentiometer and the generate a square wave pulse simulating shaft speed. I am not very skilled in coding but are learning fast. I got this code together by examples on the web. I read from analog then get an average reading to smooth the potentiometer value then I map it to an array because the linear actuator only reads from 685 to 989 not 0 to 1023. I also use the array to calibrate the flow of the fertilizer because in my testing I discovered that when the actuator is say 10% open it deposits 10 kg but when it is 40% open it deposits 60kg and at 70% it deposits 110kg of fertilizer. So I want to use the array to calibrate the flow at certain points.
I also need to mention that I am running 2 actuators on a single Nano and generating 2 pulses for a left and right side of the spreader. The code is working but not like I want it to. I am using the array to calibrate the points and works well but as soon as I start to use both actuators at the same time they influence each others readings. for example if the left side is at 100% open the pulse is at 10 m/s when I open or close the right actuator the pulses change. On the serial monitor it stays at 10m/s but on a oscilloscope I can see the pulses is changing. I am hoping someone can have a look at my code and assist me to make it work right and maybe point me in the right direction to improve to code.
const int signalPinLeft = 5; //Signal 1 generator output pin left
const int signalPinRight = 6; //Signal generator output pin right
//Left Spreader Array
int spreadposLeft[] = {10,15,20,25,30,35,40,45,50,55,60,65,70,75,80,85,90,95,100,105,110,115,120,125,130,135,140,145,150,155,160,165,170,175,180,185,190,195,200,
205,210,215,220,225,230,235,240,245,250,255,260,265,270,275,280,285,290,295,300,305,310,315,320,325,330,335,340,345,350,355,360,365,370,375,380,385,390,395,400,
405,410,415,420,425,430,435,440,445,450,455,460,465,470,475,480,485,490,495,500,505,510,515,520,525,530,535,540,545,550,555,560,565,570,575,580,585,590,595,600,
605,610,615,620,625,630,635,640,645,650,655,660,665,670,675,680,685,690,695,700,705,710,715,720,725,730,735,740,745,750,755,760,765,770,775,780,785,790,795,800,
805,810,815,820,825,830,835,840,845,850,855,860,865,870,875,880,885,890,895,900,905,910,915,920,925,930,935,940,945,950,955,960,965,970,975,980,985,990,995,1000}; //Calibration array product 1
//Right Spreader Array
int spreadposRight[] = {10,15,20,25,30,35,40,45,50,55,60,65,70,75,80,85,90,95,100,105,110,115,120,125,130,135,140,145,150,155,160,165,170,175,180,185,190,195,200,
205,210,215,220,225,230,235,240,245,250,255,260,265,270,275,280,285,290,295,300,305,310,315,320,325,330,335,340,345,350,355,360,365,370,375,380,385,390,395,400,
405,410,415,420,425,430,435,440,445,450,455,460,465,470,475,480,485,490,495,500,505,510,515,520,525,530,535,540,545,550,555,560,565,570,575,580,585,590,595,600,
605,610,615,620,625,630,635,640,645,650,655,660,665,670,675,680,685,690,695,700,705,710,715,720,725,730,735,740,745,750,755,760,765,770,775,780,785,790,795,800,
805,810,815,820,825,830,835,840,845,850,855,860,865,870,875,880,885,890,895,900,905,910,915,920,925,930,935,940,945,950,955,960,965,970,975,980,985,990,995,1000}; //Calibration array product 2
int signalStateLeft = LOW;//Initial Signal Left pin Low
int signalStateRight = LOW; //Initial Signal Right pin Low
int mVal1 = 0;
unsigned long previousTime1 = 0;
unsigned long previousTime2 = 0;
void setup() {
// initialize serial communication at 9600 bits per second:
Serial.begin(9600);
pinMode(signalPinLeft, OUTPUT);
pinMode(signalPinRight, OUTPUT);
pinMode (A3, INPUT);
pinMode (A4, INPUT);
}
void loop() {
//Sensor average left
int numReadsleft = 10;
int senseSumleft = 0;
for(int k = 0; k < numReadsleft; k++){
senseSumleft += analogRead(A3);
// delay(1);
}
int senseAveleft = senseSumleft / numReadsleft;
//sensor average right
int numReadsright = 10;
int senseSumright = 0;
for (int L = 0; L < numReadsright; L++){
senseSumright += analogRead(A4);
}
int senseAveright = senseSumright / numReadsright;
//Read Left Potentiometer
int sensorValueLeft = senseAveleft; // read the input on analog pin 3
int index = map(sensorValueLeft, 989, 685, 0, 199); //map(Potentiometervalue,max volts,min volts,array start number,number of caracters in array)
int spreadcalLeft = spreadposLeft[index];
//Read Right Potentiometer
int sensorValueRight = senseAveright;
int indexRight = map(sensorValueRight, 989, 685, 0, 199);
int spreadcalRight = spreadposRight[indexRight];
unsigned long currentTime = millis();
//Generate spreader left pulse
int long intervalLeft = spreadcalLeft; // interval at which to blink (milliseconds)
if (spreadcalLeft >= 1000) {
signalStateLeft = LOW;
}
if (currentTime - previousTime1 >= intervalLeft) {
// save the last time you blinked the LED
previousTime1 = currentTime;
// if the LED is off turn it on and vice-versa:
if (signalStateLeft == LOW) {
signalStateLeft = HIGH;
} else {
signalStateLeft = LOW;
}
digitalWrite(signalPinLeft, signalStateLeft); // set the LED with the signalStateLeft of the variable:
//Generate spreader right pulse
int long intervalRight = spreadcalRight; // interval at which to blink (milliseconds)
if (spreadcalRight >= 1000) {
signalStateRight = LOW;
}
if (currentTime - previousTime2 >= intervalRight) {
// save the last time you blinked the LED
previousTime2 = currentTime;
// if the LED is off turn it on and vice-versa:
if (signalStateRight == LOW) {
signalStateRight = HIGH;
} else {
signalStateRight = LOW;
}
digitalWrite(signalPinRight, signalStateRight); // set the LED with the signalStateLeft of the variable:
}
// print out the value you read:
Serial.print(sensorValueLeft);
Serial.print(" volt ");
Serial.print(spreadcalLeft);
Serial.print("m/s");
Serial.print(" Spreader Left ");
Serial.print(sensorValueRight);
Serial.print(" Volt ");
Serial.print(spreadcalRight);
Serial.print(" m/s ");
Serial.println(" Spreader Right ");
//delay(100); // delay in between reads for stability
}
}