Go Down

Topic: *SOLVED* Executing SimpleTimer from within a library (Read 1 time) previous topic - next topic

kDodman

Jan 19, 2013, 09:07 pm Last Edit: Jan 20, 2013, 06:09 am by kDodman Reason: 1
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: [Select]

#include "SimpleTimer.h"

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


SunSet.cpp
Code: [Select]

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
}


robtillaart

Quote
(Target_Dur[targ]*60000)/255

Think you have an overflow issue here as intermediate results do not fit in an int.

Rob Tillaart

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

PaulS

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

kDodman

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: [Select]
#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: [Select]
#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]);
   }
 
}





PaulS

Code: [Select]
    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?

Go Up