Dimmer Signal Problem in CODE

Hi good afternoon people, i made an AC dimmer control and works good bur the question here is that i want to turn on individually one lamp by one, with this code when i turn on the first lamp its okay but when i turn on the second lamp the first turn on too and if i connect another lamp the three lamps turn on and off, so i want to know how can i do this individually? if someone can help me please :slight_smile: thank you.

#include <SPI.h>
#include <String.h>
#include <EthernetV2_0.h>
#include <LiquidCrystal.h>
#include <TimerOne.h>
#define W5200_CS  10
#define SDCARD_CS 4

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };   // Escribimos la direccion MAC de el Ethernet Shield
byte ip[] = { 192, 168, 1, 2 };                       // Escribimos la IP dentro del rango de la red
EthernetServer server(80);                           // Iniciamos el puerto de servicio

volatile int i=0;                   // se declara una variable para utilizarla como contador
volatile boolean zero_cross=0;     // variable tipo boleana para almacenar el switch cuando hay un cruce a cero
int AC_pin = 3;                   // pin de salida para el Optotriac
int AC_pin2 = 7;
int pas = 8;                     //Pasos de 8 en 8
int dim = 128;
int dim3 = 256;
int freqStep = 75;
String readString = String(30);  // tamaño de string para buscar las direcciones establecidas(Ligarled,Desligarled)


void setup(){
  Serial.begin(9600);           // Inicia la comunicacion Serial
  pinMode(SDCARD_CS,OUTPUT);
  digitalWrite(SDCARD_CS,HIGH);
  
  pinMode (AC_pin,OUTPUT);
  pinMode (AC_pin2,OUTPUT);
  Ethernet.begin(mac, ip);      // Inicia la comunicacion etehernet
  server.begin();
  attachInterrupt(0, zero_cross_detect, RISING);    //  Añade una interrupcion en el pin2 para la deteccion por cero
  Timer1.initialize(freqStep);                      // Inicializamos la libreria timer one para la frecuencia que necesitamos
  Timer1.attachInterrupt(dim_check, freqStep);      // Usamos la libreria timerone para añadir una interrupcion
}                              

void zero_cross_detect() {    
  zero_cross = true;               // Ajustamos el valor booleano a verdadero para avisar que paso por cero 
  i=0;
  digitalWrite(AC_pin, LOW);
  digitalWrite(AC_pin2,LOW);
}  

void dim_check() {               // Encendemos el triac al tiempo apropiado                 
    if(i>=dim) {                     
      digitalWrite(AC_pin, HIGH);  // Encendemos la luz 
    }
    if(i>=dim3) {                     
      digitalWrite(AC_pin2, HIGH);  // Encendemos la luz 
    }
            i++;  // se incrementa el time step counter                     
    }                                

void loop(){
  
  EthernetClient client = server.available();  // Creamos la conexion a cliente

  if (client) {
    while (client.connected())
    {
      if (client.available())
      {
        char c = client.read();
       
        {
          readString += (c);          // almacena los caracteres o cadena de strings
        }
        
        if (c == '\n')        //el envio por http termina 

          {

            if(readString.indexOf("LAMPOFF")>=0)
         {
            dim=128;
          } 
 
 if(readString.indexOf("LAMPON")>=0)
          {
        dim=0;
    }
    if(readString.indexOf("LAMPON2")>=0)
          {
            dim3=0;
           }
            if(readString.indexOf("LAMPOFF2")>=0)
          {
            dim3=128;
           }
        client.println("HTTP/1.1 200 OK");
        client.println("Content-Type: text/html");
        client.println();
          
        readString="";

        client.stop();
        }
      }
    }
  }
  Serial.print("dim=");
  Serial.print(dim);
   Serial.print("    dim3=");
  Serial.print(dim3);
  Serial.print('\n');
  delay (100);

}

I see something...

When you test if (ReadString.indexof("lampon")>=0) ... And just after if (ReadString.indexof("lampon2")>=0) ... If the string is lampon2, then both tests are true... You should rename your commands : lamp1on and lamp2on , lamp1off and lamp2off

B83s:
I see something...

When you test if (ReadString.indexof("lampon")>=0) ... And just after if (ReadString.indexof("lampon2")>=0) ... If the string is lampon2, then both tests are true... You should rename your commands : lamp1on and lamp2on , lamp1off and lamp2off

Thank you very much friend this is the answer, one more question only to understand good, why if i use lampon and lampon2 the program takes that are the same? and if use lamp1on and lamp2on are different? thanks again you help me a lot :slight_smile:

The command readstring.indexof(pattern) search the first string that match the pattern (whatever the other characters are in the string ... In windows it is like lampon) you should have a look at the Learning > Reference > string(object)

So when you are looking for lampon ... if the program reads lampon2 , it sees the lampon string too

B83s:
The command readstring.indexof(pattern) search the first string that match the pattern (whatever the other characters are in the string ... In windows it is like lampon) you should have a look at the Learning > Reference > string(object)
indexOf() - Arduino Reference

So when you are looking for lampon ... if the program reads lampon2 , it sees the lampon string too

Thanks again for youl help.
Best Regards.