Strange numbers printed to serial monitor

Hi,

I've been working on a code that will operate (with user controls) the pressurization system on a rocket I've been building. I got two TI pressure sensors (http://goo.gl/V73Ag), and hooked them up to A1 and A2. The positive/negative leads were attached to ground and 9. Then it got weird. Instead of a number coming up on the serial monitor, it instead came up as 0 when the pressure sensors were attached. When I took them out, the monitor started reading a bunch of random numbers (mostly around 240 or so). I tried another code (the one pasted below), and it just brought up a number around 1643, whether or not the sensors were plugged in. I'm using an Arduino One board on an Acer Aspire One 532h Netbook (http://goo.gl/S7Ikc). The code is posted below;

int command = 0;

void setup() {
Serial.begin(9600);
pinMode (12, OUTPUT); //Pressure Control Solenoid Valve
pinMode (11, OUTPUT); //Oxidizer Control Solenoid Valve
pinMode (10, OUTPUT); //Emergency Vent Solenoid Valve
pinMode (9, OUTPUT); //Pressure Sensors
digitalWrite (11, LOW);
digitalWrite (10, HIGH);
digitalWrite (12, LOW);
digitalWrite (9, HIGH);
}
void loop() {
if (Serial.available() > 0) {
int P1 = analogRead (A1); //Pressure Gauge
int P2 = analogRead (A2); //Pressure Gauge
int Temp = analogRead (A3); //Engine Thermistors
int command = Serial.read();
switch (command) {
case 1: //Launch Sequence
digitalWrite (10, LOW);
digitalWrite(12, HIGH);
digitalWrite(11, HIGH);
break;
case 2: //End Sequence
digitalWrite (10, HIGH);
digitalWrite (11, LOW);
digitalWrite (12, LOW);
break;
case 3: //Refuel Sequence
digitalWrite (11, LOW);
digitalWrite (12, LOW);
digitalWrite (10, HIGH);
break;
default: //Prints data
Serial.println ('P1 = '+ P1);
Serial.println ('P2 = '+ P2);
Serial.println ('Temp = '+ Temp);
delay (1000);
break;
if (P1 > 100){ //Emergency Shutdown
digitalWrite (10, HIGH);
digitalWrite (11, LOW);
digitalWrite (12, LOW);
}
if (P2 > 100){ //Emergency Shutdown
digitalWrite (10, HIGH);
digitalWrite (11, LOW);
digitalWrite (12, LOW);
}
if (Temp > 100){ //Emergency Shutdown
digitalWrite (10, HIGH);
digitalWrite (11, LOW);
digitalWrite (12, LOW);
}
}
}
}

Hopefully somebody else can figure out why this is happening; I'm not the best at coding. My board could be haunted by a poltergeist for all I know.

Thanks,
Gage

Serial.println ('P1 = '+ P1);

  1. You need to use double quotes (") when trying to print strings. Single quotes (') are just for single characters.
  2. You can't add a string to a number. You need to use two print statements.

Thanks; the majority of my programming experience was using python, so the difference between apostrophes and quotation marks were not expected.
I did some touching up, and I put this together;

int command = 0;

void setup() {
  Serial.begin(9600);
  pinMode (12, OUTPUT);      //Pressure Control Solenoid Valve
  pinMode (11, OUTPUT);      //Oxidizer Control Solenoid Valve
  pinMode (10, OUTPUT);      //Emergency Vent Solenoid Valve
  pinMode (9, OUTPUT);       //Pressure Sensors
  digitalWrite (11, LOW);
  digitalWrite (10, HIGH);
  digitalWrite (12, LOW);
  digitalWrite (9, HIGH);
}
void loop() {
  int P1 = analogRead (A1);        //Pressure Gauge
  int P2 = analogRead (A2);        //Pressure Gauge
  int Temp = analogRead (A3);      //Engine Thermistors
  Serial.println ((P1 - 100)*0.119047619);             //Prints Above Data
  Serial.println ((P2 - 100)*0.119047619);
  Serial.println (Temp);
  if (Serial.available() > 0) {
    int command = Serial.read();
    switch (command) {
      case 1:              //Launch Sequence
      digitalWrite (10, LOW);
      digitalWrite(12, HIGH);
      digitalWrite(11, HIGH);
      break;
      case 2:              //End Sequence
      digitalWrite (10, HIGH);
      digitalWrite (11, LOW);
      digitalWrite (12, LOW);
      break;
      case 3:              //Refuel Sequence
      digitalWrite (11, LOW);
      digitalWrite (12, LOW);
      digitalWrite (10, HIGH);
      break;
      }
    }
  if (P1 > 512){        //Emergency Shutdown
   digitalWrite (10, HIGH);
   digitalWrite (11, LOW);
   digitalWrite (12, LOW);
 }
   if (P2 > 512){        //Emergency Shutdown
     digitalWrite (10, HIGH);
     digitalWrite (11, LOW);
     digitalWrite (12, LOW);
 }
 if (Temp > 512){        //Emergency Shutdown
     digitalWrite (10, HIGH);
     digitalWrite (11, LOW);
     digitalWrite (12, LOW);
 }
 delay (500);
}

It's giving me a constant stream of numbers now, but I'm getting strange values for P2 and Temp. I haven't plugged in either of those two yet, but I'm getting values around 5.6 for P2 and 168 for Temp. As for P1, I get -0.48 consistently. The (x - 100)*0.119047619 for the values is because 0 PSI on the pressure regulator is 0.5v, and 5v is 110 PSI. AnalogRead() measures from 0.005v (1) to 5v (1024), so (x - 100)*0.119047619 gets the PSI from the voltage.

The positive/negative leads were attached to ground and 9.

You can't power up from digital outputs. Voltage must satisfy specification, +5V , you can get it from +5V pin.
If nothing attached to analog inputs, there would be "floating" level, to verify if inputs are o'k, connect to +3.3V, see if reading make sense.
0.5V corresponds to 102
What solenoids, where this output digital pins connected?

BTW, I'd suggest to use a map function:
int P1 = map( analogRead (A1), 102, 921, 0, 110); //Pressure Gauge
http://arduino.cc/en/Reference/Map

You can't power up from digital outputs. Voltage must satisfy specification, +5V , you can get it from +5V pin.
If nothing attached to analog inputs, there would be "floating" level, to verify if inputs are o'k, connect to +3.3V, see if reading make sense.

As in, connect the pressure valve to +3.3v? I'm not quite clear on what to do here.

What solenoids, where this output digital pins connected?

The solenoids are connected to pins 12, 11, and 10. They run on a higher voltage than the Arduino can provide, so I hooked up some 5v relays I had lying around. The Arduino activates the relays, which in turn enable the solenoids to open.

Never mind, it works. Thank you!

Magician:
BTW, I'd suggest to use a map function:
int P1 = map( analogRead (A1), 102, 921, 0, 110); //Pressure Gauge
map() - Arduino Reference

Completely unrelated to the topic, but thank you for calling my attention to that function. It looks immensely useful!

Completely unrelated to the topic, but thank you for calling my attention to that function. It looks immensely useful!

If you look at the numbers, and read description of the map function, it should be clear for you that 102 is 0.5V, or lower limit, 921 is 4.5V, or upper limit, 0 - 110 is dynamic range of the sensor. There is no point to do floating point math, because you can't get accuracy better than 3 LSB.

Quote
You can't power up from digital outputs. Voltage must satisfy specification, +5V , you can get it from +5V pin.
If nothing attached to analog inputs, there would be "floating" level, to verify if inputs are o'k, connect to +3.3V, see if reading make sense.

As in, connect the pressure valve to +3.3v? I'm not quite clear on what to do here.

Quote

You can't power up sensors, or any other equipment from the digital outputs. They are not design to be a power source, as current limited to 20 mA in normal condition, and voltage is not constant, as it drops to 4.2V (20 mA) when current increasing.
Board has +5V and GND two pins, specially implemented for powering external devices.

Try to connect analog inputs to 3.3V on-board pin, instead of sensor or leaving them unconnected. Reading in software 3.3V you could simulate specific level of pressure (apr. 60 %), and verify that everything works properly.

Quote
What solenoids, where this output digital pins connected?

The solenoids are connected to pins 12, 11, and 10. They run on a higher voltage than the Arduino can provide, so I hooked up some 5v relays I had lying around. The Arduino activates the relays, which in turn enable the solenoids to open.

Read above , you can't hook up relay to digital outputs. Even 20 mA led attached to the digital output, capable to screw up analogRead value introducing too much noise.