Hi,
I'm trying to call function thats in class from diferent class. Compiler accept this code but microcontroller didn't work properly. Can you please tell me how should I write code in correct way?
main.ino
#include "ClassIN.h"
#include "ClassOUT.h"
InputPeripherals buttonA;
OutputPeripherals leds;
void setup()
{
delay(20);
Serial.begin(9600);
delay(20);
Serial.println("\nTransfer started \n\n");
leds.init();
leds.number(5);
buttonA.init(12,(void(*)) &leds.number);
}
void loop()
{
buttonA.updateClass();
delay(500);
leds.number(8);
delay(500);
Serial.print("end loop\n\n");
}
ClassIN.h
#ifndef fceIN_h
#define fceIN_h
#include "Arduino.h"
class InputPeripherals
{
public:
InputPeripherals();
void init(int buttonAdress,void (*funkce) (byte number));
void updateClass();
private:
int _pin;
void (*_fooShowNumber) (byte number);
};
#endif
ClassIN.ino
#include "ClassIN.h"
InputPeripherals::InputPeripherals(){}
void InputPeripherals::init(int buttonAdress, void (*foo) (byte number))
{
Serial.println("Output set INPUT_PULLUP");
pinMode(buttonAdress,INPUT_PULLUP);
_pin = buttonAdress;
_fooShowNumber = foo;
}
void InputPeripherals::updateClass()
{
Serial.print("Button value is: ");
Serial.println(digitalRead(_pin));
if(digitalRead(_pin) == 0)
{
Serial.println("calling 3");
_fooShowNumber(3);
}
else
{
Serial.println("calling 1");
_fooShowNumber(1);
}
}
ClassOUT.h
#ifndef fceOUT_h
#define fceOUT_h
#include "Arduino.h"
class OutputPeripherals
{
public:
OutputPeripherals();
void init(); // !!! správně by tady mělo být vkládání pole(_leds[8] ale pro zjednodušení jsem ho vynechal
void number(byte number);
private:
int _leds[8] {3,4,5,6,7,8,9,10};
};
#endif
ClassOUT.ino
#include "ClassOUT.h"
OutputPeripherals::OutputPeripherals() {}
void OutputPeripherals::init()
{
for(int i = 0; i < 8;i++)
{
pinMode(_leds[i],OUTPUT);
}
}
void OutputPeripherals::number(byte number)
{
Serial.print("Incomming number: ");
Serial.println(number);
for (int i = 0; i < 8; i++)
{
digitalWrite(_leds[i],number & (0x01<<i));
}
}
But output from arduino doesn't make any sence:
Transfer started
Incomming number: 5
Output set INPUT_PULLUP
utton value is: 1
calling 1
Incomming number: 10
Incomming number: 8
end loop
Button value is: 1
calling 1
Incomming number: 10
Incomming number: 8
end loop
Sometimes even arduino switch from input_pullup to output "0" thats make me scary can you tell me whats wrong?