Arduino and Raspberry pi to control an AC dimming module

Hello,

I am trying to setup a dimming module. The module came only with arduino code and I have not used arduino much. My problem is that the percentage of dimming has to be modified with time or by human input ( Is part of a bigger project). I dont really know if you can do that easily with arduino. I am trying to send characters through serial and have an arduino code waiting and depending on the character sent, it would set the dimming level to a certain value. Have not figured out how to do it… I’ve tried to do examples that I found for simple serial communication and they work. but whenever I try to applied them to this it just does not work.

Here is my code:

#include <TimerOne.h>

#define channel_1 4 
#define channel_2 5
#define channel_3 6
#define channel_4 7
#define channel_5 8 
#define channel_6 9
#define channel_7 10
#define channel_8 11

#define SPEED 20

#define GATE_IMPULSE 5

#define FREQ 84
  
unsigned int  CH1, CH2, CH3, CH4, CH5, CH6, CH7, CH8;
unsigned int  buf_CH1, buf_CH2, buf_CH3, buf_CH4, buf_CH5, buf_CH6, buf_CH7, buf_CH8;
unsigned char clock_cn;    
unsigned int  clock_tick;   
char receivedChar;
boolean newData = false;

void setup() {

  Serial.begin(9600);
  Serial.println("<Arduino is ready>");
  delay(100);
  pinMode(channel_1, OUTPUT);
  pinMode(channel_2, OUTPUT);
  pinMode(channel_3, OUTPUT);
  pinMode(channel_4, OUTPUT);
  pinMode(channel_5, OUTPUT);
  pinMode(channel_6, OUTPUT);
  pinMode(channel_7, OUTPUT);
  pinMode(channel_8, OUTPUT);
  attachInterrupt(1, zero_crosss_int, RISING);
  Timer1.initialize(10); 
  Timer1.attachInterrupt( timerIsr );
  

    
}

void timerIsr()
{    
    clock_tick++;


    if (clock_cn) 
     {
      clock_cn++;
      
       if (clock_cn==GATE_IMPULSE)
       {
        digitalWrite(channel_1, LOW); 
        digitalWrite(channel_2, LOW); 
        digitalWrite(channel_3, LOW); 
        digitalWrite(channel_4, LOW);
        digitalWrite(channel_5, LOW); 
        digitalWrite(channel_6, LOW); 
        digitalWrite(channel_7, LOW); 
        digitalWrite(channel_8, LOW);
        clock_cn=0;
       }
     }
   
        if (CH1==clock_tick)
         {
          digitalWrite(channel_1, HIGH);
          clock_cn=1;
         }  
    
           if (CH2==clock_tick)
            {
             digitalWrite(channel_2, HIGH);
             clock_cn=1;
            }  
        
              if (CH3==clock_tick)
               {
                digitalWrite(channel_3, HIGH);
                clock_cn=1;
               }  
    
                 if (CH4==clock_tick)
                  {
                   digitalWrite(channel_4, HIGH);
                   clock_cn=1;
                  }

                    if (CH5==clock_tick)
                    {
                     digitalWrite(channel_5, HIGH);
                     clock_cn=1;
                    }

                      if (CH6==clock_tick)
                      {
                       digitalWrite(channel_6, HIGH);
                       clock_cn=1;
                      }

                         if (CH7==clock_tick)
                         {
                          digitalWrite(channel_7, HIGH);
                          clock_cn=1;
                         }

                           if (CH8==clock_tick)
                           {
                            digitalWrite(channel_8, HIGH);
                            clock_cn=1;
                           }

                             

                  
}

 

void zero_crosss_int()
{
  CH1=buf_CH1;
   CH2=buf_CH2;
    CH3=buf_CH3;
     CH4=buf_CH4;
      CH5=buf_CH5;
       CH6=buf_CH6;
        CH7=buf_CH7;
         CH8=buf_CH8;
  
  clock_tick=0;        
}

unsigned int DIMM_VALUE (unsigned char level)
{
 unsigned int buf_level;

 if (level < 26)  {level=26;}
 if (level > 229) {level=229;}
  
 return ((level*(FREQ))/256)*10;  
}


void loop() {
  
 recvOneChar();
 showNewData();

}

void recvOneChar() {
 if (Serial.available() > 0) {
 receivedChar = Serial.read();
 newData = true;
 }
}

void showNewData() {
 if (newData == true) {
 Serial.print("This just in ... ");
 Serial.println(receivedChar);
 newData = false;
  
 buf_CH1=DIMM_VALUE(180);
 delay(SPEED);
  
 }
 buf_CH1=DIMM_VALUE(100);
 delay(SPEED);
}

In this test I am just trying to get serial communication going.
When I run it does set the dimming level to the initial one but when I send a command id does not do anything.

Than you very much hope you could help me,

I don't understand the purpose of the many outputs in your code - do you?

If the module comes with Arduino code, why don't you use it?

void showNewData() {
 if (newData == true) {
 Serial.print("This just in ... ");
 Serial.println(receivedChar);

You print the character that you received from Serial right there. Now you have it in that receivedChar variable. Go ahead and do whatever you want to do with it.

Sorry, the code is made for an 8 channel dimming module and I am using the 8 channels but I just want that to work. I need to set the dimming level upon request from a user (user interface on the pi).

I tried to use the serial data but the weird thing is that the if loop Delta_G showed is not running completely "buf_CH1=DIMM_VALUE(180);" this line is not running. Only the one outside of the loop runs when I upload the program

  1. It's not an "if loop". Never say that again. If doesn't loop. It's an if statement.

  2. Nothing is being ignored. Your code:

void showNewData() {
 if (newData == true) {
 Serial.print("This just in ... ");
 Serial.println(receivedChar);
 newData = false;
  
 buf_CH1=DIMM_VALUE(180);
 delay(SPEED);
  
 }
 buf_CH1=DIMM_VALUE(100);
 delay(SPEED);
}

Will see that new data is available, and set the dim value to 180 and delay for

#define SPEED 20

20 milliseconds and then the next line sets it back to 100. It's not at 180 long enough for you to see it.

EDIT: I take that back. I don't think there's anything to see. This code changes buf_CH1 but the ISR is using CH1. And that only gets set in the zero cross ISR. So it may not ever even affect the actual dimming.

Sorry for that... I know

Is there I way that I can modified the dimming level after it has been set using serial? Is adjusting the time the answer two this question? I am a little bit confused after you edit

WIll it be possible to change where I set the ISR so that every time I want to change it, it actually does it. For example putting it inside the void loop inside the if statement so that it is being called when there is something from serial?

Thanks,

Do you understand how this works to dim something? Do you understand what those interrupt routines are doing and how they work?

If not, then stop here and learn that first.