Go Down

Topic: Sketch help? Several sensor inputs and outputs, use analog or digital pins? (Read 4084 times) previous topic - next topic

TrailerTrash

my white led dont light when i speak and my blue and red led does not go off after sticking the DS18B20 sensor in snow.
for more info about this project, click my sig.
Code: [Select]
//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 and/or voice.
//I will worry less about things going wrong with my off grid trailer when away.
//Worrying can lead to high anxiety. Stress affects you emotionally and physically.

int Water = 22; //Voltage sensor for my SeeLevel II water tank sensor strip
int Tank1 = 24; //load sensor for propane tank 1
int Tank2 = 26; //load sensor for propane tank 2
int Humid1 = 28; //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 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 Temp2
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 does but its needed
//end of my int pins

//Start Code for one Electret mini condenser microphone pins
// these constants won't change:
const int ElectretLED1 = A2; // white led connected to analog pin 2 will light up when i talk
const 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 = 1023;
int threshold = 1200;
//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(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 electret microphone

}//end setup

//these will read inputs and write them somewhere that can be seen in a droid app.
//and also to be seen on my laptop online when im home or away. i will need help.
void loop() {
val = digitalRead(Temp1);
digitalWrite(Temp1, 0);
val = digitalRead(Temp2);
digitalWrite(Temp2, 0);
val = digitalRead(Humid1);
digitalWrite(Humid1, 0);
val = digitalRead(Water);
digitalWrite(Water, 0);
val = digitalRead(Tank1);
digitalWrite(Tank1, 0);
val = digitalRead(Tank2);
digitalWrite(Tank2, 0);

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

//start DS18B20 temperature sensor under camper.
if (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 (Temp1 >= 1)
{digitalWrite(Relay1, LOW); }//and then shuts it off
if (Temp1 <= 0)
{analogWrite(RB1, HIGH); }//turns on warning LED inside
if (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 ((sensorReading >= threshold)) {
Serial.println(sensorReading-threshold); //Will send only positive and absolute values of waveform        
}//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("");

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

}
Webmaster living off grid in a camper with 300watts of solar panels charging 9 absorbed glass mat 12V batteries. i own about 40 dot coms. i

Nick Gammon

You lost me right here:

Code: [Select]

  val = digitalRead(Temp1);
  digitalWrite(Temp1, 0);
  val = digitalRead(Temp2);
  digitalWrite(Temp2, 0);
  val = digitalRead(Humid1);
  digitalWrite(Humid1, 0);
  val = digitalRead(Water);
  digitalWrite(Water, 0);
  val = digitalRead(Tank1);
  digitalWrite(Tank1, 0);
  val = digitalRead(Tank2);
  digitalWrite(Tank2, 0);


You are reading lots of things into val, and then discarding them.

Code: [Select]

int Temp1 = 42; //DS18B20 temperature sensor under camper

...

//start DS18B20 temperature sensor under camper.
  if (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 (Temp1 >= 1)
  {
    digitalWrite(Relay1, LOW);
  }//and then shuts it off


Temp1 is always going to be 42 (the answer to the meaning of life, BTW) so there is no point checking if it is <= 0.

You need to save the readings into variables (not just "val").

Even if you did:

Code: [Select]

  val = digitalRead(Temp1);


val will be 0 or 1. It won't ever be greater than 1.

You are confusing the pin numbers with the readings you get from the pins.
Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

TrailerTrash


You are confusing the pin numbers with the readings you get from the pins.


well thanks for the reply.
now what?
pin number 42 output is either going to be below freezing 0c or above freezing 1c.
and then i have to figure it out in F degrees cause im in usa
Webmaster living off grid in a camper with 300watts of solar panels charging 9 absorbed glass mat 12V batteries. i own about 40 dot coms. i

Delta_G

We can use the metric system in the US if we want.  That's what freedom is all about.  Nobody making you use Fahrenheit.

TrailerTrash


We can use the metric system in the US if we want.  That's what freedom is all about.  Nobody making you use Fahrenheit.


i just learned Celsius yesterday, so i should let the user choose when i start on my droid code.
Webmaster living off grid in a camper with 300watts of solar panels charging 9 absorbed glass mat 12V batteries. i own about 40 dot coms. i

TrailerTrash

Webmaster living off grid in a camper with 300watts of solar panels charging 9 absorbed glass mat 12V batteries. i own about 40 dot coms. i

UKHeliBob

The first thing to do is to read the sensors properly as has been explained.
This code
Code: [Select]
val = digitalRead(Temp1);
digitalWrite(Temp1, 0);

looks at the pin you have named Temp1  (pin 42), stores it in a variable called val, then sets the same pin to 0 (LOW).  The value read from the pin is never used again and, indeed , is overwritten very soon after and is, therefore, not available.

What are those 2 lines of code, and several other like them actually meant to do ?
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

Arrch


maybe i need the word "const" in front of all my int`s?


The first thing you should do is fix the complete lack of indenting.

TrailerTrash


The first thing to do is to read the sensors properly as has been explained.
This code
Code: [Select]
val = digitalRead(Temp1);
digitalWrite(Temp1, 0);

looks at the pin you have named Temp1  (pin 42), stores it in a variable called val, then sets the same pin to 0 (LOW).  The value read from the pin is never used again and, indeed , is overwritten very soon after and is, therefore, not available.

What are those 2 lines of code, and several other like them actually meant to do ?


i will delete those then
Webmaster living off grid in a camper with 300watts of solar panels charging 9 absorbed glass mat 12V batteries. i own about 40 dot coms. i

TrailerTrash



maybe i need the word "const" in front of all my int`s?


The first thing you should do is fix the complete lack of indenting.


happy now?

Code: [Select]
//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 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 Temp2
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:
const int ElectretLED1 = A2; // led connected to analog pin 2 will light up when i talk
const 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 = 1023;
int threshold = 1200;
//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(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 (Tank1 <= 17)
  {
    digitalWrite(RB2, HIGH);
  }//led alerts me when 20lb propane tank 1 is low
  if (Tank2 <= 17)
  {
    digitalWrite(RB3, HIGH);
  }//led alerts me when 20lb propane tank 2 is low
  if (Tank1 >= 18)
  {
    digitalWrite(RB2, LOW);
  }//led is off for propane tank 1
  if (Tank2 >= 18)
  {
    digitalWrite(RB3, LOW);
  }//led is off for propane tank 2
  //end load sensors

  //start DS18B20 temperature sensor under camper.
  if (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 (Temp1 >= 1)
  {
    digitalWrite(Relay1, LOW);
  }//and then shuts it off
  if (Temp1 <= 0)
  {
    analogWrite(RB1, HIGH);
  }//turns on warning LED inside
  if (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 ((sensorReading >= threshold)) {
    Serial.println(sensorReading-threshold); //Will send only positive and absolute values of waveform         
  }//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?

}
Webmaster living off grid in a camper with 300watts of solar panels charging 9 absorbed glass mat 12V batteries. i own about 40 dot coms. i

PeterH


Code: [Select]

if (Tank1 <= 17)



Tank1 is the pin number of the input. This code just compares the pin number against 17. Obviously that is pointless since the pin number does not change. If you want to read the state of the pin you need to use digitalRead() or analogRead() to get it. In this case you seem to be expecting an analog value so analogRead() is probably what you need here. It would loook like this:

Code: [Select]

if (analogRead(Tank1) <= 17)

Similarly in all the other places where you have used the pin number directly, instead of the value read from that pin.
I only provide help via the forum - please do not contact me for private consultancy.

TrailerTrash

thanks a bundle dude! so i will just paste this to all the mistakes
analogRead( stuff )

how about now?


Code: [Select]
//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 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 Temp2
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:
const int ElectretLED1 = A2; // led connected to analog pin 2 will light up when i talk
const 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 = 1023;
int threshold = 1200;
//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(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 (analogRead(Tank1) <= 17)
  {
    digitalWrite(RB2, HIGH);
  }//led alerts me when 20lb propane tank 1 is low
  if (Tank2 <= 17)
  {
    digitalWrite(RB3, HIGH);
  }//led alerts me when 20lb propane tank 2 is low
  if (analogRead(Tank1) >= 18)
  {
    digitalWrite(RB2, LOW);
  }//led is off for propane tank 1
  if (Tank2 >= 18)
  {
    digitalWrite(RB3, LOW);
  }//led is off for propane tank 2
  //end load sensors

  //start DS18B20 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 it off
  if (Temp1 <= 0)
  {
    analogWrite(RB1, HIGH);
  }//turns on warning LED inside
  if (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 ((sensorReading >= threshold)) {
    Serial.println(sensorReading-threshold); //Will send only positive and absolute values of waveform         
  }//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?

}
Webmaster living off grid in a camper with 300watts of solar panels charging 9 absorbed glass mat 12V batteries. i own about 40 dot coms. i

TrailerTrash

i am gonna go get some snow and goto dollartree.

Code: [Select]
//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:
const int ElectretLED1 = A2; // led connected to analog pin 2 will light up when i talk
const 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 (analogRead(Tank1) <= 17)
  {
    digitalWrite(RB2, HIGH);
  }//led alerts me when 20lb propane tank 1 is low
  if (Tank2 <= 17)
  {
    digitalWrite(RB3, HIGH);
  }//led alerts me when 20lb propane tank 2 is low
  if (analogRead(Tank1) >= 18)
  {
    digitalWrite(RB2, LOW);
  }//led is off for propane tank 1
  if (Tank2 >= 18)
  {
    digitalWrite(RB3, LOW);
  }//led is off for propane tank 2
  //end load sensors

  //start DS18B20 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 it off
  if (Temp1 <= 0)
  {
    analogWrite(RB1, HIGH);
  }//turns on warning LED inside
  if (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, LOW);
}
if (threshold<600) {
  (LED1, HIGH);
}  //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?

}
Webmaster living off grid in a camper with 300watts of solar panels charging 9 absorbed glass mat 12V batteries. i own about 40 dot coms. i


Nick Gammon

@TrailerTrash: Your signature is a bit much. It is distracting when trying to read your posts.
Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

Go Up