Show Posts
Pages: 1 2 [3] 4 5 ... 33
31  Using Arduino / Programming Questions / Re: Static function for interrupts in a class on: January 29, 2014, 09:05:15 am
I brought up this topic because it applies to my hot tub controller project.  I have two Arduinos communicating over I2C.  One Arduino is for the user panel (I2C Slave) which controls an LCD display, and has inputs for some buttons and a rotary encoder.  That's the Arduino this post is about.  Then I have another Arduino (I2C Master) that controls the hot tub heater, pump, bubbler and monitors pressure, temperature and amps. 

The hot tub controller (I2C master), controls the operation of the hot tub. It's reading the sensors and deciding when to turn things on and off.   It sends the water temperature to the user panel Arduino (I2C slave), and it needs to know from the user panel the status of the push buttons and what temperature the user desires.  So over I2C the master is sending and receiving information.

In this project, I create one instance of the class to represent the hot tub in the user panel arduino.  Since there is just one hot tub, there would only be one instance.  There would only be two instances if there were two hot tubs.  But this would probably mess up the I2C since there would be two I2C masters.  So I'd have to change that around so the user panel was the I2C master. If that were the case, then I wouldn't need interrupts on the master and this static function wouldn't be needed. 

32  Development / Other Software Development / Re: Arduino on Xcode Project — Official Thread on: January 22, 2014, 04:17:35 pm
When I create a new project, I see both EmbedXcode and EmbedXcode+ templates.  Is there a way to get rid of EmbedXcode.  I don't think I need it now that I have plus.
33  Using Arduino / Programming Questions / Re: Static function for interrupts in a class on: January 22, 2014, 10:00:59 am
Your class, as it is designed now, is nothing more than a collection of related functions that is not designed to be instanced more than once. As such, it really should not be a class.

I did it this way, using a class, so I could start familiarizing myself with classes and OOP.  I've noticed that it really helps me make my code easier to understand.  Using a bunch of small functions would do the same thing.  Any other ways to do this, other then a class or just a bunch of functions?  I did play around with using the namespace concept in another program.  It seemed like a good way to group things.
34  Using Arduino / Programming Questions / Static function for interrupts in a class on: January 21, 2014, 08:19:10 pm
I have a class that's using I2C communication via the wire.h library.  When I tried to use the Wire.onRequest(A_Function_In_My_Class) to set the function that should be called, I got a "no matching function error".  After some research I figured out I needed to make the function static in my .h file.  Here's a skeleton version of my program that compiles successfully.

// this would be in .h file   ===========================
#include <Wire.h>
const byte SLAVE_ID = 46;
class MyTestClass {
    void begin();
    static void i2cReceiveCmd(int bytesReceived);
    static void i2cSendData();
    static byte _i2cCmd;

// this would be in .cpp file    ===========================
byte MyTestClass::_i2cCmd;

void MyTestClass::begin() {
  Wire.begin (SLAVE_ID);
  Wire.onReceive (i2cReceiveCmd);  // interrupt handler for incoming commands
  Wire.onRequest (i2cSendData);    // interrupt handler to send data to the master when the master requests it

void MyTestClass::i2cSendData() {
  if (_i2cCmd == 1)
  { Wire.write(SLAVE_ID); }

void MyTestClass::i2cReceiveCmd(int bytesReceived)
  _i2cCmd = 1;

// this would be in .ino file  ===========================
void setup(){}
void loop(){}

My full program can be found here:

My full sketch runs just fine.  I'm new to using classes and I never used static functions before.  I was wondering if this is a good way to deal with the scenario of interrupt handlers in a class? 
35  Development / Other Software Development / Re: Arduino on Xcode Project — Official Thread on: January 21, 2014, 03:52:10 pm
I just got a new iMac and installed EmbedXcode+ v126 and Xcode 5.   All my projects have EmbedXcode legacy.  How do I upgrade my projects to work with the new EmbedXcode+
36  Using Arduino / Programming Questions / Re: Multiple definition error [solved] on: January 09, 2014, 08:08:09 am
That fixed it.  Thank you!!
37  Using Arduino / Programming Questions / Multiple definition error on: January 09, 2014, 12:47:17 am
I'm getting some multiple definition errors when I compile and I can't figure out what I'm doing wrong.

Here's the errors I'm getting:

LocalLibrary.cpp.o: In function `HotTubControl':
LocalLibrary.cpp:6: multiple definition of `oneWire'
Hot_Tub_Main.cpp.o:(.bss.oneWire+0x0): first defined here
/Applications/ Disabling relaxation: it will not work with multiple definitions
LocalLibrary.cpp.o: In function `HotTubControl':
LocalLibrary.cpp:6: multiple definition of `oneWireBus'
Hot_Tub_Main.cpp.o:(.bss.oneWireBus+0x0): first defined here

The code is located in GitHub:

I had this working, but I put some of the functions into a class in separate .cpp file and now I get these errors.
38  Using Arduino / Project Guidance / Re: How to short two pins when both are LOW on: December 23, 2013, 10:56:54 pm
Just found this:, USB charging port controller, looks interesting aswell.

That TI TPS2513 chip looks great. I'm planning on building a phone charging station, I think that chip would make life a lot easier. Do you know when to use the TPS2513 verses the TPS2514?  Also in the example circuit they have a TI TPS2561A current limiting power switch.  Would you really need this?  If your power supply had sufficient power, then just let the phone draw as much current as it wants, right?
39  Using Arduino / Microcontrollers / Re: watchdog with the mega does not work? on: December 23, 2013, 12:24:57 pm
I updated my website.
Can you provide a link to this on your website? I'd like to check it out.
40  Using Arduino / Programming Questions / Re: Countdown Timer and makeTime() on: December 06, 2013, 02:21:40 pm
I think I found the problem, the year should be tm.Year = 2013 - 1970, I was using 2013 - 1900.  I'll test it later and confirm.
41  Using Arduino / Programming Questions / Countdown Timer and makeTime() on: December 06, 2013, 09:38:25 am
I'm have a RTC and I want to make a countdown timer.  The time remaining will be displayed on an LCD matrix in hh:mm:ss format.  I'm having trouble figuring out the number of seconds from now to the countdown time.  I apparently lacking in some understanding of how makeTime(), and epochtime() work.

My RTC is set with the right time, I ran one of the example sketches that outputs the time and it was right.  My intent is to set my countdown time using makeTime(), then subtract the now.unixtime().  I thought this would give me the number of seconds until I reached the countdown, but I'm getting numbers that are way too big.

Initially I was setting tm.Year = 2013, then I read it should be 2013 - 1900, but that didn't fix it.  I'm also not sure if tm.Month is 11 for December or 12.    Neither way got things working.

Below is my test sketch

#include <Wire.h>
#include "RTClib.h"
#include "Time.h"   


uint32_t refreshTimer;
tmElements_t tm;

void setup() {
  refreshTimer = millis();

void loop()  {
  time_t nextMooverTime;
  uint32_t countdownSec;
  if ((long) (millis() - refreshTimer) > 0 )
    DateTime now =;
    tm.Second = 0;
    tm.Hour = 10;
    tm.Minute = 30;
    tm.Day = 7;
    tm.Month = 12;
    tm.Year = 2013 - 1900;
    nextMooverTime =  makeTime(tm);
    countdownSec = nextMooverTime - now.unixtime();
   // at 8:55 AM EST on 12/6/13, I get this
   // 3595401000 2209080988 1386320012
   // if I change month to 11, I get this
   // 3592809000 2206488929 1386320071
   // change year to 2013 instead of 2013 - 1900
   // 2708568104 1322247995 1386320109

    refreshTimer = millis() + 1000; // update display every 1/2 second

42  Using Arduino / Networking, Protocols, and Devices / Ethernet Client connections on: November 30, 2013, 07:21:24 pm
I'm trying to get a handle on Ethernet client connections.  I'm using the Ethernet.h library with the standard Ethernet shield (R3).  I've seen this code is several Ethernet examples:
// if there's no net connection, but there was one last time
  // through the loop, then stop the client:
  if (!client.connected() && lastConnected) {

This doesn't seem to work very well for me, I have a sketch (you can see it here: doing an HTTP POST to and frequently this if() statement doesn't go true and doesn't disconnect. 

One thing I don't really understand is the distinction between being connected and client.stop().  If client.connected() is false it means I'm not connected to the server, right?  So why do I still need the client.stop() statement?  I've noticed if the client.stop() is not executed the Ethernet connection can get hung up or something - meaning I try to POST, but it's not successful.

I've seen another example where client.stop() is implemented like this:
while( client.connected() ) {
  while(client.available()) {
    inChar =;
    connectLoop = 0;

  if(connectLoop > 1000) {

This seems more reliable in getting the client.stop() to execute.  I'd like to better understand how all this works and what people recommend for a reliable way to implement HTTP POST. 
43  Using Arduino / Networking, Protocols, and Devices / Re: client.available() returning strange data on: November 26, 2013, 11:52:02 pm
Thanks.  I didn't realize the remaining bytes from client.available() were getting mixed in with the  Serial.print(c); characters.  I see my mistake now.
44  Using Arduino / Networking, Protocols, and Devices / client.available() returning strange data on: November 26, 2013, 08:39:18 pm
I thought client.available() is supposed to return the number of bytes available for reading.  See:
But when I use it like this:

I get stiff like this:

Here's my entire skecth
#include <Ethernet.h>
#include <SPI.h>

long unsigned int time = 0;

byte mac[] = {  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[] =  { 192, 168, 216, 50 };   // IP address of Ethernet Shield

char serverName[] = "";
char url[] = "/pushingbox";
EthernetClient client; // create a client that connects to Google
boolean lastConnected = false; // State of the connection last time through the main loop

void setup()

  Serial.println("Begin Setup");

  Ethernet.begin(mac, ip);
  delay(1000); // give the Ethernet shield a second to initialize
  Serial.print(F("My IP address: "));

void loop()
  byte temp[4];
  static byte cnt;
  if ( millis() > time + 30000)
    client.connect(serverName, 80);

    temp[0] = cnt++;
    temp[1] = 61;
    temp[2] = 92;
    temp[3] = millis();

    char sensorTemp[10];
    char feedData[150];
    strcpy(feedData, "devid=vB************8A");   // removed real devid for forum post
    for(int i=0; i<4; i++)
      sprintf(sensorTemp, "&T%d=%d", i+1, temp[i]);
      strcat(feedData, sensorTemp);
    postRequest(serverName, url, feedData);
    time = millis();


  // print the response from server
  // Should get HTTP/1.1 200 OK
  if (client.available())
    char c =;

  // if there's no net connection, but there was one last time
  // through the loop, then stop the client:
  if (!client.connected() && lastConnected)
  lastConnected = client.connected();


void postRequest(char *hostName, char *url, String feedData)
  String buf = "POST " + String(url) + " HTTP/1.1";
  Serial.println(F("Data sent to server: "));
  Serial.println("Host: " + String(hostName));
  Serial.println("Content-Type: application/x-www-form-urlencoded");
  Serial.println("Content-Length: " + String(feedData.length()));

  client.println("Host: " + String(hostName));
  client.println("Content-Type: application/x-www-form-urlencoded");
  client.println("Content-Length: " + String(feedData.length()));
45  Using Arduino / Sensors / Re: Filter out speakers vibrations from accelerometer on: November 05, 2013, 08:21:05 pm
The accelerometer takes about 1mS to update.  For some reason I thought it was longer.  So I put this Low Pass filter in:

bool ClockAccel::isMoving(uint16_t threshold)
  int currentAccelVal[] = {0,0,0};  // Stores the 12-bit signed value
  int filteredAccelVal[] = {0,0,0};   // filter accelerometer values
  uint16_t maxAxisChange = 0;       // value of the axis with the maximum accelerometer value
  float filterVal = 0.9;    // smooth the incomming data.  O = no smoothing, 1 = infinite smoothing
  for(int k = 0; k < 15; k++)  // take 15 samples
    readAccelData(currentAccelVal);   // Read accelerometer values
    // Filter the valuse.  Numbers added to currentAccelVal try to normalize to zero when still
    filteredAccelVal[0] = ( (currentAccelVal[0] +   48) * (1 - filterVal)) + (filteredAccelVal[0] * filterVal);
    filteredAccelVal[1] = ( (currentAccelVal[1] -   15) * (1 - filterVal)) + (filteredAccelVal[1] * filterVal);
    filteredAccelVal[2] = ( (currentAccelVal[2] - 1000) * (1 - filterVal)) + (filteredAccelVal[2] * filterVal);
  // see which axis had the most change
  for ( int i = 0; i < 3; i++ )
    filteredAccelVal[i] = abs(filteredAccelVal[i]); // make everything positive
    if ( filteredAccelVal[i] > maxAxisChange )
    { maxAxisChange = filteredAccelVal[i]; }

  // If any axis was greater then the threshold then we're moving
  if ( maxAxisChange > threshold )
  { return true; }
  { return false; }

This filter  works very well.  I use 300 for the threshold.  The vibrations from the speaker usually are less then 100.
Pages: 1 2 [3] 4 5 ... 33