Hi peeps. I am am having a bit of trouble and wondering if it is a problem with my code. The problem is that I am getting highs on my outputs which is making my clocks count up, when they should be low. The code is for monitoring when 3 gases are flowing and 2 valves are open. When any of the 3 gasses are flowing then I want a clock to monitor how long they are flowing for. If a gas is flowing then it is going to trigger the dep timer. Each of the gasses are high (5V ish) when they are not flowing and low when they are flowing. The problem is that I am getting a high output to the dep timer even when none of the gasses are flowing. The same is happening with the 2 valves (BGA - BGB). These are high when shut and low when open. Even when they are shut, I am still getting a high on my outputs and my clocks are counting up.
Now is this a fault with my code which is included below or a wiring fault? I have tried using all 3 gnds on the uno but it is exactly the same. Any help or asdvice would be greatly appreciated
Dave
int PHOS_IN_A1 = 1; //Declares an integer of Phos input
int BOR_IN_A2 = 2; //Declares an integer of Boron input
int ARS_IN_A3 = 3; //Declares an integer of Arsine input
int BGA_IN_A4 = 4; //Declares an integer of BGA input
int BGB_IN_A5 = 4; //Declares an integer of BGB input
int DEP_TIME = 12; //Declares an integer of Dep timer
int A_SIDE = 11; //Declares an integer of BGA timer
int B_SIDE = 10; //Declares an integer of BGB timer
long aRead = 0; //Assumes voltage range from input
void setup()
{
Serial.begin(9600); //Serial console for debugging
pinMode(DEP_TIME, OUTPUT); //Sets dep timer as an output
pinMode(A_SIDE, OUTPUT); //Sets BGA as an output
pinMode(B_SIDE, OUTPUT); //Sets BGB as an output
}
void loop()
{
float voltage; //Declares a floating number on voltage
for(int i=0; i <10; i++) //Takes 10 readings
aRead += analogRead(PHOS_IN_A1); //Reads phos in pin
aRead = aRead/10; //Takes the average of 10 readings
voltage = (aRead * 5.0)/1024.0; //Converts to digital
voltage = voltage*1000; //Converts to millivolts
if( voltage <= 2000 ) //If voltage less than 2V
{
digitalWrite(DEP_TIME, HIGH); //Gives 5V output to dep timer
}
for(int i=0; i <10; i++) //Takes 10 readings
aRead += analogRead(BOR_IN_A2); //Reads boron in pin
aRead = aRead/10; //Takes the average of 10 readings
voltage = (aRead * 5.0)/1024.0; //Converts to digital
voltage = voltage*1000; //Converts to millivolts
if( voltage <= 2000 ) //If voltage less than 2V
{
digitalWrite(DEP_TIME, HIGH); //Gives 5V output to dep timer
}
aRead = 0; //Assumes voltage range from input
for(int i=0; i <10; i++) //Takes 10 readings
aRead += analogRead(ARS_IN_A3); //Reads arsine in pin
aRead = aRead/10; //Takes the average of 10 readings
voltage = (aRead * 5.0)/1024.0; //Converts to digital
voltage = voltage*1000; //Converts to millivolts
if( voltage <= 2000 ) //If voltage less than 2V
{
digitalWrite(DEP_TIME, HIGH); //Gives 5V output to dep timer
}
aRead = 0; //Assumes voltage range from input
for(int i=0; i <10; i++) //Takes 10 readings
aRead += analogRead(BGA_IN_A4); //Reads BGA in pin
aRead = aRead/10; //Takes the average of 10 readings
voltage = (aRead * 5.0)/1024.0; //Converts to digital
voltage = voltage*1000; //Converts to millivolts
if( voltage <= 2000 ) //If voltage less than 2V
{
digitalWrite(A_SIDE, HIGH); //Gives 5V output to BGA timer
}
aRead = 0; //Assumes voltage range from input
for(int i=0; i <10; i++) //Takes 10 readings
aRead += analogRead(BGB_IN_A5); //Reads BGB in pin
aRead = aRead/10; //Takes the average of 10 readings
voltage = (aRead * 5.0)/1024.0; //Converts to digital
voltage = voltage*1000; //Converts to millivolts
if( voltage <= 2000 ) //If voltage less than 2V
{
digitalWrite(B_SIDE, HIGH); //Gives 5V output to BGB timer
}
aRead = 0; //Assumes voltage range from input
//Debug info to serial console
Serial.print("Analog: ");
Serial.print(aRead);
Serial.print(" Voltage:");
Serial.println(voltage);
delay(1000); //1 sec delay between measurements
}
Paul, there are 3 outputs, Dep timer, A-Side and B-side. The dep timer is counting how long one of the 3 gasses are flowing for (boron, arsine and phosphorous). If any of these are flowing then I want a high on my output, which will give me 5v to power my clock. Each gas when not flowing is a high when I measure it is a high and then goes low when one of the flows. So when any of them are flowing I want the output to go high. The problem is that when none of them are flowing, I am still getting a high on my output. Only one gas can flow at a time as they go towards producing a plasma.
The machine is an implanter which is used to manufacture semi conductors. The machine has 2 end stations where the silicon wafers are implated with the plasma. On the end stations there is a valve which allows the plasma through to implant the wafers. I am also trying to monitor when these valves are open and closed. It is the same situatuion with the valves where upon I am getting a high on my outputs even though it should be low.
One thing that I have noticed is that I have not assigned the input voltage to either be compared to the default one or the internal. Would this automatically not go to the default one if I did not assign it to anything?
The problem is that when none of them are flowing, I am still getting a high on my output.
Where do you ever set the pin LOW again?
If you've set the pin HIGH because the reading from PHOS_IN_A1 is low enough, is there really any reason to read the voltage of the other two gas pins?
When you have set the timer pin HIGH, what should set the timer pin LOW again? What causes the gasses to begin flowing? What prevents multiple gasses from flowing?
One thing that I have noticed is that I have not assigned the input voltage to either be compared to the default one or the internal. Would this automatically not go to the default one if I did not assign it to anything?
The default is to compare the input voltage to the reference voltage, which if one is not supplied is 5V.
The problem is that when none of them are flowing, I am still getting a high on my output.
Where do you ever set the pin LOW again?
If you've set the pin HIGH because the reading from PHOS_IN_A1 is low enough, is there really any reason to read the voltage of the other two gas pins?
When you have set the timer pin HIGH, what should set the timer pin LOW again? What causes the gasses to begin flowing? What prevents multiple gasses from flowing?
One thing that I have noticed is that I have not assigned the input voltage to either be compared to the default one or the internal. Would this automatically not go to the default one if I did not assign it to anything?
The default is to compare the input voltage to the reference voltage, which if one is not supplied is 5V.
You select the gas from the control panel. There is a seperate switch for each gas and they are all in parrallel. When a switch is pressed is goes grom 5V to 0V. I have soldered onto the output of each switch and they are going into the inputs in my uno. I need to monitor all the gasses as at some point the operator will switch from one gas to the other. There is only one output for all these 3 inputs as I need to monitor the total time which all the gasses are flowing. Once the gasses have been flowing for 300 hours then it will be time to perform a maint on the machine
When you have set the timer pin HIGH, what should set the timer pin LOW again? What causes the gasses to begin flowing? What prevents multiple gasses from flowing?
I have asked th uno to give me a high on the dep timer output if any of the gas inputs are less than 2 volts. Could I change my code from how I have got it to say IF any of the gas inputs are less than 2V, then give me a high on the dep timer output, else stay low all the time?
I have asked th uno to give me a high on the dep timer output if any of the gas inputs are less than 2 volts.
Yes, you have. But, you have never asked it to set the pin LOW if no gas is flowing.
Could I change my code from how I have got it to say IF any of the gas inputs are less than 2V, then give me a high on the dep timer output, else stay low all the time?
The pin stays however you set it. It does not automatically revert to some setting (except at reset). So, yes, you need to change your code.
I have asked th uno to give me a high on the dep timer output if any of the gas inputs are less than 2 volts.
Yes, you have. But, you have never asked it to set the pin LOW if no gas is flowing.
Could I change my code from how I have got it to say IF any of the gas inputs are less than 2V, then give me a high on the dep timer output, else stay low all the time?
The pin stays however you set it. It does not automatically revert to some setting (except at reset). So, yes, you need to change your code.
Cheers Paul. I need to put an else statement in don't I telling it to go low if none of the gasses are running? I have honestly been at this from 7am this morning ( it is now 5:30pm ) either soldering wires, setting everything up or trying to suss why it is not working. I am going to call it a day here now and carry on tomorrow.
Thanks for your help mate
Yes, but be careful where you put this else. You do not want to turn the pin off if gas 2 is not flowing, after turning it on because gas 1 IS flowing.
You should probably add a boolean, gasIsFlowing, with an initial value of false. In each gas test, set the glat true if the gas is flowing.
Then, after all three tests, turn the pin on or off, based on the flag.
Cheers Paul. i am going to give it a go today so will let you know how I get on. I can test it here at home so will play around with the code and see how I get on
Hi peeps. I have altered my code using the boolean for and. Can I do this so it measures the input of all 3 pins or do I have to seperate it into sections?? If anyone can take the time to have a look for me then i would appreciate it.
int PHOS_IN_A1 = 1; //Declares an integer of Phos input
int BOR_IN_A2 = 2; //Declares an integer of Boron input
int ARS_IN_A3 = 3; //Declares an integer of Arsine input
int BGA_IN_A4 = 4; //Declares an integer of BGA input
int BGB_IN_A5 = 4; //Declares an integer of BGB input
int DEP_TIME = 12; //Declares an integer of Dep timer
int A_SIDE = 11; //Declares an integer of BGA timer
int B_SIDE = 10; //Declares an integer of BGB timer
long aRead = 0; //Assumes voltage range from input
void setup()
{
Serial.begin(9600); //Serial console for debugging
pinMode(DEP_TIME, OUTPUT); //Sets dep timer as an output
pinMode(A_SIDE, OUTPUT); //Sets BGA as an output
pinMode(B_SIDE, OUTPUT); //Sets BGB as an output
}
void loop()
{
float voltage; //Declares a floating number on voltage
for(int i=0; i <10; i++) //Takes 10 readings
aRead += analogRead(PHOS_IN_A1 && BOR_IN_A2 && ARS_IN_A3); //Reads phos, bor
//and ars in pin
aRead = aRead/10; //Takes the average of 10 readings
voltage = (aRead * 5.0)/1024.0; //Converts to digital
voltage = voltage*1000; //Converts to millivolts
if( voltage <= 2000 ) //If voltage less than 2V
{
digitalWrite(DEP_TIME, HIGH); //Gives 5V output to dep timer
}
else if( voltage > 2000 ) //If voltage more than 2V
{
digitalWrite(DEP_TIME, LOW) ; //Gives no output if no gasses are flowing
}
aRead = 0; //Assumes voltage range from input
for(int i=0; i <10; i++) //Takes 10 readings
aRead += analogRead(BGA_IN_A4); //Reads BGA in pin
aRead = aRead/10; //Takes the average of 10 readings
voltage = (aRead * 5.0)/1024.0; //Converts to digital
voltage = voltage*1000; //Converts to millivolts
if( voltage <= 2000 ) //If voltage less than 2V
{
digitalWrite(A_SIDE, HIGH); //Gives 5V output to BGA timer
}
else if( voltage > 2000 ) //If voltage more than 2V
{
digitalWrite(A_SIDE, LOW); //Give no output on a side
}
aRead = 0; //Assumes voltage range from input
for(int i=0; i <10; i++) //Takes 10 readings
aRead += analogRead(BGB_IN_A5); //Reads BGB in pin
aRead = aRead/10; //Takes the average of 10 readings
voltage = (aRead * 5.0)/1024.0; //Converts to digital
voltage = voltage*1000; //Converts to millivolts
if( voltage <= 2000 ) //If voltage less than 2V
{
digitalWrite(B_SIDE, HIGH); //Gives 5V output to BGB timer
}
else if( voltage > 2000 ) //If voltage more than 2V
{
digitalWrite(B_SIDE, LOW); //Give no output on B side
}
aRead = 0; //Assumes voltage range from input
//Debug info to serial console
Serial.print("Analog: ");
Serial.print(aRead);
Serial.print(" Voltage:");
Serial.println(voltage);
delay(1000); //1 sec delay between measurements
}
if you find you're writing the same (or very nearly the same) code multiple time, like here
for(int i=0; i <10; i++) //Takes 10 readings
aRead += analogRead(BGA_IN_A4); //Reads BGA in pin
aRead = aRead/10; //Takes the average of 10 readings
voltage = (aRead * 5.0)/1024.0; //Converts to digital
voltage = voltage*1000; //Converts to millivolts
it is a sure sign that you need to be introduced to functions:
float readVoltage (int inputPin, int nReadings)
{
unsigned long sum = nReadings / 2; // pre-round the result
for(int i=0; i < nReadings; i++) {
sum += analogRead(inputPin);
}
sum /= nReadings;
return (sum * 5.0)/1024.0;
}
As you surmise, you need to do them one at a time.
Try adding more serial prints so you can see what readings your sensors are actually giving the Arduino - might flush out a wiring issue. Speaking of which, seeing the wiring diagram may well help too.
Also, it sounds like you're using an Arduino output pin to drive an external device (timer). Is this direct? In which case does this device draw more than 40ma? You may need a transistor or relay to drive it from another source.
Cheers lads. I am having issues uploading any code to my unit again. I keep getting protocol issues. It will let me upload then if I reset and try to upload another code then it will not allow it. I am going to have a bite to eat and a bit of a break before i have another go at the code and resolving any issues.
Thanks for your help, will let you know how I get on this afternoon
As you surmise, you need to do them one at a time.
Try adding more serial prints so you can see what readings your sensors are actually giving the Arduino - might flush out a wiring issue. Speaking of which, seeing the wiring diagram may well help too.
Also, it sounds like you're using an Arduino output pin to drive an external device (timer). Is this direct? In which case does this device draw more than 40ma? You may need a transistor or relay to drive it from another source.
No the load (timer) is below 40mA. I have had the timers going but because I did not tell the chip what to do if no gasses were flowing, then my chip latched out and kept giving me a high on my output, when I needed it to be low.
Paul put me on the right path and I am slowly getting my head round the code and how it works.
I think my wiring is ok mate as I have my dvm out checking voltages and so on and it seems to be working. I just need to sort my code out and resolve this issue regarding uploading the code.
Let me have a brew, bath and bite to eat and I will crack on again. I will let you know how i get on later this afternoon.
Cheers fellas
Yes, but be careful where you put this else. You do not want to turn the pin off if gas 2 is not flowing, after turning it on because gas 1 IS flowing.
You should probably add a boolean, gasIsFlowing, with an initial value of false. In each gas test, set the glat true if the gas is flowing.
Then, after all three tests, turn the pin on or off, based on the flag.
Paul
I am unsure what you think I should do here. Could you explain it a bit more or perhaps show me an example?
Would I put GasIsFlowing in my setup bit and declare it false? Then in each bit for the gas flowing, instead of saying if gas < 2000, give me a gigh on the dep timer. I would put GasIsFlowing = true. Then after it had read all the pin inputs, I would put, If GasIsFlowing = True, DigitalWrite Dep_Time, High. Else DigitalWrite Dep_time = Low.
Would it be something along them lines?
Dave
Dave
If you take AWOL's advice to move the pin reading/average calculation code into a function, you would call it like so:
void loop()
{
float voltage = readVoltage(PHOS_IN_A1);
if( voltage <= 2000 ) //If voltage less than 2V
{
digitalWrite(DEP_TIME, HIGH); //Gives 5V output to dep timer
}
else if( voltage > 2000 ) //If voltage more than 2V
{
digitalWrite(DEP_TIME, LOW) ; //Gives no output if no gasses are flowing
}
voltage = readVoltage(BOR_IN_A2);
if( voltage <= 2000 ) //If voltage less than 2V
{
digitalWrite(DEP_TIME, HIGH); //Gives 5V output to dep timer
}
else if( voltage > 2000 ) //If voltage more than 2V
{
digitalWrite(DEP_TIME, LOW) ; //Gives no output if no gasses are flowing
}
voltage = readVoltage(ARS_IN_A3);
if( voltage <= 2000 ) //If voltage less than 2V
{
digitalWrite(DEP_TIME, HIGH); //Gives 5V output to dep timer
}
else if( voltage > 2000 ) //If voltage more than 2V
{
digitalWrite(DEP_TIME, LOW) ; //Gives no output if no gasses are flowing
}
}
which clearly would be a problem, as the timer would be turned on if a gas was flowing, and off if the last gas was not flowing. So, you fix that by adding a flag:
void loop()
{
bool gasIsFlowing = false;
float voltage = readVoltage(PHOS_IN_A1);
if( voltage <= 2000 ) //If voltage less than 2V
gasIsFlowing = true;
voltage = readVoltage(BOR_IN_A2);
if( voltage <= 2000 ) //If voltage less than 2V
gasIsFlowing = true;
voltage = readVoltage(ARS_IN_A3);
if( voltage <= 2000 ) //If voltage less than 2V
gasIsFlowing = true;
// If gas is flowing, turn on the timer
if(gasIsFlowing)
{
digitalWrite(DEP_TIME, HIGH); //Gives 5V output to dep timer
}
else
{
digitalWrite(DEP_TIME, LOW) ; //Gives no output if no gasses are flowing
}
}