Hello everyone,
I'm working on an arduino UNO with and ethernet shield and a Tinkerkit with 2 potentiometers and 1 mosfet.
My goal is to achieve to send MQTT messages of the value given by the potentiometer and in the same time to control the brightness of a lamp on the Mosfet.
The thing is that the Uno can send the potentiometer value on one hand. And on the other hand, with the basic example of Mosfet, i can control the brightness of the lamp.
But when i want to mix both of the codes, the lamp will be on but i can not control the brightness anymore. The potentiometers values are still sended to the broker.
After some researches on the internet and on this forum, it seems that i should use a "without delay" method using millis.
But i encountered some difficulties to understand that method and to translate that in my code.
(i've seen that brilliant topic with the example for 4 leds)
So i would like to be sure that i need to use this solution before trying hard !
And also if somebody knows how to deal with this particular case i would be really interested 
If you need more informations let me know !
Regards,
Chopanaweur
Hey after a chilling night i used the millis non blocking method, but still it's not working...
I tried on the basic example of the Mosfet to replace the delay by the non blocking way using millis but it's not working.
So i left the delay only for my mosfet function. I tried so many things like commenting line in the setup to see if the mosfet can be controlle but nothing has worked so far...
Here is my code if someone knows where does the problem come from i would be happy to hear you !
#include <SPI.h>
#include <Ethernet2.h>
#include <TinkerKit.h>
#include <PubSubClient.h>
//--------------------------CONSTANTS----------------------------
byte mac[] = { 0xDE, 0xED, 0xBA, 0xFE, 0xFE, 0xED };
byte server[] = { 192, 168, 0, 100 };
byte ip[] = { 192, 168, 0, 115 };
int delayReconnect = 5000; //delay in milliseconds
int delayPublish = 1000;
int delayPublish1 = 1000;
int delayPublishM = 1000;
int delayMosfet = 10;
//-------------------------VARIABLES----------------------------
TKPotentiometer pot(I0); //create pot and mos objects
TKPotentiometer pot1(I1);
TKMosFet mos(O0);
int brightnessVal0 = 0 ;
int brightnessVal1 = 0 ;
unsigned long currentMillis = 0; //Stores the value of millis() in each iteration of loop()
unsigned long previousReconnectMillis = 0; //will store the last time a reconnection was tried
unsigned long previousPublishMillis =0;
unsigned long previousPublish1Millis =0;
unsigned long previousPublishMMillis=0;
unsigned long previousMosfetMillis =0;
//===============================================================
void callback(char* topic, byte* payload, unsigned int length) {
Serial.print("Message arrived [");
Serial.print(topic);
Serial.print("] ");
for (int i=0;i<length;i++) {
Serial.print((char)payload[i]);
}
Serial.println();
}
//===============================================================
EthernetClient ethClient;
PubSubClient client(ethClient);
void reconnect() {
// Loop until we're reconnected
while (!client.connected()) {
Serial.print("Attempting MQTT connection...");
// Attempt to connect
if (client.connect("arduinoClient")) {
Serial.println("connected");
// Once connected, publish an announcement...
client.publish("Uno","hello i'm UNO");
// ... and resubscribe
client.subscribe("inTopic");
} else {
if(currentMillis - previousReconnectMillis >= delayReconnect){ // Wait 5 seconds before retrying
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 5 seconds");
previousReconnectMillis += delayReconnect;
}
}
}
}
//==========================================================================
void publication() {
if(currentMillis - previousPublishMillis >= delayPublish){
brightnessVal0 = pot.read();
char P0val[100];
int P0value=brightnessVal0;
sprintf(P0val, "%d", P0value);
Serial.print("value for potentiometre 0 = ");
Serial.println(P0value);
client.publish("Uno", "The value for pententionmetre 0 is :");
client.publish("Uno",P0val);
previousPublishMillis += delayPublish;
}
}
//=============================================================
void MosFetPublish(){
if(currentMillis - previousPublishMMillis >= delayPublishM){
int MFPval = pot.read();
int MFPval2= pot1.read();
int MFPval3= (MFPval + MFPval2)/2;
char MFPval4[100];
int MFPvalue=MFPval3;
sprintf(MFPval4, "%d", MFPvalue);
Serial.print("Value for MosFet = ");
Serial.println(MFPvalue);
client.publish("Uno", "The value for mosfet is :");
client.publish("Uno",MFPval4);
previousPublishMMillis += delayPublishM;
}
}
//===============================================================
void publication1(){
if(currentMillis - previousPublish1Millis >= delayPublish1){
brightnessVal1 = pot1.read();
char P1val1[100];
int P1value1=brightnessVal1;
sprintf(P1val1, "%d", P1value1);
Serial.print("value for potentiometre 1 = ");
Serial.println(P1value1);
client.publish("Uno", "The value for pententionmetre 1 is :");
client.publish("Uno",P1val1);
previousPublish1Millis += delayPublish1;
}
}
//=======================================================================
void MosFet(){
int val = pot.read(); //assign to a "val" variable
int val2 = pot1.read();
int val3 = (val+val2)/2; //the potentiometer values
mos.write(val3); //assign the values to the mosfet
delay(10); //rest for 10 milliseconds.
}
//===============================================================
void setup() {
Serial.begin(57600);
client.setServer(server, 1883); //set the MQTT server
client.setCallback(callback);
Ethernet.begin(mac, ip);
//delay(1500);
//previousReconnectMillis =0;
}
//==================================================================
void loop() {
currentMillis = millis();
if (!client.connected()) {
reconnect();
client.loop();
}
if (client.connected()){
publication();
publication1();
MosFetPublish();
MosFet();
}
}