Pages: [1]   Go Down
Author Topic: *SOLVED* Executing SimpleTimer from within a library  (Read 1006 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 11
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

This is driving me nutts, I know its doable. But I'm not very familiar with C++ and pointers and stuff. This is my first attempt at writing a library, the code works just fine if I run in directly in the arduino code, so I know its a pointer issue, but I'm having a heck of a time wrapping my head around pointers.

I'm trying to write a library, that user the SimpleTimer library. What it does is simulate a sunset with an RGB strip for my aquarium.

I cant get around  "error: no matching function for call to 'SunSet::setTimer(int, <unresolved overloaded function type>, int)'"
Here is the striped down code showing just what is relavant to the isssue, I hope.

Sunset.h
Code:
#include "SimpleTimer.h"

class SunSet
{
  public:
     void Tick();
  private:
     SimpleTimer _t;
     void SetTimer(int targ);

SunSet.cpp
Code:
void SunSet::SetTimer(int targ){
_t.setTimer((int)(Target_Dur[targ]*60000)/255, Tick, 255); //ERROR popsup on this line
}

void SunSet::Tick(){
    //do stuff here
}

« Last Edit: January 20, 2013, 12:09:32 am by kDodman » Logged

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 211
Posts: 13477
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
(Target_Dur[targ]*60000)/255
Think you have an overflow issue here as intermediate results do not fit in an int.

Logged

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Your SunSet.h code is not complete. What does the rest of it look like?
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 11
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Think you have an overflow issue here as intermediate results do not fit in an int.
Nope works just fine in normal code.

Here is my header file
Code:
#ifndef SunSet_h
#define SunSet_h

#include "Arduino.h"
#include "SimpleTimer.h"

class SunSet
{
  public:
    SunSet(int Rpin,int Gpin,int Bpin, boolean Defaults);
    void SetDefault();
    void SetTarget(int Target, int Red, int Green, int Blue, float Dur);
    void Begin();
    void Update();
    void Tick();
    
float current[4];
  
    int Target_1[4];
    int Target_2[4];
    int Target_3[4];
    
    
  
    
  private:
   void SetTimer(int targ);
    int _tick;
    int pin[4];
    double _Duration;
    float Target_Dur[4];
    float Target_1_tick[4];float Target_2_tick[4];float Target_3_tick[4];
    
    SimpleTimer _t;
    
 
    
};

#endif



And my cpp

Code:
#include "SunSet.h"

SunSet::SunSet(int Rpin,int Gpin,int Bpin, boolean Defaults){
  pinMode(Rpin, OUTPUT);pinMode(Gpin, OUTPUT);pinMode(Bpin, OUTPUT);
  current[1] = 255;current[2] = 255;current[3] = 255;
  pin[1] = Rpin;pin[2] = Gpin;pin[3] = Bpin;
  _tick = 0;
  if(Defaults){
    analogWrite(pin[1], current[1]);analogWrite(pin[2], current[2]);analogWrite(pin[3], current[3]);
    SetDefault();
  }
}

void SunSet::SetDefault(){
  SetTarget(1, 255,150,170,.2); // Target #, Red, Green, Blue, Time minutes
  SetTarget(2, 150,0,150,.2);
  SetTarget(3, 0,0,50,.2);
  
}

void SunSet::SetTarget(int Target, int Red, int Green, int Blue, float Dur){
  int* _Target;
  
  if(Target == 1){_Target = Target_1;}
  else if (Target == 2){_Target = Target_2;}
  else if (Target == 3){_Target = Target_3;}
  Target_Dur[Target] = Dur;
  _Target[1] = Red;_Target[2] = Green;_Target[3] = Blue;
}


void SunSet::Begin(){
  //calculate Target ticks
  for(int x=1;x<4;x++){
    Target_1_tick[x] = (current[x] - Target_1[x])/255;
    Serial.print("Target 1 ");Serial.print(x);Serial.print(" : ");Serial.println(Target_1_tick[x]);
  }
  SetTimer(1);
  
}

void SunSet::Update(){
  _t.run();
}



void SunSet::SetTimer(int targ){
_t.setTimer((int)(Target_Dur[targ]*60000)/255, Tick, 255);
}

void SunSet::Tick(){
  
  _tick ++;  
  //Serial.println(_tick);
   if(_tick <255){//target 1
    
     for(int x=1;x<4;x++){//calculate new current
        current[x] -= Target_1_tick[x];
     }
   }else if(_tick <510){//target 2
     if( _tick == 255){
       SetTimer(2);
        for(int x=1;x<4;x++){
          Target_2_tick[x] = (current[x] - Target_2[x])/255;
          //Serial.print("Target 2 ");Serial.print(x);Serial.print(" : ");Serial.println(Target_2_tick[x]);
        }
     }
      for(int x=1;x<4;x++){//calculate new current
        current[x] -= Target_2_tick[x];
       }
   }else if(_tick <765){//target 3
     if( _tick == 510){
       SetTimer(3);
        for(int x=1;x<4;x++){
          Target_3_tick[x] = (current[x] - Target_3[x])/255;
          //Serial.print("Target 3 ");Serial.print(x);Serial.print(" : ");Serial.println(Target_3_tick[x]);
        }
     }
     for(int x=1;x<4;x++){//calculate new current
        current[x] -= Target_3_tick[x];
     }
   }
 
 
   for(int x=1;x<4;x++){//write each new current to pins
        //Serial.print(" Current ");Serial.print(x);Serial.print(" : ");Serial.print(current[x]);
        analogWrite(pin[x], (int) current[x]);
    }
  
}




Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
    float Target_1_tick[4];float Target_2_tick[4];float Target_3_tick[4];
Is your enter key broken? One statement per line!

What does your sketch look like?
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 11
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset


Quote
Is your enter key broken? One statement per line!
.

Just makes for less code to scroll through
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Just makes for less code to scroll through
So does not posting the sketch. But, if you really want help, ...
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 11
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I got it figured.
Tracked down a modified simpletimer.h that uses a deligate system
http://arduino.cc/forum/index.php?topic=86758.0

Just had to modify the new simpletimer code for arduino 1.0 by changing Wprogram.h to Arduino.h

Very good solution, no help whats so ever from you guys, especial Mr. PaulS there.
Logged

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 211
Posts: 13477
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Good to hear it is solved,

To keep the library backwards compatible you could use this construct
Code:
#if defined(ARDUINO) && ARDUINO >= 100
  #include "Arduino.h"
#else
  #include "WProgram.h"
#endif
This way the compiler can select the right #include based upon the Arduino version.

Similar code constructs exists based upon board type.

Logged

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Very good solution, no help whats so ever from you guys, especial Mr. PaulS there.
Well, you're welcome.

You know, in any given situation when you have a problem, you can hope that someone else has figured out a solution, or you can learn to fix it yourself.

In this case, you got lucky, and someone else had solved the problem for you. But, you didn't learn squat.

Next time, you'll be up the creek and still won't have a paddle.

What we were trying to do was to teach you how to make a paddle, instead of buying one off ebay.

If you are not interested in learning to make a paddle, fine. Saying that up front would have had a lot of us just ignoring you, instead of wasting your time.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 11
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I did learn exactly how to solve my problem, just not from this thread. I do not just take open source code and cut paste and forget, I try to understand it, and almost always modify it to my particular needs. When I come across this problem again, and I'm sure I will, I will simply incorporate the fast delegate system, so I have learned exactly what I needed to learn.
I greatly appreciate any help I can find, and constructive criticism is always welcomed. I don't want to bash this forum, as searching it has helped me with a lot of issues, I did find the solution on this forum. However, as paul pointed out, I did learn nothing from this thread, I do not see that as my fault. I know the standard is one statement per line, and if I were to post full functioning code I would standardise it and comment the heck out of it, and thank you for that tip, you could be abit more polite in pointing it out tho. I would like to make a quick point, that irrelevant comments also tend to get a thread side tracked, difficult to follow and help no one.
I rarely post on forums in general, because someone else has always had a similar problem, and google can always find it. So why duplicate an issue. In this case I was getting frustrated and decided to ask for help, that only lead to more frustration.
Logged

Temple, Texas
Offline Offline
Sr. Member
****
Karma: 14
Posts: 360
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

IMO.  You posted a well formed Initial Problem Statement and supplied almost all the needed information.  Your code was well written and formatted.  By and large you followed the posting guidelines.  Much much better than many questions this forum receives!!!  Even those that contribute mightily to a forum or industry cannot speak for "all of us" in it.  Glad you got it working!

Cheers,
John
Logged

Pages: [1]   Go Up
Jump to: