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);
}
}