Dear all,
I am having some trouble with my masters project, studying multivariable controllers. I am using Serial to send data from 2 sensors to Simulink, solve the control law and give back signals to my actuators.
Sensors: LDR and LM35
Actuators: rotation of a PC fan and voltage of a lamp.
My problem is specifically with the lamp. I am having something which looks like interference from serial, making my lamp to flicker. The lamp is connected in AC and I use zero cross detection (opto) to know where to cut the wave and then a TRIAC to send the a DelayMicroseconds to the lamp.
As I use Simulink to perform my controllers calculations, I have to send 2 numbers to Simulink and receive 2 as well, every second.
P.S.1: Using only a simple code to turn on the lamp for a specific "powertime" works fine. I tested all the components of my circuit
P.S.2: Even running just the Arduino Code without running the Simulink Model, I also have this flickering problem.
P.S.3: When I enable the interrupts it seems the problem stops, but I really need them to perform all the loops every second. Probably there is a more elegant way to perform such tasks.
CODE 1: Just to turn on the lamp for a given "powertime". Works perfectly.
#include <DueTimer.h>
#define zeroPin 2
#define outputPin 3
int i;
float powertime;
void setup() {
Serial.begin(57600);
pinMode(zeroPin, INPUT);
pinMode(outputPin, OUTPUT);
while(true){
if (digitalRead(zeroPin) == HIGH){
zeroCross();
}
}
}
void zeroCross() {
powertime = 5000;
delayMicroseconds(powertime);
digitalWrite(outputPin,HIGH);
delayMicroseconds(15);
digitalWrite(outputPin,LOW);
}
void loop(){
}
CODE 2: A code I´ve developed to communicate using serial. I receive the data from Simulink and give it back. Also it works perfectly.
char strToSend[128];
double ddataBuff, ddataBuff1, ddataBuff2;
String Buff, cBuff,dBuff;
char Buff1[128], Buff2[128];
int n;
int posfinal[2], posD;
void setup() {
Serial.begin(57600);
}
void loop() {
while (Serial.available() > 0) {
Buff = Serial.readStringUntil('\n');
}
posD=0;
posfinal[0] = Buff.indexOf(32,posD);
posD = posfinal[0]+1;
posfinal[1] = Buff.indexOf(0,posD);
posD = posfinal[1];
for(n = 0; n < posfinal[0]; ++n){
Buff1[n] = Buff[n];
}
Buff1[127] = '\0';
for(n = 0; n < posfinal[1]; ++n){
Buff2[n] = Buff[posfinal[0] + 1 + n];
}
Buff2[127] = '\0';
ddataBuff1 = atof(Buff1);
ddataBuff2 = atof(Buff2);
ddataBuff = ddataBuff1 + ddataBuff2;
cBuff = String(ddataBuff,DEC);
dBuff = cBuff;
dBuff.toCharArray(strToSend,8);
Serial.println(strToSend);
for( n = 0; n < sizeof(Buff1); ++n ){
Buff1[n] = (char)0;
}
for( n = 0; n < sizeof(Buff2); ++n ){
Buff2[n] = (char)0;
}
}
CODE 3: My complete code that does not work and make the lamp to flicker.
#include <DueTimer.h>
#define zeroPin 2
#define dimmerPin 3
#define fanPin 11
#define ldrPin A0
#define lm35Pin A2
#define ledPin 13
char strToSend1[128], strToSend2[128], finalString[128];
volatile double ddataBuff1, ddataBuff2;
String Buff, c1Buff, c2Buff, d1Buff, d2Buff;
char Buff1[128], Buff2[128];
int n;
int posfinal[2], posD;
volatile double powerlamp0, powerfan0;
volatile int powerlamp1, powerfan1, powertime;
int cont1, cont2;
double tempAux1[50],tempAux2, tempReal;
double lumAux1[50], lumAux2, lumReal;
int ledState = 0;
void setup() {
Serial.begin(57600);
pinMode(zeroPin,INPUT);
pinMode(dimmerPin,OUTPUT);
pinMode(fanPin,OUTPUT);
pinMode(ledPin,OUTPUT);
Timer3.attachInterrupt(matlab);
Timer3.start(1000000);
Timer4.attachInterrupt(send_data);
Timer4.start(1000000);
Timer5.attachInterrupt(receive_data);
Timer5.start(1000000);
while (true) {
if (digitalRead(zeroPin) == HIGH){
zeroCross();
}
digitalWrite(ledPin,ledState);
}
}
void matlab() {
powerlamp0 = ddataBuff1;
powerfan0 = ddataBuff2;
powerlamp1 = (int)powerlamp0;
powerfan1 = (int)powerfan0;
analogWrite(fanPin,powerfan1);
ledState = !ledState;
}
void receive_data() {
while (Serial.available() > 0) {
Buff = Serial.readStringUntil('\n');
}
posD=0;
posfinal[0] = Buff.indexOf(32,posD);
posD = posfinal[0]+1;
posfinal[1] = Buff.indexOf(0,posD);
posD = posfinal[1];
for(n = 0; n < posfinal[0]; ++n){
Buff1[n] = Buff[n];
}
Buff1[127] = '\0';
for(n = 0; n < posfinal[1]; ++n){
Buff2[n] = Buff[posfinal[0] + 1 + n];
}
Buff2[127] = '\0';
ddataBuff1 = atof(Buff1);
ddataBuff2 = atof(Buff2);
for( n = 0; n < sizeof(Buff1); ++n ){
Buff1[n] = (char)0;
}
for( n = 0; n < sizeof(Buff2); ++n ){
Buff2[n] = (char)0;
}
}
void send_data() {
tempAux2 = 0;
tempReal = 0;
lumAux2 = 0;
lumReal = 0;
for (cont1 = 0; cont1 < 50;cont1++) {
tempAux1[cont1] = analogRead(lm35Pin);
tempAux2 = tempAux2 + tempAux1[cont1];
}
tempReal = tempAux2 / 50;
for (cont2 = 0; cont2 < 50;cont2++) {
lumAux1[cont2] = analogRead(ldrPin);
lumAux2 = lumAux2 + lumAux1[cont2];
}
lumReal = lumAux2 / 50;
c1Buff = String(lumReal,DEC);
c2Buff = String(tempReal,DEC);
d1Buff = c1Buff;
d2Buff = c2Buff;
d1Buff.toCharArray(strToSend1,8);
d2Buff.toCharArray(strToSend2,8);
sprintf(finalString,"%s %s",strToSend1,strToSend2);
Serial.println(finalString);
}
void zeroCross() {
powertime = powerlamp1;
delayMicroseconds(powertime);
digitalWrite(dimmerPin, HIGH);
delayMicroseconds(15);
digitalWrite(dimmerPin, LOW);
}
void loop() {
}