Loading...
  Show Posts
Pages: 1 ... 7 8 [9] 10
121  Forum 2005-2010 (read only) / Exhibition / hot water system datalogger on: January 24, 2011, 12:10:13 am
Hey guys, I just thought I'd post my largest sketch yet (11714 bytes smiley-grin) that I've written that both compiles and does what I want. you know, probably. I haven't tested it in the field yet, but initial reports come back good! I put it into all different tabs, so I guess I'll paste one below the other? ah well.

Also, I'll let you know that I commented the code for the first time especially for this, I wouldn't have otherwise. smiley
As I commented it, I noticed some stuff that could be better (bottom/top factoring etc) so I changed it, so no promises that it'll work.

Anyway, the purpose of this is to monitor my house's hot water system. We have some solar collectors up on the roof that heat our water with a backup regular heating element if it's cloudy for a couple days or something. I (my dad) thought it would be fun to monitor the system, see when stuff is on & off, see what temperature it is, and it would be so easy to do w/ arduino right? anyway, a year and a bit later, here's the finished code, fingers crossed. What it does is connects to a flash drive with a VDIP1 module and logs the times when two heat pumps and a heating element turn on and off and also logs the temperatures of two water pipes every five minutes. The heat pumps are both at 24VAC so I threw a diode in line with each (I might make a full wave rectifier if I feel like being productive) and voltage dividers to step it down. I'll probably end up changing it from a digitalRead to an analogRead because it'll pick up the AC better. The heating element is 240VAC so I just stuck a cell charger in there that brings it down to 5v and I'm reading that. I'm using two LM35s for the temperatures with the same setup as I wrote in a playground page a while back... http://www.arduino.cc/playground/Main/LM35HigherResolution , basically just using internal analog reference.

I found that working with Strings with the VDIP1 generally saved my some headaches, so that's why they're all over the place in the code. I dunno if they actually make it better, but it gave me better results. Also, let me say something about the VDIP1. I HATE IT. IT'S THE WORST. I picked up electronics about a year and a half ago and the VDIP1 has definitely been the most infuriating part of it. If you're going to go with datalogging, I'd go with an SD card or something, just because the VDIP1 is so darn picky in my experience.

Code:
#include <Time.h>
#include <NewSoftSerial.h>

NewSoftSerial mySerial(3, 4); // serial w/ the VDIP1 module

boolean state = LOW; //State of heating element
boolean state1 = LOW; // state of heat pump 1
boolean state2 = LOW; // state of heat pump 2
boolean stateElem = LOW; //pretty sure this one is the previous state of the heating element
int previous = 0; // previous logtime, it goes every five minutes for the temperatures
boolean LED_STATE = LOW; //not used anymore I don't think but left in anyway

int pPin1 = 11; //heat pump 1
int pPin2 = 12; // heat pump 2
int tPin1 = 0; // temp 1
int tPin2 = 1; // temp 2
int elementPin = 10; //you can guess this one

//int RTSpin = 9;
// RTS may be implemented later when it screws up for lack of handshaking :)

void setup()
{
  pinMode(pPin1, INPUT); //you can guess these
  pinMode(pPin2, INPUT);
  pinMode(elementPin, INPUT);  
  pinMode(13, OUTPUT); //LED pin if I want it
  
  Serial.begin(9600); //for debugging/monitoring
  
  mySerial.begin(9600); //connection to the VDIP1 module
  mySerial.print("IPA"); //tell it I want to work in ASCII
  mySerial.print(13, BYTE);
  Serial.println("Setupdone"); //the standard feedback line
  analogReference(INTERNAL); // I use the internal for higher resolution w/ the LM35s, see my playground page
}

void loop()
{
  int incoming = 0;
  if (Serial.available()) {incoming = Serial.read();} // receive incoming data from the computer
  
  if (incoming =='1')
  {
    mySerial.print("DIR"); //tell the VDIP1 to spit out everything on the usb disk
    mySerial.print(13, BYTE);
    incoming = '0';
  }  

if(incoming == '2') // print out all the states of the pins
{
 
  Serial.print(digitalRead(pPin1));
  Serial.print(",");
  Serial.print(digitalRead(pPin2));
  Serial.print(",");
  Serial.print(digitalRead(elementPin));
  Serial.print(",");
  Serial.print(analogRead(tPin1));
  Serial.print(",");
  Serial.print(analogRead(tPin2));
  Serial.print("   ");
  Serial.println(getTime());
  incoming = '0';
  
}


  while (mySerial.available()) { // spits back responses from VDIP1 to computer.
    char letter = (char)mySerial.read();
    if(letter == '>')
    Serial.println(letter);
    else
    Serial.print(letter);
  }
  
  temps();
  element();
  heatPumps();
  
//  digitalWrite(13, LED_STATE);
 
}

String getTemp(int pin)
{
  int reading;
  for(int i = 0;i<10;i++) // average the readings to get rid of noise
  {
    reading += analogRead(pin);
  }
  reading = (float)reading / 93.1;
  String temp = String(reading, DEC);
  return temp;
}

String getTime() // Strings FTW
{
  String time = String(month());
  time = time.concat("/");
  time = time.concat(day());
  time = time.concat("/");
  time = time.concat(year());
  time = time.concat(" ");
  time = time.concat(hour());
  time = time.concat(":");
  if(minute() < 10)
  {
    time = time.concat("0"); // for format like 9:05 instead of 9:5
  }
  time = time.concat(minute());
  return time;
}

void timeStamp(String time) // used for timestamping data sent to VDIP1
{
  mySerial.print("WRF ");
  mySerial.print(time.length() + 2);
  mySerial.print(13, BYTE);
  mySerial.print(" ");
  mySerial.print(time);
  mySerial.print(13, BYTE);
}
boolean readAC(int pin) // I have to see if a half-rectified AC wave is present
{
  for(int i = 0;i< 20;i++)
  {
    if(digitalRead(pin) == HIGH)
    {
      return HIGH;
    }
    else
    {
      delay(1);
    }
  }
  return LOW;
}

String dToS(double x) // changing a double to a string
{
  int y = ((int)x)*10000; // I probably could have used % here but I didn't know it then
  int z = (10000*x) - y;
  String foo = String(y/10000);
  foo = foo.concat(".");
  foo = foo.concat(z);
  return foo;
}

boolean readDigital(int pin) // make sure it's not noise
{
  boolean vals[10]; // I did this because it would register when I touched the arduino for some reason.
  for(int i = 0; i < 10; i++)
  {
    vals[i] = digitalRead(pin);
    delay(100);
  }
  for(int i = 0;i < 10; i++)
  {
    if(vals[i] == LOW)
    {
      return LOW;
    }
  }
  return HIGH; // only return high if all of the readings over a second are high
}

void element()
{
  state = readDigital(elementPin);
  if(state != stateElem) // if the state changed from before
  {
    // LED_STATE = !LED_STATE;
    mySerial.print("OPW ELEMENT.TXT"); //logging data
    mySerial.print(13, BYTE);
    if(state == HIGH)
    {
      mySerial.print("WRF 5");
      mySerial.print("On at");
      timeStamp(getTime());
      stateElem = true;
     }
  
    if(state == LOW)
    {
      mySerial.print("WRF 6");
      mySerial.print("Off at");
      timeStamp(getTime());
      stateElem = false;
     }
     mySerial.print("CLF ELEMENT.TXT");
     mySerial.print(13, BYTE);
     Serial.print("element!   ");
     Serial.println(minute());
  }
}


void heatPumps()
{
  
  state = readAC(pPin1);
  
  if(state != state1)
  {
 //   LED_STATE = !LED_STATE;
  mySerial.print("OPW HEATPUMP1.TXT");
  mySerial.print(13, BYTE);
  
  if(state == HIGH)
  {
    mySerial.print("WRF 5");
    mySerial.print("On at");
    timeStamp(getTime());
    state1 = true;
  }

  if(state == LOW)
  {
    mySerial.print("WRF 6");
    mySerial.print("Off at");
    timeStamp(getTime());
    state1 = false;
   }
  
   mySerial.print("CLF HEATPUMP1.TXT");
   mySerial.print(13, BYTE);
  
   Serial.print("heatpumps!   ");
   Serial.println(minute());

  }
  
  state = readAC(pPin2);

  if(state != state2)
  {
  //  LED_STATE = !LED_STATE;
    mySerial.print("OPW HEATPUMP2.TXT");
    mySerial.print(13, BYTE);
    
    if(state == HIGH)
    {
      mySerial.print("WRF 5");
      mySerial.print("On at");
      timeStamp(getTime());
      state2 = true;
    }
    if(state == LOW)
    {
     mySerial.print("WRF 6");
     mySerial.print("Off at");
     timeStamp(getTime());
     state2 = false;
     }
     mySerial.print("CLF HEATPUMP2.TXT");
     mySerial.print(13, BYTE);
    
     Serial.print("heatpumps!   ");
     Serial.println(minute());
  }
}

void temps()
{
  if(minute() - previous >=5)
  {
   String temp1= dToS(analogRead(tPin1) / 9.31); // convert voltage to temperature
   String temp2= dToS(analogRead(tPin2) / 9.31); // see my playground page
 
   mySerial.print("OPW TEMPS.TXT");
   mySerial.print(13, BYTE);
   mySerial.print("WRF ");
   mySerial.print(temp1.length()+temp2.length()+1);
   mySerial.print(13, BYTE);
   mySerial.print(temp1);
   mySerial.print(",");
   mySerial.print(temp2);
   previous = minute();
   timeStamp(getTime());
   mySerial.print("CLF TEMPS.TXT");
   mySerial.print(13, BYTE);
   Serial.print("temps!   ");
   Serial.println(String(minute()));
  }
}


phew! Comments, questions, feedback, etc? After all this work, I'd like some sort of validation from people who know more than zip about programming, from my fam it's just "oh that's nice all those little brackets and stuff"
122  Forum 2005-2010 (read only) / Frequently-Asked Questions / Re: piggybacked arduinos on: January 11, 2011, 04:36:59 pm
roar. While I'm no god member, I do understand that it isn't the best solution by a long shot. I was wondering if anyone had tried it out.

I agree though, when I first got into arduino I didn't have the idea that pins shouldn't be pushed too hard firmly implanted in my head. I understood ohm's law etc and how electricity worked, but it didn't transfer to what will smoke a chip and what wouldn't.
123  Forum 2005-2010 (read only) / Frequently-Asked Questions / Re: piggybacked arduinos on: January 11, 2011, 12:08:38 pm
Hmm. I didn't want to use this for something, I was just curious what would happen.

Quote
Oops  Smiley

 ;D
124  Forum 2005-2010 (read only) / Frequently-Asked Questions / Re: piggybacked arduinos on: January 10, 2011, 11:55:39 pm
Yeah. I've been coming up with ways of getting around sync issues (program each separately, have a serial connection keeping them in sync, etc), but eventually it comes to the point where it's much easier to just have pop in some transistors where necessary.
125  Forum 2005-2010 (read only) / Frequently-Asked Questions / piggybacked arduinos on: January 10, 2011, 11:37:47 pm
I just had a brainwave. Everybody always complains about the 40mA limit on I/O pins, so could one plop a second chip on top of the original and have twice the current / pin? Presumably the timing would be the same and everything, perhaps uploading code might screw it up. Has anyone done this, and if not, why not?

kevin
126  Forum 2005-2010 (read only) / Frequently-Asked Questions / Re: What's going on behind the scenes? on: December 14, 2010, 11:27:40 pm
ok, to the wikipedia. I went from flash memory -> floating gate mosfet and I just wanted to be sure that I'm getting the general idea. So a floating gate mosfet is sort of a capacitor and a transistor combined? It gets a charge and then keeps it until the next time?

Now this has raised some more questions. So the Uno for instance has 32K of flash, 2K of SRAM, and 1K of EEPROM. I've used the EEPROM before and get the idea behind that. I'm thinking the sram is used for variable storage etc during execution and the flash would hold the code itself. So what happens if the sram gets full? would it lock up, maybe start overwriting old variables, or start filling up something else perhaps?

Quote
PORTB |= (1<<PB5)

oof. not good. However, I do like the 40 times faster bit. I seem to recall from my browsing that the digitalWrite command actually checks that the pin is indeed the state you specified? Can you use code like PORTB |= (1<<PB5) mixed in with 'normal' code, like so?

Code:
digitalWrite(pin, state);
PORTB |= (1<<PB5);
for(int i = 0;...

I think I saw an instructable the other day about making one's own flash storage from transistors, I may have a look at that and see if I can reason out some facts.
127  Forum 2005-2010 (read only) / Frequently-Asked Questions / Re: What's going on behind the scenes? on: December 14, 2010, 10:37:45 pm
ok, that all makes sense. I think the biggest thing I don't understand is the leap from machine language to a blinking LED. To my understanding, a chip like the atmega 328 is pretty much a zillion transistors with maybe some other stuff too (like what, an oscillator/resonator/whatever?), so I guess the hex loaded onto the chip is stored in the transistors themselves? However, this cannot be the case because the transistors would reset whenever power is lost. I guess the main thing is just how do the internals of a chip store/execute a program?
128  Forum 2005-2010 (read only) / Frequently-Asked Questions / What's going on behind the scenes? on: December 14, 2010, 08:20:01 pm
So all of you god members out there seem to know everything ever about everything ever. I want this.  ;D  However, I would be content with knowing how arduino code (eg "digitalWrite(ledPin, HIGH));") gets turned into whatever the heck is going on in the chip itself. I understand it goes through a lot of processing on the way, but what specifically happens to it? What is the language the chip runs called? What does it look like? Is it possible to write that language directly?

Also, what is the name of the arduino language? I know it's based on wiring etc, but is it just 'arduino'?
129  Forum 2005-2010 (read only) / Frequently-Asked Questions / Re: water leak sensor on: October 22, 2010, 06:32:28 pm
if it isn't in salt water, you could always include a small payload of salt scattered around the bottom   ;D
130  Forum 2005-2010 (read only) / Frequently-Asked Questions / Re: Extremely low power consumption on: September 11, 2010, 12:00:10 pm
okay, that's good to know. how exactly do you put the arduino to sleep? Is there some command, or do you do something special with a pin or something? I'd just as soon turn the arduino off instead of putting it to sleep, if the two are not the same thing. I'm actually planning on running this off of either a 9 volt or AAs.

So a retigerable one shot turns a pulse into a constant signal? kind of like a switch? So the RTC sends out a pulse, the retrigerable one shot receives it and puts out a constant signal to the switching regulator, which powers the arduino. Then, the RTC sends out another pulse, the one shot receives it and kills the signal, the regulator turns off the arduino. Correct?

I'm also unsure of how an RTC works. Is it standard for them to just put out a pulse every second?

Thanks!
131  Forum 2005-2010 (read only) / Frequently-Asked Questions / Re: Extremely low power consumption on: September 10, 2010, 10:31:36 am
I didn't know you could use an arduino without providing exactly 5 volts. If I use rechargeables AAs, 1.2 * 4 is 4.8 volts. Would that damage the arduino at all? Also, can you program an RTC to give a pulse every X seconds?

As for a switching supply, I assume that that is a power supply that turns on or off when it detects a pulse? In that case, the RTC would give it a pulse at t = 0, the arduino turns on, then maybe the RTC (or arduino, I guess) sends another pulse at t = .1 seconds or so to turn the arduino off. The cycle then repeats?
132  Forum 2005-2010 (read only) / Frequently-Asked Questions / Re: Extremely low power consumption on: September 09, 2010, 11:01:16 pm
agg. tab plus enter means bad news. oh well.

I've read this:
-the voltage regulator uses up unnecessary power
-the chip can be put into a 'sleep' mode, and brought back by a reset or an interrupt (shaky on the meaning of interrupt)
-you can use an external device to turn the arduino on for a bit and then off for a bit
-something about pins and pullup resistors

my project requires that the arduino check a sensor every five seconds or so and possible store a time to eeprom. It can be off the rest of the time, assuming eeprom is stable without power (yes?). Would a 555 timer be able to do this, or an RTC? I haven't used either, so I dunno.

Any information regarding reducing power consumption would be excellent. Thanks!
133  Forum 2005-2010 (read only) / Frequently-Asked Questions / Extremely low power consumption on: September 09, 2010, 10:57:36 pm
Hi all, I'm making a project that needs to use as little power as possible. I've read that most of the power consumption of a project is the sensors etc (not the arduino itself), but I can't really change that very much. I've read the following concerning power consumption:
134  Forum 2005-2010 (read only) / Frequently-Asked Questions / Re: 5.9v wall souce to digital in of Arduino on: April 01, 2010, 12:47:20 pm
Great, thanks a ton!
135  Forum 2005-2010 (read only) / Frequently-Asked Questions / Re: 5.9v wall souce to digital in of Arduino on: April 01, 2010, 12:32:57 pm
Awesome! My usage doesn't really need to be fast, so long as it doesn't take ten minutes to decide. But, I do have a bunch of 10K resistors laying around so that's not a problem. My last question: what does ADC mean? I'm guessing it's either Arduino Digital Current or Analog Digital Current.
Pages: 1 ... 7 8 [9] 10