The Planted Tank Forum - View Single Post - Hunter X's Arduino Whole Tank Controller (52K Warning)
View Single Post
Old 04-09-2014, 01:05 AM   #168
bigd603
Planted Member
 
PTrader: (1/100%)
Join Date: May 2011
Location: Portland, OR
Posts: 258
Default

Hey Guys,

SO I have been making a lot of good progress on my project (after not doing anything for far too long), and I have got the thing online, its serving data to a website hosted on a raspberry pi (check it out here, but keep in mind its still very early in development), and the site has graphs fed from Xively, but im having some issues with some timing aspects of my sketch. I want the arduino to to feed the data to Xively every ten seconds without using a delay command so the rest of the sketch doesn't get held up in the mean time. I'm using the millis method from the Blink Without Delay example. It works well for a while, but after several hours it stops the feed until i reset the Arduino. I think it has something to do with the millis crossing over, but I don't really know how to fix it. Anyone have any ideas? Here's my sketch:


Code:
#include <Wire.h>
#include <Time.h>
#include <DS1307RTC.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#include <Adafruit_MotorShield.h>
#include "utility/Adafruit_PWMServoDriver.h"
#include <Xively.h>
#include <SPI.h>
#include <Ethernet.h>
#include <HttpClient.h>


// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = { 
  0x90, 0xA2, 0xDA, 0x0D, 0x26, 0x4E };
//IPAddress ip(192,168,0,14);

// Initialize the Ethernet server library
// with the IP address and port you want to use 
// (port 80 is default for HTTP):
EthernetServer server(80);



// Create the motor shield object with the default I2C address
Adafruit_MotorShield AFMS = Adafruit_MotorShield(); 
// Or, create it with a different I2C address (say for stacking)
// Adafruit_MotorShield AFMS = Adafruit_MotorShield(0x61); 

// Select which 'port' M1, M2, M3 or M4. In this case, M1
Adafruit_DCMotor *macroPump = AFMS.getMotor(1);
// You can also make another motor on port M2
Adafruit_DCMotor *microPump = AFMS.getMotor(2);

//THIS SECTION TELLS ARDUINO THE ADDRESS OF THE TEMP SENSORS
#define ONE_WIRE_BUS 2 //temp is coming from pin 2
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
DeviceAddress testProbe = { 
  0x28, 0xD5, 0xD7, 0xAA, 0x02, 0x00, 0x00, 0xC3 };
DeviceAddress testProbe2 = { 
  0x28, 0x05, 0x02, 0x84, 0x04, 0x00, 0x00, 0x3F };
float tempTest;
float tempTest2;
//END OF TEMP SENSORS SECTION

/////XIVELY
// Your Xively key to let you upload data
char xivelyKey[] = "8Ot0JKtOfqg7yLaG9QhNN5s3HSqbPl9WnO7c4wrBptpiq6ER";
//your xively feed ID
#define xivelyFeed 835109294
//datastreams
char sensorID[] = "TankTemp";
char resID[] = "ResTemp";

 
// Define the strings for our datastream IDs
XivelyDatastream datastreams[] = {
  XivelyDatastream(sensorID, strlen(sensorID), DATASTREAM_FLOAT),
  XivelyDatastream(resID, strlen(resID), DATASTREAM_FLOAT),
};
// Finally, wrap the datastreams into a feed
XivelyFeed feed(xivelyFeed, datastreams, 2 /* number of datastreams */);
 
EthernetClient client;
XivelyClient xivelyclient(client);

//timing for sending Xively data
unsigned long lastXivelyPut = 0; //stores the last time Xively sent data
unsigned long putInterval = 10000;  //interval at which to send data



unsigned char doseTime;

void setup() {
  Serial.begin(9600);           // set up Serial library at 9600 bps
  
   Ethernet.begin(mac/*, ip*/);
  server.begin();
  
  setSyncProvider(RTC.get); //Retrieves time from the RTC
   

  AFMS.begin();  // create with the default frequency 1.6KHz
  //AFMS.begin(1000);  // OR with a different frequency, say 1KHz
  
  // Set the speed to start, from 0 (off) to 255 (max speed)
  macroPump->setSpeed(150);
  macroPump->run(FORWARD);
  // turn on motor
  macroPump->run(RELEASE);//Turn off motor
  
  microPump->setSpeed(150);
  microPump->run(FORWARD);
  // turn on motor
  microPump->run(RELEASE);
}

void loop() {
  uint8_t i;
  unsigned long currentMillis = millis();
  
  sensors.requestTemperatures();
  tempTest = (sensors.getTempF(testProbe));
  tempTest2 = (sensors.getTempF(testProbe2));
  
  int TankTemp = (sensors.getTempF(testProbe));
  datastreams[0].setFloat(TankTemp);
  
  int ResTemp = (sensors.getTempF(testProbe2));
  datastreams[1].setFloat(ResTemp);
  
   
  
  if (currentMillis - lastXivelyPut > putInterval) {
    lastXivelyPut = currentMillis;
    Serial.println("Uploading it to Xively");
    int ret = xivelyclient.put(feed, xivelyKey);
  }
  
  
  if (hour() == 7 && minute() == 46 && second() == 0) {
      doseTime = second();
    macroPump->setSpeed(155);
    macroPump->run(FORWARD);
    }
  if (second() >= (doseTime + 2)) {
        macroPump->run(RELEASE);
      }
      
    if (hour() == 13 && minute() == 5 && second() == 0) {
      doseTime = second();
    microPump->setSpeed(155);
    microPump->run(FORWARD);
    }
  if (second() >= (doseTime + 2)) {
        microPump->run(RELEASE);
      }
}
bigd603 is online now   Reply With Quote