Go Down

Topic: Getting output highs when I should be getting lows (Read 15766 times) previous topic - next topic

AWOL

#30
Mar 22, 2012, 09:43 pm Last Edit: Mar 22, 2012, 10:23 pm by AWOL Reason: 1
Your remaining debug output isn't very useful, it seems to me.
Scatter some more prints around and see what pops out.
Code: [Select]
const int PHOS_IN_A1 = 1;
const int BOR_IN_A2 = 2;
const int ARS_IN_A3 = 3;
const int BGA_IN_A4 = 4;
const int BGB_IN_A5 = 5;
const int DEP_TIME = 12;
const int A_SIDE = 11;  
const int B_SIDE = 10;    

#define N_READINGS 10
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
}

float readVoltage (int pin)
{
 long aRead = N_READINGS / 2;  //pre-round the result
 for(int i=0; i <N_READINGS; i++)  
   aRead += analogRead(pin);  
 aRead = aRead / N_READINGS;
 float voltage = (aRead * 5.0)/1024.0;   //Converts to digital
 return voltage*1000;      
}

void loop()
{
  bool gasIsFlowing = false;
  float phosVoltage = readVoltage(PHOS_IN_A1);
  if( phosVoltage <= 2000 )  //If voltage less than 2V
     gasIsFlowing = true;
  Serial.print(" gasIsFlowing:");
  Serial.println(gasIsFlowing ? "true":"false");
  // If gas is flowing, turn on the timer
  digitalWrite(DEP_TIME, gasIsFlowing ? HIGH : LOW);

  float bgaVoltage = readVoltage(BGA_IN_A4 );
  digitalWrite(A_SIDE, bgaVoltage <= 2000 ? HIGH : LOW);   //Gives 5V output to BGA timer
  Serial.print("bgaVoltage:");
  Serial.println(bgaVoltage);
// and so on


substance

I put a few in but I don't understand what I am suppose to be getting out. It is driving me mad now and I seem to be going backwards rather than forwards.
I amended my code to the following
Code: [Select]
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 = 5;  //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

#define N_READINGS 10    // somewhere near the top of your program
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
}

// just about anywhere in your program, except inside another function
float readVoltage (int pin)
{
  long aRead = N_READINGS / 2;  //pre-round the result
  for(int i=0; i <10; i++)   //Takes 10 readings
    aRead += analogRead(pin);  //Reads phos in pin
 
  aRead = aRead / N_READINGS;        //Takes the average of 10 readings
  float voltage = (aRead * 5.0)/1024.0;   //Converts to digital
  return voltage*1000;       
}

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;
Serial.print(" gasISFlowing:");
    Serial.println(gasIsFlowing ? HIGH : LOW);

   // If gas is flowing, turn on the timer
   if(gasIsFlowing = true)
   {
     digitalWrite(DEP_TIME, HIGH); //Gives 5V output to dep timer
   }
   else
   {
     digitalWrite(DEP_TIME,  LOW) ;   //Gives no output if no gasses are flowing
   }

   voltage = readVoltage(BGA_IN_A4 );
   if( voltage <= 2000 )           //If voltage less than 2V
   {
      digitalWrite(A_SIDE, HIGH);   //Gives 5V output to BGA timer
   }
   else
   {
      digitalWrite(A_SIDE, LOW);   //Give no output on a side
       Serial.print("BGA_IN_A4:");
    Serial.println(BGA_IN_A4);

   }

   voltage = readVoltage(BGB_IN_A5 );
   if( voltage <= 2000 )  //If voltage less than 2V
   {
      digitalWrite(B_SIDE, HIGH); //Gives 5V output to BGB timer
   }
   else
   {
      digitalWrite(B_SIDE, LOW);   //Give no output on B side
   }

//Debug info to serial console
    Serial.print(" Voltage:");
    Serial.println(voltage);

    delay(1000);  //1 sec delay between measurements
}


The output from the serial monitor I really do not understand. Someone please help :-(

PaulS

Code: [Select]
Serial.print(" gasISFlowing:");
    Serial.println(gasIsFlowing ? HIGH : LOW);

This should print "gasIsFlowing: true" or "gasIsFlowing: false", depending on what was read from the gas sensor pins.

Code: [Select]
      Serial.print("BGA_IN_A4:");
    Serial.println(BGA_IN_A4);

I'm not sure why you are printing the pin number.

Code: [Select]
    Serial.print(" Voltage:");
    Serial.println(voltage);

Move these into readVoltage(), so you can see the voltage from each of the 5 pins, not just the voltage from the last pin.

Show us the output.
The art of getting good answers lies in asking good questions.

substance

Right peeps, heres what I changed the code with, with all the serial parts. I am totally lost now but determined not to give up
Code: [Select]
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 = 5;  //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

#define N_READINGS 10    // somewhere near the top of your program
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
}

// just about anywhere in your program, except inside another function
float readVoltage (int pin)
{
  long aRead = N_READINGS / 2;  //pre-round the result
  for(int i=0; i <10; i++)   //Takes 10 readings
    aRead += analogRead(pin);  //Reads phos in pin
 
  aRead = aRead / N_READINGS;        //Takes the average of 10 readings
  float voltage = (aRead * 5.0)/1024.0;   //Converts to digital
  return voltage*1000;   
  Serial.print(" Voltage:");
    Serial.println(voltage); 
}

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;
     
        Serial.print(" Voltage:");
    Serial.println(voltage);

   // If gas is flowing, turn on the timer
   if(gasIsFlowing = true)
   {
     digitalWrite(DEP_TIME, HIGH); //Gives 5V output to dep timer
   }
   else
   {
     digitalWrite(DEP_TIME,  LOW) ;   //Gives no output if no gasses are flowing
   }

   voltage = readVoltage(BGA_IN_A4 );
     Serial.print(" Voltage:");
    Serial.println(voltage);
   if( voltage <= 2000 )           //If voltage less than 2V
   {
      digitalWrite(A_SIDE, HIGH);   //Gives 5V output to BGA timer
   }
   else
   {
      digitalWrite(A_SIDE, LOW);   //Give no output on a side
         Serial.print(" A_SIDE:");
    Serial.println(A_SIDE);
   
   }

   voltage = readVoltage(BGB_IN_A5 );
   if( voltage <= 2000 )  //If voltage less than 2V
   {
      digitalWrite(B_SIDE, HIGH); //Gives 5V output to BGB timer
   }
   else
   {
      digitalWrite(B_SIDE, LOW);   //Give no output on B side
       Serial.print(" B_SIDE:");
    Serial.println(B_SIDE);
     
   }

//Debug info to serial console
    Serial.print(" Voltage:");
    Serial.println(voltage);

    delay(1000);  //1 sec delay between measurements
}


And here is what I got from the serial monitor with nothing connected to the board apart from the usb
Quote
Voltage:2143.55
Voltage:2402.34
A_SIDE:11
B_SIDE:10
Voltage:2988.28
Voltage:2036.13
Voltage:2319.34
A_SIDE:11
B_SIDE:10
Voltage:2998.05
Voltage:1923.83
Voltage:2216.80
A_SIDE:11
B_SIDE:10
Voltage:2993.16
Voltage:1811.52
Voltage:2089.84
A_SIDE:11
B_SIDE:10
Voltage:2924.80
Voltage:1713.87
Voltage:1962.89
B_SIDE:10
Voltage:2822.27
Voltage:1948.24
Voltage:1708.98
Voltage:1738.28
Voltage:2163.09
Voltage:2426.76
A_SIDE:11
B_SIDE:10
Voltage:2778.32
Voltage:1962.89
Voltage:2275.39
A_SIDE:11
B_SIDE:10
Voltage:2895.51
Voltage:1831.05
Voltage:2119.14
A_SIDE:11
B_SIDE:10
Voltage:2866.21

AWOL

A_SIDE and B_SIDE are both program constants, so there's no real need to keep printing them out.
OToH, telling which voltage is which is very difficult from your printout.
That is easily fixed.

PaulS

Code: [Select]
   if(gasIsFlowing = true)
Needs ==, not =.

Code: [Select]
        Serial.print(" Voltage:");
    Serial.println(voltage);

You are printing the voltage in the function. It is redundant to print it here in loop() again.

Code: [Select]
   else
   {
      digitalWrite(A_SIDE, LOW);   //Give no output on a side
         Serial.print(" A_SIDE:");
    Serial.println(A_SIDE);
   
   }

I fail to understand why you are printing the pin number if the BGA_IN_A4 voltage is greater than 2. What does that tell you?
The art of getting good answers lies in asking good questions.

substance

Paul to be honest, I have no idea what I am doing with this serial monitor. AWOl suggested I just have a mess around and see if I can get to grips with what I am doing. Put bits in here and there to see if I can work things out. I will be honest and admit that I am a bit lost with things at the minute but nothing worthwhile in life ever comes easy does it?
I will have another bit of a mess and see how things go.
I feel like I owe you an awol a few pints already lol :-)

PaulS

Quote
I will be honest and admit that I am a bit lost with things at the minute but nothing worthwhile in life ever comes easy does it?

No, it doesn't. And it does get easier. Perhaps the thing to do it to post your code, line by line, with your understanding of what each line does. We can correct any misunderstandings, and explain anything you don't understand. For example:
Code: [Select]

float readVoltage (int pin)

Defines a function that take s pin number, and returns a float
Code: [Select]
{
The start of the function block
Code: [Select]
  long aRead = N_READINGS / 2;  //pre-round the result
Not sure what you are doing here. I would have initialized aRead to 0
Code: [Select]
  for(int i=0; i <10; i++)   //Takes 10 readings
The comment says it all.
Code: [Select]
    aRead += analogRead(pin);  //Reads phos in pin
Read from the pin, and add the reading to aRead.
Code: [Select]
  aRead = aRead / N_READINGS;        //Takes the average of 10 readings
  float voltage = (aRead * 5.0)/1024.0;   //Converts to digital
  return voltage*1000;   
  Serial.print(" Voltage:");
    Serial.println(voltage); 
}

Add your comments the rest of the way through...

By the way, the Voltage: nnnn.nn output that the last two line should print will never happen, since they are placed AFTER the return statement.
The art of getting good answers lies in asking good questions.

substance

Thank you very much Paul. I would really appreciate it if you would do that for me. I will dismantle it line by line and go through what I think is happening

AWOL

Quote
Not sure what you are doing here. I would have initialized aRead to 0

The comment makes it clear.
The code is about to average a number of readings; this line makes sure the average is rounded.

PaulS

Quote
The code is about to average a number of readings; this line makes sure the average is rounded.

Perhaps it's the headache, but I don't see this.

If N_READINGS is 10, aRead gets an initial value of 5. If N_READINGS were changed to 13, aRead would get an initial value of 6.

Since each reading gets added to the initial value in aRead, the more readings, the more skewed the results, as I see it.

Please, explain what I am missing.
The art of getting good answers lies in asking good questions.

AWOL

Imagine I want to average ten integer readings.
I read eight ones and two zeroes, total eight.
I divide eight by ten and get zero.
Not very representative, huh?
So, initialise the sum to half of ten before you start, or add five at the end.
You say skew, I say round.

PaulS

Quote
You say skew, I say round.

OK. I follow that. But, since the function returns a float, and a long and a float are the same size, and only one divide is performed, I think I would simply accept the performance it, and make aRead a float. No need to pre-round anything, that way.
The art of getting good answers lies in asking good questions.

AWOL

Yeah, I know, but I originally wrote the function to return an average reading, not do the conversion to volts.

substance

Declares intergers of variables . These are my inputs and outputs to the uno
Quote
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 = 5;  //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


Assumes the voltage range from  the input
Code: [Select]
long aRead = 0;

I am a bit unsure what is happening here
Code: [Select]
#define N_READINGS 10  

This is my setup part where I set my output pins and set the serial console for debugging. The (9600) is the baud rate
Code: [Select]
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
}

I presume this means that voltage read on the int pins will have a floating value??
[code]float readVoltage (int pin)


I am a bit unsure with some of this next bit. I will highlight in the code what I am unsure about
Code: [Select]
float readVoltage (int pin)
{
 long aRead = N_READINGS / 2;  //pre-round the result (not sure what this is doing)
 for(int i=0; i <10; i++)   //Takes 10 readings
   aRead += analogRead(pin);  //Does this mean the that the analog inputs are being read
 
 aRead = aRead / N_READINGS;        //Takes the average of 10 readings (again not sure about N_READINGS)
 float voltage = (aRead * 5.0)/1024.0;   //Converts to digital
 return voltage*1000;       //converts to milivolts
 Serial.print(" Voltage:");  // I put these serial bits in trying to see what happens
   Serial.println(voltage);  
}

[/code]

This is the part of the code which carries out all my functions
Code: [Select]
void loop()
{
  bool gasIsFlowing = false;  //sets a flag which will be used later
  float voltage = readVoltage(PHOS_IN_A1);   //reads the floating voltage on the phos in pin
  if( voltage <= 2000 )  //If voltage less than 2V
     gasIsFlowing = true;  //makes my flag true if the criteria is met

  voltage = readVoltage(BOR_IN_A2);    //This is the same as the above bit of code except reading a different pin
  if( voltage <= 2000 )  //If voltage less than 2V
     gasIsFlowing = true;

  voltage = readVoltage(ARS_IN_A3);   //As above
  if( voltage <= 2000 )  //If voltage less than 2V
     gasIsFlowing = true;
     
       Serial.print(" Voltage:");  //I put these in just to see
   Serial.println(voltage);


Bow it looks for my boolean flag
Code: [Select]
if(gasIsFlowing == true)  //If this is true depending on the above criteria being met
  {
    digitalWrite(DEP_TIME, HIGH); //Gives 5V output to dep timer
  }
  else // If gasisflowing is not true then do something else
  {
    digitalWrite(DEP_TIME,  LOW) ;   //Gives no output if no gasses are flowing
  }


Code: [Select]
voltage = readVoltage(BGA_IN_A4 ); //reads the voltage on the beamgate A side pin
    Serial.print(" Voltage:"); // I put this in, not sure whats happening
   Serial.println(voltage);
  if( voltage <= 2000 )           //If voltage less than 2V
  {
     digitalWrite(A_SIDE, HIGH);   //Gives 5V output to BGA timer
  }
  else
  {
     digitalWrite(A_SIDE, LOW);   //Give no output on a side
        Serial.print(" A_SIDE:");  //again not sure why I have put this in
   Serial.println(A_SIDE);



This is the same as above but for the b side end station
Code: [Select]
 voltage = readVoltage(BGB_IN_A5 );
  if( voltage <= 2000 )  //If voltage less than 2V
  {
     digitalWrite(B_SIDE, HIGH); //Gives 5V output to BGB timer
  }
  else
  {
     digitalWrite(B_SIDE, LOW);   //Give no output on B side
      Serial.print(" B_SIDE:"); // again I have no idea whats going on
   Serial.println(B_SIDE);
     
  }



The debugging part. What will be sent back to the monitor by the serial communication
Code: [Select]
//Debug info to serial console
   Serial.print(" Voltage:");
   Serial.println(voltage);

   delay(1000);  //1 sec delay between measurements
}

Go Up