Back to Parent

//int led = D0;
int s_avg=5; //default spending average will be 5.
//IMPORTANT High spending will result in low s_avg values, and vice-versa.
//Setting up the Pins----------------------------
int pumpIn = A2; //This direction pumps water into the fish tank and out of the balancing tank
int pumpOut = A0; //This direction pumps water out of the fish tank into the balancing tank
int sensePin = A5; //connected to the depth sensor
///int potePin = A1; //the pin for reading the potentiometer, this is for diagnostics only

//Setting up the Variables-----------------------
///int poteSense = 0; //get the number for the potentiometer, used for debugging
int waterSense = 0; //level of water as returned by the water senser
int waterLevel = 0; // variable to store the water level

void setup() {
  Serial.begin(9600);
  //pinMode(led, OUTPUT);
  Particle.function("newData", handleData);
  pinMode(pumpIn, OUTPUT);
  pinMode(pumpOut, OUTPUT);
  pinMode(sensePin, INPUT);
  //Particle.variable("user_budget", budget , &INT );
  //Particle.variable("spending", spendingdaily , &INT);
  //pinMode(potePin, INPUT); //Potentiometer used for debugging
Serial.begin(9600);

digitalWrite(pumpIn, LOW);
digitalWrite(pumpOut, LOW);
}
void loop() {
//-----------------------------Debugging code please ignore--------
  //digitalWrite(led, HIGH);
  //delay(500);
  //digitalWrite(led, LOW);
  //delay(sec);
  //delay(200);
  //Serial.println(sec);
  //poteSense = analogRead(potePin);
  //poteSense = map(poteSense, 0, 4000, 0, 10);
  //Serial.print("petentiometer, ");
  //Serial.println(poteSense);
//--------------------------------------------------------
  waterSense = analogRead(sensePin);
  //Serial.println(waterSense);
delay(1);
//delay(500);
//waterLevel = 0;
//----------------------------------------------
  //mapping the waterSense to the water level
  if (waterSense > 0 && waterSense < 415)
  { waterLevel = 1; }
  if (waterSense > 416 && waterSense < 430)
  { waterLevel = 2; }
  if (waterSense > 431 && waterSense < 460)
  { waterLevel = 3; }
  if (waterSense > 461 && waterSense < 500)
  { waterLevel = 4; }
  if (waterSense > 501 && waterSense < 600)
  { waterLevel = 5; }
  if (waterSense > 601 && waterSense < 700)
  { waterLevel = 6; }
  if (waterSense > 701 && waterSense < 900)
  { waterLevel = 7; }
  if (waterSense > 901 && waterSense < 1200)
  { waterLevel = 8; }
  if (waterSense > 1201 && waterSense < 2000)
  { waterLevel = 9; }
  if (waterSense > 2001 && waterSense < 4000)
  { waterLevel = 10; }
  Serial.print("waterLevel, ");
  Serial.println(waterLevel);
  //-----------Pump out the water--------------------
if (s_avg < waterLevel)
  {
   digitalWrite(pumpOut, HIGH);
   delay(1000);
  }
  else
  {
    digitalWrite(pumpOut, LOW);
  }

//-----------Pump in the water--------------------
if (s_avg > waterLevel)
  {
   digitalWrite(pumpIn, HIGH);
   delay(1000);
  }
  else
  {
    digitalWrite(pumpIn, LOW);
  }

//-----------DON'T pump the water--------------------
if (s_avg == waterLevel)
  {
   digitalWrite(pumpIn, LOW);
   digitalWrite(pumpOut, LOW);
   delay(1000);
  }
//delay(1000);
//if (4001 < waterSense < 4095)
//{ waterLevel = 10; }
//--------------------------
}
//tells the function that it takes in a string called command
int handleData(String command)
{
Serial.print( "Received = ");
Serial.println( command );
s_avg = command.toInt();
Particle.publish("Average Spending", "&s_avg");
// if the first row is an decimal number and we wanted to extract
// it from the string.. we would do this.
// find the index of the first comma in the string
/*int spacePos = command.indexOf(",");*/
// create a substring from the first character to the first comma
// and convert that to a floating point (decimal number).
/*float spend = command.substring(0, spacePos ).toFloat();*/
// Return 1 to say completed successfull
return 1;
}
Click to Expand

Content Rating

Is this a good/useful/informative piece of content to include in the project? Have your say!

0