Sketch help? Several sensor inputs and outputs, use analog or digital pins?

thanks paul, what you think now?

//This is my first ever sketch i am writing for my 2650. i might need help.
//This sketch will eventually control my camper with my droid or voice.
//I will worry less about things going wrong with my off grid trailer.
//Worrying can lead to high anxiety. Stress affects you emotionally and physically.

//Start of my pins
int Water = 22; //Voltage sensor for my SeeLevel II water tank sensor strip
int Tank1 = 24; //load sensor pin for propane tank 1
int Tank2 = 26; //load sensor pin for propane tank 2
int Humid1 = 28; //pin for humidity sensor inside
int Humid2 = 30; //humidity sensor inside
int Temp1 = 42; //DS18B20 temperature sensor under camper
int Temp2 = 34; //DS18B20 temperature inside camper
int Volts = 36; //battery bank voltage
int Amps = 38; //amps being used
int Relay1 = 40; //turns on a relay switch when Temp1 goes below 32degrees
int LED1 = A2; //white led lights up when i talk
int RB1 = A9; //red and blue warning led for Temp1
int RB2 = A10; //red and blue warning led for Tank1
int RB3 = A11; //red and blue warning led for Tank2
int RB4 = A12; //red and blue warning led for low Water
int Light1 = A12; //Will control 1156 bulbs inside. or maybe i need digital pin.
int Light2 = A13; //Will control 1156 bulbs inside. and might need some relays.
int Light3 = A14; //Will control 1156 bulbs inside. what did i get myself into?
int Light4 = A15; //Will control 1156 bulb outside.
int val = 0; //not sure what this pin does but its needed
//end of my pins

//Start Code for one Electret mini condenser microphone pins
// these constants won't change:
int ElectretLED1 = A2; // led connected to analog pin 2 will light up when i talk
int Electret1 = A1;//amplifier output connected to analog pin 1
// these variables will change:
int sensorReading = 0;// variable to store the value read from the sensor pin
int sensorMax = 2000;
int sensorMin = 0;
int threshold = 600;
//End Code for the electret microphone

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);  // initialize serial communications
  pinMode(Temp1, INPUT);
  pinMode(Temp2, INPUT);
  pinMode(Tank1, INPUT);
  pinMode(Tank2, INPUT);
  pinMode(Humid1, INPUT);
  pinMode(Humid2, INPUT);
  pinMode(Electret1, INPUT);
  pinMode(Water, INPUT);
  pinMode(Volts, INPUT);
  pinMode(Amps, INPUT);
  pinMode(LED1, OUTPUT);
  pinMode(RB1, OUTPUT);
  pinMode(RB2, OUTPUT);
  pinMode(RB3, OUTPUT);
  pinMode(Relay1, OUTPUT);
  pinMode(Light1, OUTPUT);
  pinMode(Light2, OUTPUT);
  pinMode(Light3, OUTPUT);
  pinMode(Light4, OUTPUT);
  pinMode(ElectretLED1, OUTPUT);
  //end my pinmode code

  //Start Code for Electret1 microphone
  analogWrite(A2, HIGH);
  while (millis() < 3000) {
    threshold = analogRead(Electret1);
    // record the maximum sensor value
    if (threshold > sensorMax) {
      sensorMax = threshold;
    }
  }
  // signal the end of the calibration period
  analogWrite(A2, LOW);
  threshold = sensorMax;
  //End Code for Electret1 microphone

}//end setup


void loop() {

  //two load sensors outside to measure how much propane i have in each 20lb tank
  if (digitalRead(Tank1) <= 17)
  {
    digitalWrite(RB2, HIGH); 
  }//led alerts me when 20lb propane tank 1 is low
  if (digitalRead(Tank2) <= 17)
  {
    digitalWrite(RB3, HIGH); 
  }//led alerts me when 20lb propane tank 2 is low
  if (digitalRead(Tank1) >= 18)
  {
    digitalWrite(RB2, LOW); 
  }//led is off for propane tank 1
  if (digitalRead(Tank2) >= 18)
  {
    digitalWrite(RB3, LOW); 
  }//led is off for propane tank 2
  //end two load sensors outside to measure weight of propane tanks


  //start DS18B20 temperature sensor under camper. 
  if (digitalRead(Temp1) <= 0)
  {
    digitalWrite(Relay1, HIGH); 
  }//turns on heat tape or something to
  //warm up a outside pipe with using the least amount of power at night.
  if (digitalRead(Temp1) >= 1)
  {
    digitalWrite(Relay1, LOW); 
  }//and then shuts it off
  if (analogRead(Temp1) <= 0)
  {
    analogWrite(RB1, HIGH); 
  }//turns on warning LED inside
  if (analogRead(Temp1) >= 1)
  {
    analogWrite(RB1, LOW); 
  }//and shuts LED off
  //end flashing warning led for DS18B20 temperature sensor


  //Start reading the water voltage sensor that i will need help with!
  int sensorValue = digitalRead(22);
  // Convert the digital voltage reading from (11V - 15V) to a percent (0% - 100%)
  float voltage = sensorValue * (11.0 / 15.0);
  Serial.print("Voltage: ");
  Serial.println(voltage); // print out the value you read:
  // end reading the water voltage sensor


  //start electret code
  //read the sensor and store it in the variable sensorReading:
  sensorReading = analogRead(Electret1);    
  // if the sensor reading is greater than the threshold:
  if (threshold>600) {
  (LED1, HIGH);
}
 if (threshold<600) {
  (LED1, LOW); 
}  //end electret code


  //now i will display all sensors onto my laptop somehow. Help?
  //in the future these will be displayed on my droid app.
  Serial.print("Water: "); 
  Serial.print(Water);
  Serial.println("");
  Serial.print("Tank1: "); 
  Serial.print(Tank1);
  Serial.println("");
  Serial.print("Tank2: "); 
  Serial.print(Tank2);
  Serial.println("");
  Serial.print("Temp1: "); 
  Serial.print(Temp1);
  Serial.println("");
  Serial.print("Temp2: "); 
  Serial.print(Temp2);
  Serial.println("");
  Serial.print("Humidity: "); 
  Serial.print(Humid1);
  Serial.println("");
  Serial.print("Volts: "); 
  Serial.print(Volts);
  Serial.println("");
  Serial.print("Amps: "); 
  Serial.print(Amps);
  Serial.println("");
  //end of this stuff. lol

  delay(1000); //this rechecks every 1 second?

}

thanks paul, what you think now?

I think you need to print that code out, and go through the reference page, looking up each function to understand what it does.

You are still analogWrite()ing to non-PWM pins. You are still testing for HIGH or LOW being less than 17. Both are, so the if test is useless.

On the other hand, a load sensor is probably not a digital device, so connecting it to a digital pin is probably not right.

You aren't even close on how to use the DS18B20s. Or the water sensor on the anonymous pin.

my A9 is blinking

i will read the water sensor with a voltage sensor.

PaulS:

thanks paul, what you think now?

I think you need to print that code out, and go through the reference page, looking up each function to understand what it does.

You are still analogWrite()ing to non-PWM pins. You are still testing for HIGH or LOW being less than 17. Both are, so the if test is useless.

On the other hand, a load sensor is probably not a digital device, so connecting it to a digital pin is probably not right.

You aren't even close on how to use the DS18B20s. Or the water sensor on the anonymous pin.

my leds should connect to pwm pins? why?
a propane tank weighs 15lbs empty and 35lbs full. so they cant be both.

my leds should connect to pwm pins? why?

No. And, because they don't, you should not be using the functions for PWM pins.

a propane tank weighs 15lbs empty and 35lbs full. so they cant be both.

What can't be both? The load sensor is probably not a digital device. It needs to be connected to an analog pin. Most likely. Though its hard to be positive, since you have provided no details about them.

Instead of trying to develop the whole application at once, create sketches to test each sensor and confirm that you are getting reasonable data from them.

PaulS:

my leds should connect to pwm pins? why?

No. And, because they don't, you should not be using the functions for PWM pins.

a propane tank weighs 15lbs empty and 35lbs full. so they cant be both.

What can't be both? The load sensor is probably not a digital device. It needs to be connected to an analog pin. Most likely. Though its hard to be positive, since you have provided no details about them.

Instead of trying to develop the whole application at once, create sketches to test each sensor and confirm that you are getting reasonable data from them.

well my project has been in my sig. i thought you knew.

well which direction should my 4.7k resistor go?

//This is my first ever sketch i am writing for my 2650. i might need help.
//This sketch will eventually control my camper with my droid or voice.
//I will worry less about things going wrong with my off grid trailer.
//Worrying can lead to high anxiety. Stress affects you emotionally and physically.

//Start of my pins
int Water = 22; //Voltage sensor for my SeeLevel II water tank sensor strip
int Tank1b = 24; //load sensor pin for propane tank 1 blue wire
int Tank1w = 25; //load sensor pin for propane tank 1 white wire
int Tank2b = 26; //load sensor pin for propane tank 2 blue wire
int Tank2w = 27; //load sensor pin for propane tank 2 white wire
int Humid1 = 28; //pin for humidity sensor inside
int Humid2 = 30; //pin for humidity sensor inside
int Temp1 = 32; //DS18B20 temperature sensor under camper
int Temp2 = 34; //DS18B20 temperature inside camper
int Volts = 36; //battery bank voltage
int Amps = 38; //amps being used
int Relay1 = 40; //turns on a relay switch when Temp1 goes below 32degrees
int Electret1 = A1;//amplifier output connected to analog pin 1
int ElectretLED1 = A2; // led connected to analog pin 2 will light up when i talk
int RB1 = A3; //red and blue warning led for Temp1
int RB2 = A10; //red and blue warning led for Tank1
int RB3 = A11; //red and blue warning led for Tank2
int RB4 = A12; //red and blue warning led for low Water
int Light1 = A12; //Will control 1156 bulbs inside. or maybe i need digital pin.
int Light2 = A13; //Will control 1156 bulbs inside. and might need some relays.
int Light3 = A14; //Will control 1156 bulbs inside. what did i get myself into?
int Light4 = A15; //Will control 1156 bulb outside.
int val = 0; //not sure what this pin does but its needed
//end of my pins

//Start Code for Electret1 mini condenser microphone. these variables will change
int sensorReading = 0;// variable to store the value read from the sensor pin
int sensorMax = 2000;
int sensorMin = 100;
int threshold = 600;
//End Code for the electret microphone

void setup() {
// put your setup code here, to run once:
Serial.begin(9600); // initialize serial communications
pinMode(Temp1, INPUT);
pinMode(Temp2, INPUT);
pinMode(Tank1b, INPUT);
pinMode(Tank1w, INPUT);
pinMode(Tank2b, INPUT);
pinMode(Tank2w, INPUT);
pinMode(Humid1, INPUT);
pinMode(Humid2, INPUT);
pinMode(Electret1, INPUT);
pinMode(Water, INPUT);
pinMode(Volts, INPUT);
pinMode(Amps, INPUT);
pinMode(RB1, OUTPUT);
pinMode(RB2, OUTPUT);
pinMode(RB3, OUTPUT);
pinMode(Relay1, OUTPUT);
pinMode(Light1, OUTPUT);
pinMode(Light2, OUTPUT);
pinMode(Light3, OUTPUT);
pinMode(Light4, OUTPUT);
pinMode(ElectretLED1, OUTPUT);
//end my pinmode code

//Start Code for analog Electret1 microphone
analogWrite(ElectretLED1, HIGH);
while (millis() < 3000) {
threshold = analogRead(Electret1);
// record the maximum sensor value
if (threshold > sensorMax) {
sensorMax = threshold;
}
}
// signal the end of the calibration period
analogWrite(ElectretLED1, LOW);
threshold = sensorMax;
//End Code for Electret1 microphone

}//end setup

void loop() {

//two load sensors outside to measure how much propane i have in each 20lb tank
if (digitalRead(Tank1b) <= 17)
{digitalWrite(RB2, HIGH);}//led alerts me when 20lb propane tank 1 is low
if (digitalRead(Tank2b) <= 17)
{digitalWrite(RB3, HIGH);}//led alerts me when 20lb propane tank 2 is low
if (digitalRead(Tank1b) >= 18)
{digitalWrite(RB2, LOW);}//led is off for propane tank 1
if (digitalRead(Tank2b) >= 18)
{digitalWrite(RB3, LOW);}//led is off for propane tank 2
//end two load sensors outside to measure weight of propane tanks

//start DS18B20 digital temperature sensor under camper.
if (digitalRead(Temp1) <= 0)
{digitalWrite(Relay1, HIGH);}//turns on heat tape or something to
//warm up a outside pipe with using the least amount of power at night.
if (digitalRead(Temp1) >= 1)
{digitalWrite(Relay1, LOW);}//and then shuts heat tape off.
if (digitalRead(Temp1) <= 0)
{digitalWrite(RB1, HIGH);}//turns on warning LED inside
if (digitalRead(Temp1) >= 1)
{digitalWrite(RB1, LOW);}//and shuts LED off
//end flashing warning led for DS18B20 temperature sensor

//Start reading the water voltage sensor that i will need help with!
int sensorValue = digitalRead(22);
// Convert the digital voltage reading from (11V - 15V) to a percent (0% - 100%)
float voltage = sensorValue * (11.0 / 15.0);
Serial.print("Voltage: ");
Serial.println(voltage); // print out the value you read:
// end reading the water voltage sensor

//start analog electret code
sensorReading = analogRead(Electret1); //read the sensor and store it in the variable sensorReading:
// if the sensor reading is greater than the threshold:
if (threshold>200) {(ElectretLED1, HIGH);}
if (threshold<200) {(ElectretLED1, LOW);}
//end electret code

//now i will display all sensors onto the arduino serial monitor.
//in the future these will be displayed on my droid app, a webpage and my laptop. Help?
Serial.print("Water: ");
Serial.print(Water);
Serial.println("");
Serial.print("Tank1: ");
Serial.print(Tank1b);
Serial.println("");
Serial.print("Tank2: ");
Serial.print(Tank2b);
Serial.println("");
Serial.print("Temp1: ");
Serial.print(Temp1);
Serial.println("");
Serial.print("Temp2: ");
Serial.print(Temp2);
Serial.println("");
Serial.print("Humidity: ");
Serial.print(Humid1);
Serial.println("");
Serial.print("Volts: ");
Serial.print(Volts);
Serial.println("");
Serial.print("Amps: ");
Serial.print(Amps);
Serial.println("");
//end of this stuff. lol

delay(1000); //this rechecks every 1 second?

}

TrailerTrash:
well which direction should my 4.7k resistor go?

Please edit your post, select the code, and put it between [code] ... [/code] tags.

You can do that by hitting the # button above the posting area.

Resistors are bidirectional, there is no correct or incorrect direction to wire them up. But I suspect that's not your real question?

And on all those statements like:

if (digitalRead(Tank1b) <= 17)
  {digitalWrite(RB2, HIGH);}//led alerts me when 20lb propane tank 1 is low

A digitalRead(pin#) can only return a value of 0 or 1, you can't use it logically with an if statement testing if it's any value other then 0 or 1. In this case the statement will always say the tank is low. Digital inputs are just on or off, 1 or 0, TRUE or FALSE, +5vdc or 0vdc. Got it?

Lefty

retrolefty:
Resistors are bidirectional, there is no correct or incorrect direction to wire them up. But I suspect that's not your real question?

And on all those statements like:

if (digitalRead(Tank1b) <= 17)

{digitalWrite(RB2, HIGH);}//led alerts me when 20lb propane tank 1 is low





A digitalRead(pin#) can only return a value of 0 or 1, you can't use it logically with an if statement testing if it's any value other then 0 or 1. In this case the statement will always say the tank is low. Digital inputs are just on or off, 1 or 0, TRUE or FALSE, +5vdc or 0vdc. Got it?

Lefty

ut oh, that means i dont have enough pins to do this project.
or
i can connect my Funduino Mega 2560 to my Arduino Mega 2560 thru the ICSP in the center. um no
or
i can cross trade my TX1 and RX2. um maybe
or
communicate wirelessly. um yes

TrailerTrash:

retrolefty:
Resistors are bidirectional, there is no correct or incorrect direction to wire them up. But I suspect that's not your real question?

And on all those statements like:

if (digitalRead(Tank1b) <= 17)

{digitalWrite(RB2, HIGH);}//led alerts me when 20lb propane tank 1 is low





A digitalRead(pin#) can only return a value of 0 or 1, you can't use it logically with an if statement testing if it's any value other then 0 or 1. In this case the statement will always say the tank is low. Digital inputs are just on or off, 1 or 0, TRUE or FALSE, +5vdc or 0vdc. Got it?

Lefty

ut oh, that means i dont have enough pins to do this project.
or
i can connect my Funduino Mega 2560 to my Arduino Mega 2560 thru the ICSP in the center. um no
or
i can cross trade my TX1 and RX2. um maybe
or
communicate wirelessly. um yes

Your stressing me out with those responses. :wink:

Lefty

where do i connect this Waterproof Digital Temperature Sensor Thermal Probe DS18B20?
to a digital pin or analog pin? its a digital sensor.

where do the green and white wires goto? analong pins or digital pins?

is it possible to analogRead a digital pin?

anybody want a job? i got paypal.

TrailerTrash:
is it possible to analogRead a digital pin?

Nope, but as unfair (and stressful) as it may seem any analog pin can be used as a digital pin. :wink:

Lefty

i replaced all digitalreads with analogreads

//This is my first ever sketch i am writing for my 2650. i might need help.
//This sketch will eventually control my camper with my droid or voice.
//I will worry less about things going wrong with my off grid trailer.
//Worrying can lead to high anxiety. Stress affects you emotionally and physically.

//Start of my pins
int Water = 22; //Voltage sensor for my SeeLevel II water tank sensor strip
int Tank1b = 24; //load sensor pin for propane tank 1 blue wire
int Tank1w = 25; //load sensor pin for propane tank 1 white wire
int Tank2b = 26; //load sensor pin for propane tank 2 blue wire
int Tank2w = 27; //load sensor pin for propane tank 2 white wire
int Humid1 = 28; //pin for humidity sensor inside
int Humid2 = 30; //pin for humidity sensor inside
int Temp1 = 32; //DS18B20 temperature sensor under camper
int Temp2 = 34; //DS18B20 temperature inside camper
int Volts = 36; //battery bank voltage
int Amps = 38; //amps being used
int Relay1 = 40; //turns on a relay switch when Temp1 goes below 32degrees
int Electret1 = A1;//amplifier output connected to analog pin 1
int ElectretLED1 = A2; // led connected to analog pin 2 will light up when i talk
int RB1 = A3; //red and blue warning led for Temp1
int RB2 = A10; //red and blue warning led for Tank1
int RB3 = A11; //red and blue warning led for Tank2
int RB4 = A12; //red and blue warning led for low Water
int Light1 = A12; //Will control 1156 bulbs inside. or maybe i need digital pin.
int Light2 = A13; //Will control 1156 bulbs inside. and might need some relays.
int Light3 = A14; //Will control 1156 bulbs inside. what did i get myself into?
int Light4 = A15; //Will control 1156 bulb outside.
int val = 0; //not sure what this pin does but its needed
//end of my pins

//Start Code for Electret1 mini condenser microphone. these variables will change
int sensorReading = 0;// variable to store the value read from the sensor pin
int sensorMax = 2000;
int sensorMin = 100;
int threshold = 600;
//End Code for the electret microphone

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);  // initialize serial communications
  pinMode(Temp1, INPUT);
  pinMode(Temp2, INPUT);
  pinMode(Tank1b, INPUT);
  pinMode(Tank1w, INPUT);
  pinMode(Tank2b, INPUT);
  pinMode(Tank2w, INPUT);
  pinMode(Humid1, INPUT);
  pinMode(Humid2, INPUT);
  pinMode(Electret1, INPUT);
  pinMode(Water, INPUT);
  pinMode(Volts, INPUT);
  pinMode(Amps, INPUT);
  pinMode(RB1, OUTPUT);
  pinMode(RB2, OUTPUT);
  pinMode(RB3, OUTPUT);
  pinMode(Relay1, OUTPUT);
  pinMode(Light1, OUTPUT);
  pinMode(Light2, OUTPUT);
  pinMode(Light3, OUTPUT);
  pinMode(Light4, OUTPUT);
  pinMode(ElectretLED1, OUTPUT);
  //end my pinmode code

  //Start Code for analog Electret1 microphone
  analogWrite(ElectretLED1, HIGH);
  while (millis() < 3000) {
    threshold = analogRead(Electret1);
    // record the maximum sensor value
    if (threshold > sensorMax) {
      sensorMax = threshold;
    }
  }
  // signal the end of the calibration period
  analogWrite(ElectretLED1, LOW);
  threshold = sensorMax;
  //End Code for Electret1 microphone

}//end setup


void loop() {

  //two load sensors outside to measure how much propane i have in each 20lb tank
  if (analogRead(Tank1b) <= 17)
  {digitalWrite(RB2, HIGH);}//led alerts me when 20lb propane tank 1 is low
  if (analogRead(Tank2b) <= 17)
  {digitalWrite(RB3, HIGH);}//led alerts me when 20lb propane tank 2 is low
  if (analogRead(Tank1b) >= 18)
  {digitalWrite(RB2, LOW);}//led is off for propane tank 1
  if (analogRead(Tank2b) >= 18)
  {digitalWrite(RB3, LOW);}//led is off for propane tank 2
  //end two load sensors outside to measure weight of propane tanks


  //start DS18B20 digital temperature sensor under camper. 
  if (analogRead(Temp1) <= 0)
  {digitalWrite(Relay1, HIGH);}//turns on heat tape or something to
  //warm up a outside pipe with using the least amount of power at night.
  if (analogRead(Temp1) >= 1)
  {digitalWrite(Relay1, LOW);}//and then shuts heat tape off.
  if (analogRead(Temp1) <= 0)
  {digitalWrite(RB1, HIGH);}//turns on warning LED inside
  if (analogRead(Temp1) >= 1)
  {digitalWrite(RB1, LOW);}//and shuts LED off
  //end flashing warning led for DS18B20 temperature sensor


  //Start reading the water voltage sensor that i will need help with!
  int sensorValue = analogRead(22);
  // Convert the digital voltage reading from (11V - 15V) to a percent (0% - 100%)
  float voltage = sensorValue * (11.0 / 15.0);
  Serial.print("Voltage: ");
  Serial.println(voltage); // print out the value you read:
  // end reading the water voltage sensor


  //start analog electret code
  sensorReading = analogRead(Electret1); //read the sensor and store it in the variable sensorReading:    
  // if the sensor reading is greater than the threshold:
  if (threshold>200) {(ElectretLED1, HIGH);}
  if (threshold<200) {(ElectretLED1, LOW);}
  //end electret code


  //now i will display all sensors onto the arduino serial monitor.
  //in the future these will be displayed on my droid app, a webpage and my laptop. Help?
  Serial.print("Water: "); 
  Serial.print(Water);
  Serial.println("");
  Serial.print("Tank1: "); 
  Serial.print(Tank1b);
  Serial.println("");
  Serial.print("Tank2: "); 
  Serial.print(Tank2b);
  Serial.println("");
  Serial.print("Temp1: "); 
  Serial.print(Temp1);
  Serial.println("");
  Serial.print("Temp2: "); 
  Serial.print(Temp2);
  Serial.println("");
  Serial.print("Humidity: "); 
  Serial.print(Humid1);
  Serial.println("");
  Serial.print("Volts: "); 
  Serial.print(Volts);
  Serial.println("");
  Serial.print("Amps: "); 
  Serial.print(Amps);
  Serial.println("");
  //end of this stuff. lol

  delay(1000); //this rechecks every 1 second?

}

When are you going to take the advice several pages back and break this problem down into solvable pieces.

Write a sketch to read a load sensor, and print a value to the serial port. One sensor. One value. NOTHING else.

When that works, you will know how to connect a load sensor, and how to read one of them. Expanding that to read more than one is pretty trivial.

Then, put the load sensor(s) away. Get the thermometer out. Google that sensor, and you'll find that there are plenty of examples for connecting it and reading valid data from it. Learn to do that.

When that works, look at using more than one of them, if you need to. When that works, put the thermometers away, with the load sensors.

Tackle the next sensor type. Get it working. Put it away.

When you know how to read from each sensor individually, and how to set pins HIGH or LOW, or to some PWM value, where appropriate, then, and NOT UNTIL THEN, you are ready to tackle your project.

The DS18B20 device uses the "one wire" system. You need to read about that.

i understand now. digital is ones and zeros! and i used to know that too. where my f`ing brain go?
i start all over and post new code next week