Getting Button Threaded controlled from a Controller

Hello

I do have 2 Classes. I´m quiet new to it so please dont be so hard with me. Sorry for the german named Variables. I didnt knew that I would need help.

As you maybe can see, its a simple traffic light "Controller.h", that schedules in initializes my lights and Buttons.

What i want to to know is implementing the Button. And I want it to run the getButtonState() in a Thread(); controlled from the Controller.

EDIT:

when i try to create a Thread(); in the controller class i have this code:
but on bt.onRun i always get:

Invalid arguments 'Candidates are:void onRun(void (*)())'

void Controller::_buttonThread(){
 Thread bt = Thread();
 bt.enabled = true;
 bt.onRun(_readButton);
}

MY Controller looks like this;

#include "Arduino.h"
#include "Ampel.h"
#include "FussAmpel.h"
#include "Button.h"
#include "Lamp.h"
#include "Thread.h"
#include "ThreadController.h"

#ifndef Controller_h
#define Controller_h

class Controller{

 public:

 Controller(int onlinePin);;
 void start();
 void stop();
 void set_isOn(byte power);
 int getOnlinePin();
 //void recieve(int serialInput);

 private:
 //variables
 int _pin;
 Ampel _ampel;
 FussAmpel _fussAmpel;
 Button _button;
 boolean _isOn;

 //functions
 Ampel _initializeAmpel();
 FussAmpel _initializeFussAmpel();
 Button _initializeButton();
 void _ampelStop();
 void _ampelStart();
 void _ampelWarnsignal();
 void _controllerAlert();
 void _callbackButton();
 void _timerCallback();
};



#endif

cpp file:

#include "Arduino.h"
#include "Thread.h"
#include "Controller.h"
#include "Ampel.h"
#include "FussAmpel.h"
#include "Button.h"
#include "Lamp.h"
#include "ThreadController.h"

// Constructor
Controller::Controller(int pin)

: _pin(pin), _ampel(_initializeAmpel()), _fussAmpel(_initializeFussAmpel()), _button(_initializeButton())

{
 _isOn = true;
 //_status ='O';
 pinMode(_pin, OUTPUT);
 digitalWrite(_pin, HIGH);

};

int Controller::getOnlinePin(){
 return _pin;
}

// Initlializes
Ampel Controller::_initializeAmpel(){
 Lamp red(13);
 Lamp yellow(12);
 Lamp green(11);
 Ampel ampel(red,yellow,green);
 return ampel;

}

FussAmpel Controller::_initializeFussAmpel(){
 Lamp rot(9);
 Lamp gruen(10);
 FussAmpel fussAmpel(rot, gruen);
 return fussAmpel;
}

Button Controller::_initializeButton(){
 Button button(7);
 return button;
}

void Controller::_timerCallback(){
 _tc.run();
}

void Controller::_callbackButton(){
 Serial.println("callback");
}

// public Methoden
void Controller::stop(){
 _ampelWarnsignal();
}

void Controller::start(){
 while(_isOn){
 _ampelStop();
 delay(10000);
 _ampelStart();
 delay(10000);
 }
}

// private Methoden

// logic
void Controller::set_isOn(byte controllerPower){
 if(controllerPower == 1){
 _isOn = true;
 }else if (controllerPower == 0) {
 _isOn = false;
 }else{
 while(controllerPower !=0 || controllerPower !=1){
 _ampelWarnsignal();
 _controllerAlert();
 }
 }
}

void Controller::_controllerAlert(){
 while (!_isOn){
 delay(1000);
 digitalWrite(_pin, LOW);
 delay(1000);
 digitalWrite(_pin, HIGH);
 }
 digitalWrite(_pin, HIGH);
}




//Ampelschaltungen
void Controller::_ampelStop(){
 _ampel.setGelb(1);
 delay(2000);
 _ampel.setRot(1);
 _ampel.setGelb(0);
 _ampel.setGruen(0);
 delay(2000);
 _fussAmpel.setRot(0);
 _fussAmpel.setGruen(1);
}

void Controller::_ampelStart(){
 _fussAmpel.setRot(1);
 _fussAmpel.setGruen(0);
 delay(2000);
 _ampel.setGelb(1);
 delay(2000);
 _ampel.setRot(0);
 _ampel.setGelb(0);
 _ampel.setGruen(1);


}
void Controller::_ampelWarnsignal(){
 _ampel.setRot(0);
 _ampel.setGelb(0);
 _ampel.setGruen(0);
 _fussAmpel.setRot(0);
 _fussAmpel.setGruen(0);
 while(_isOn){
 _ampel.setGelb(1);
 _fussAmpel.setRot(1);
 delay(1000);
 _ampel.setGelb(0);
 _fussAmpel.setRot(0);
 delay(1000);

 }
}

Maybe the demo Several Things at a Time will be of use. It does what its name suggests without any requirement for Threading libraries.

The Thread Planning and Implementing a Program uses a similar system.

...R

Hi,

yes I do have a lot of examples. But:
they all use Setup and loop Method. I want this to run in my class. Thats the point. :frowning:

I have the exampleCode from Thread() library. But I dont understand how i get it run within my class.

I thought it must be something inside of the initializeButtun() function.

There i should create the Thread() and so on. But Im not sure how to to it.

do you know where the

Invalid arguments '
Candidates are:
void onRun(void (*)())
'
comes from in the Thread.onRun function? I Edited the first post..

Thanks a lot for the Links Robin. Was a great thing to me. I was on a completly Java styled way. thats much easier than i thought it would be. Now I got communitcation between devices an everything works very fine to me.

Thanks again.

There is nothing to stop you doing the same thing in java.

Mark

holmes4:
There is nothing to stop you doing the same thing in java.

I have the idea that Java gurus don't like things that look obvious - hence I avoid Java even though I like the idea of the JVM.

...R