Okay so I updated my code so at the beginning of the program weight is set up as a global and it is 32.2
I have also taken out the strings. My output is now
Start of the Bins
Bolt is
Uknown
186
What can I do to better pass the global variable into the funtion?
// libraries needed for operation
//Scale imports
#include <hx711.h>
Hx711 scale(A2, A3);
//------------------
//Stepper imports
#include <Stepper.h>
#define STEPS 200
Stepper stepper(STEPS, 8, 9, 10, 11); //Pins for the motor driver going from 1 -> 4
//Global Variable Zone-------------
float weight = 32.2; // for the weight reading
int previous = 0; // for the stepper motor setup
//---------------------------------------------------------------------------------------------------------
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
// Pin Names are initialized here
//Drum and CMD----------------------------------------------------------------------------------
pinMode(13, OUTPUT); // Drum and CMD Transistor
digitalWrite(13, HIGH);
pinMode(12, OUTPUT); // Vibration Transistor
digitalWrite(12, HIGH);
//Meterwheel/COPA------------------------
pinMode(4, OUTPUT); // Meter Transistor
pinMode(7, OUTPUT); // Brush Transistor
pinMode(5, OUTPUT); // Valve
pinMode(2, INPUT); // Light Sensor A
digitalWrite(4, LOW);
digitalWrite(7, LOW);
digitalWrite(5, LOW);
//WeighStation------------------------
pinMode(6, OUTPUT); // Stopper Transistor
pinMode(3, INPUT); // Light Sensor B
digitalWrite(6, LOW);
//BINS------------------------Note the pins are initalized in the stepper command in the above section
stepper.setSpeed(30);
}
//--------------------------------------------------------------
// MAIN
void loop() {
// put your main code here, to run repeatedly:
delay(1000);
//MeterCopa();
//delay(200);
//WeighStation();
//delay(200);
Bins();
delay(200);
}
//------------------------------------------------------------------
//Meter wheel and copaslip ( Verrified on 4/10 at 5:46 pm)
void MeterCopa() // this section of the code is responsible for priming the bolts in the meter wheel and doing the COPA, but will not send the bolt forward
{
Serial.println("MeterCopa Start");
int LightSensorA = 0;
LightSensorA = digitalRead(2);
Serial.println("Priming the Bolt");
while(LightSensorA == HIGH) // this while loop will prime the bolt
{
digitalWrite(4,HIGH);
LightSensorA = digitalRead(2);
}
digitalWrite(4,LOW); // Bolt is primed no need to move the wheel anymore just need to run the CMD proccess
Serial.println("CopaSlipTime");
delay(200);
digitalWrite(5,HIGH); // This will move the cylinder up
digitalWrite(7,HIGH); // This will turn the mini motors on
delay(2000); // This delay will have to change with time when the machine is made
digitalWrite(5,LOW); // Release air P will bring cylinder down
digitalWrite(7,LOW); // Turn off the COPA motors
Serial.println("END of MeterCopa");
}
//---------------------------------------------------------------------
//Weighstation
void WeighStation()
{
Serial.println("WeighStation Start");
float tarr = 0;
float measure = 0;
int LightSensorA = 0;
int LightSensorB = 0;
LightSensorA = digitalRead(2);
LightSensorB = digitalRead(3);
//Before anything starts we need to stop any incoming bolts
digitalWrite(6,LOW);
Serial.println("Tarr the Scale");
delay(500);
//First calibrate the scale
tarr = scale.getGram();
// Now send the next bolt through
Serial.print(tarr,1);
Serial.println("g");
Serial.println("Send the Bolt");
while(LightSensorA == LOW) // this small while loop will send the bolt away from the meter wheel
{
digitalWrite(4,HIGH);
LightSensorA = digitalRead(2);
}
// may need to add a small delay here
digitalWrite(4,LOW); // stop the meter wheel once the bolt is away
Serial.println("Bolt Sent");
// Right now the bolt is in transit and we need to wait for it to arrive
while(LightSensorB == HIGH) // need to wait till sensor says bolt is present
{
Serial.println("Waiting on Bolt");
delay(100);
LightSensorB = digitalRead(3);
}
delay(100);
// this while loop will end when the light sensor is LOW ( sees a bolt)
measure = scale.getGram();
weight = tarr - measure;
weight = abs(weight);
Serial.print("bolt weight in g ");
Serial.println(weight);
// the bolt has now been weighed and is now ready to leave.
Serial.println("WeighStation End");
}
//-----------------------------------------------------------------------------------------
//Bins
void Bins()
{
Serial.println("Start of the Bins");
int LightSensorB = 0;
LightSensorB = digitalRead(3);
String bolt= "--Null--" ;
int val = 0;
// once the weight has been calculated we need to identify the bolt and then say the absolute position
// I'll make this part once I know more about the weights I expect to see
if ((weight >= 30) && (weight<=35))
{
val = 0;
bolt = "AL-0.500" ;
}
if ((weight >= 39) && (weight<=45))
{
val = 14;
bolt = "AL-0.750" ;
}
if ((weight >= 46) && (weight<=52))
{
val = 28;
bolt = "AL-1.000" ;
}
if ((weight >= 74) && (weight<=80))
{
val = 42;
bolt = "AG-1.000" ;
}
if ((weight >= 83) && (weight<=85))
{
val =56;
bolt = "AG-1.250" ;
}
if ((weight >= 86) && (weight<=90))
{
val = 70;
bolt = "AJ-0.875" ;
}
if ((weight >= 91) && (weight<=95))
{
val = 84;
bolt = "AJ-1.000" ;
}
if ((weight >=106 ) && (weight<=110))
{
val = 98;
bolt = "AJ-1.250" ;
}
if ((weight >=140 ) && (weight<=150))
{
val = 112;
bolt = "AJ-1.750" ;
}
if ((weight >=157 ) && (weight<=165))
{
val = 126;
bolt = "AJ-2.000" ;
}
if ((weight >=209 ) && (weight<=220))
{
val = 140;
bolt = "AJ-2.750" ;
}
if ((weight >=225 ) && (weight<=235))
{
val = 154;
bolt = "AJ-3.000" ;
}
if ((weight >=240 ) && (weight<=265))
{
val = 172;
bolt = "AG-4.500" ;
}
else
{
val = 186;
bolt = "Uknown";
}
Serial.println("Bolt is");
Serial.println(bolt);
Serial.println(val);
Serial.println("Stepper Motor Move now");
// now that the bolt and absolute position have been identifiied we need to move to that value ( lets call it val and will range 0 to 200)
stepper.step(val - previous); // go to the correct place
previous = val; // remember where you now are
Serial.println("Stepper in position");
// now we are in position and are ready to send the bolt away from the weigh station
digitalWrite(6,LOW);
Serial.println("Bolt in transit");
while(LightSensorB == LOW)
{
digitalWrite(6,HIGH);
}
delay(100); // extra timer to make sure the bolt got away
digitalWrite(6,LOW); // stop bolts again
Serial.println("End of Bins");
}