Hardware serial port

see above is my edited conclusion. There was no need of sending all 255 numbers as I was mapping my AC voltage output of 220 volts to 255 numbers which can be achieved by mapping it to just 170 or 180 whatever we want it to be until our voltage requirement is fulfilled. I set my range to 0-180 as my output reached its maximum at this value.
Please note I used both these codes that both of you provided. I did not changed your codes but i changed my Button range that was sending numbers to arduino.
here are both codes that solved issue one is commented out. Indeed I need to change N and F as they exist between 0-180 numbers . I will choose characters that occur after 180 in ASCII table.

int mydelay         = 0;
int myvalue         = 0;
int last_CH1_state  = 0;

const int PinDiac   = 3;
const int PinLed    = 11;

enum { LedOn = HIGH, LedOff = LOW };

// -----------------------------------------------------------------------------
void setup ()
{
    Serial.begin (9600);

    PCICR  |= (1 << PCIE0);    //enable PCMSK0 scan
    PCMSK0 |= (1 << PCINT0);  //Set pin D8 trigger an interrupt on state change. Input from optocoupler

    pinMode       (PinDiac, OUTPUT); //Define D3 as output for the DIAC pulse
    pinMode       (PinLed,  OUTPUT);
    digitalWrite  (PinDiac, HIGH);
    digitalWrite  (PinLed,  LedOff);
}

// -----------------------------------------------------------------------------
void loop ()

////////////////////////////// SECOND METHOD ///////////////////////////////////
    // {
    //     if (Serial.available()) {
    //     char rec = Serial.read (); // char is equivalent to byte and uint8_t

    //     switch (rec)  {
    //         case '\n':
    //         break;
    //     case 0:
    //         digitalWrite (PinLed, LedOff);
    //         break;

    //     case 1:
    //         digitalWrite (PinLed, LedOn);
    //         break;

    //     default:
    //         myvalue = map (rec, 2, 255, 6700, 10);
    //         Serial.print   ("  myvalue ");
    //         Serial.println (myvalue);
    //         break;
    //     }
    // }
    ////////////////////// fIRST METHOD \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
    {
        {
        if (Serial.available()) {
        char rec = Serial.read ();

        switch (rec)  {
        case '\n':
            break;

        case 'F':
            digitalWrite (PinLed, LedOff);
            break;

        case 'N':
            digitalWrite (PinLed, LedOn);
            break;

        default:
            myvalue = map (rec, 0, 255, 6700, 10);
            Serial.print   ("  myvalue ");
            Serial.println (myvalue);
            break;
        }
    }

    if (mydelay) {
        delayMicroseconds (myvalue); //This delay controls the power
        digitalWrite      (PinDiac, HIGH);
        delayMicroseconds (100);
        digitalWrite      (PinDiac, LOW);
        mydelay=0;
    }
}

// -----------------------------------------------------------------------------
ISR (PCINT0_vect) {
    bool currentState = PINB & B00000001;
    if (currentState != last_CH1_state) {
        last_CH1_state = currentState;
        if (currentState == HIGH) {
            mydelay = 1;
        }
    }

    // If pin 8 is LOW and the last state was HIGH then we have a state change
    else if (last_CH1_state == 1)  {
        mydelay        = 1;  // We haev detected a state change!
        last_CH1_state = 0;  //Store the current state into the last state for the next loop
    }
}

I am sorry to announce that even the project worked expectedly as I tested initially, but upon testing it continuously for hours I discovered that even using the characters outside my slider range, both buttons commands interfere with each others output. when I move the slider continuously it makes the LED PIN on and off but it should not, and when i click the LED button it controls the operation which was expected to be controlled by the slider. I think this happens due to the interrupt that is being triggered at pin-8. I had left this project 4 years back due to the same reason. So I can not close this Topic as solved till now. Please share your ideas to address this issue.

are you using the slider (on some other device) to generate the ASCII code to both toggle the LED as well as generate the delay?

if so, there's no guarantee that it doesn't generate the values for 'F' and 'N'. I had thought you were generating the serial data via key presses

As far as I understand

means (I think) "Moving the slider on my APP will send a byte (if parameter set as "as a number") ranging from 0 to 180 to the serial port"

Necessary but not implemented in code post #21.

@74younus : tell me if I'm wrong.

So, did you make it so that the buttons on the APP send values outside [0-180] and modify the Arduino program accordingly ?

Can you modify you slider code so that it doesn't send the values of 'F' and 'N'?

@gcjr I am using another software that calls the bluetooth client to send one byte number and in that i skipped 0 and 1 but still there was interference. I then tried to skip 254 and 255 for LED pin control , initially it also worked as expected but as I tried controlling things for some more time , it again started creating interference with each other my LED turned on and off when i moved the slider button which was expected by the Led button not by the slider. I am suspicious this could be due to capacitance between the pins as they are pulled down by default , should I use internal pull up resistors then!

here is the output when i send 0 and 1 via bluetioth

myvalue 6045
myvalue 6150

And tis is output when I send ON\n and OFF\n

myvalue 4628
myvalue 4654
myvalue 4628
myvalue 4864
myvalue 4864
myvalue 4628
myvalue 4654

Yes, I will try to set soft slider to block some numbers. and then I will be back again here.

I changed N and F to 181 and 182 but It did not turn LED on or OFF neither it displays anything on serial monitor . Do you have any idea why.

Yes i did but the same issue persists.
here is the changed code

 {
        if (Serial.available()) {
        char rec = Serial.read ();

        switch (rec)  {
        case '\n':
            break;

        case '0':
            digitalWrite (PinLed, LedOff);
            break;

        case '1':
            digitalWrite (PinLed, LedOn);
            break;

        default:
            myvalue = map (rec, 2, 255, 6700, 10);
            Serial.print   ("  myvalue ");
            Serial.println (myvalue);
            break;
        }
    }

Since as far as i can see, nobody has mentioned it.
Any variable that is shared by the main thread and the ISR (read in one, modified in the other or modified in both) has to be declared 'volatile' i would say that this applies to

int mydelay         = 0;

also depending on the architecture of the board. If it's an 8-bit MCU like any AVR. Any variable that bigger than 8-bit (say int, which is 16-bit) is read in the main thread, should be copied into another variable while interrupts are turned off, otherwise the chance exists that 1 8-bit part is modified before the 8-bit part is read. This doesn't appear to matter much here since the value of the variable changes between 0 & 1 and the variable may as well have been an 8-bit variable.

there should be no myvalue output when the values for turning the LED on/off are received.

just to reiterate, the ASCII value of '0' is 48, '1' is 49, 'F' is 70 and 'N' is 78

Of course, you need to change the values on both side, sender and receiver:
case '0': should be case 181: and case '1': should be case 182: (note there is no quotes around 181 & 182)

Yes, here is my comment about the code:

Considering the way you use them, you should write:

bool mydelay            = false;
int myvalue             = 0;
uint8_t last_CH1_state  = 0;

but since mydelay is used and modified inside and outside an Interrupt Service Routine, you must declare it as volatile. Hence:

volatile bool mydelay   = false;
int myvalue             = 0;
uint8_t last_CH1_state  = 0;

and if(mydelay) {} would then become:

  if (mydelay) {
    delayMicroseconds (myvalue); //This delay controls the power
    digitalWrite      (PinDiac, HIGH);
    delayMicroseconds (100);
    digitalWrite      (PinDiac, LOW);
    mydelay = false;
  }

even though it doesn't make a big difference (except the volatile).

currentState should be a uint8_t (i.e. a byte), not a bool even though it also works...

a bool is false or true, not LOW or HIGH even though the resulting value is (I think) the same.

No, not in this part of the sketch, count the accolades (curly brackets). This part of the code is probably not useful at all. If you want to trigger the diac pulse on both rising and falling edges, here is a sketch:

volatile bool mydelay   = false;
int myvalue             = 0;
uint8_t last_CH1_state  = 0;

const int PinDiac       = 3;
const int PinLed        = 11;

enum { LedOn = HIGH, LedOff = LOW };

// -----------------------------------------------------------------------------
void setup () {
  Serial.begin (9600);

  PCICR  |= (1 << PCIE0);    //enable PCMSK0 scan
  PCMSK0 |= (1 << PCINT0);  //Set pin D8 trigger an interrupt on state change. Input from optocoupler

  pinMode       (PinDiac, OUTPUT); //Define D3 as output for the DIAC pulse
  pinMode       (PinLed,  OUTPUT);
  digitalWrite  (PinDiac, HIGH);
  digitalWrite  (PinLed,  LedOff);
}

// -----------------------------------------------------------------------------
void loop () {
  if (Serial.available()) {
    char rec = Serial.read ();

    switch (rec)  {
      case '\n':
        break;

      case 181:  // OR ANY VALUE SENT BY THE APP!!!
        digitalWrite (PinLed, LedOff);
        break;

      case 182:  // OR ANY VALUE SENT BY THE APP!!!
        digitalWrite (PinLed, LedOn);
        break;

      default:
        myvalue = map (rec, 0, 180, 6700, 10);  // OR ANY VALUE SENT BY THE APP!!!
        Serial.print   ("  myvalue ");
        Serial.println (myvalue);
        break;
    }
  }

  if (mydelay) {
    delayMicroseconds (myvalue); //This delay controls the power
    digitalWrite      (PinDiac, HIGH);
    delayMicroseconds (100);
    digitalWrite      (PinDiac, LOW);
    mydelay = false;
  }
}

// -----------------------------------------------------------------------------
ISR (PCINT0_vect) {
  uint8_t currentState = PINB & B00000001;
  if (currentState != last_CH1_state) {
    last_CH1_state = currentState;
    // if (currentState == B00000001) { // REMOVE THE IF TO TRIGGER THE PULSE ON BOTH EDGES
    mydelay = true;
    // }
  }
}

If you notice jitter in the power level, you probably can use a timer interrupt to replace the delayMicroseconds (myvalue)

@Deva_Rishi @Etienne_74
I am checking your code in the first attempt it worked fine, I sent 0\n and 1\n and in your code it checks 181 and 182 against what I sent. But I had not set my slider button to block values after 180 so it interfered. Now as I blocked the slider values after 180. I sent 0\n and 1\n still same thing happens . Next I sent 181\n and 182\n against arduino code which also checks 181 and 182, the arduino Tx LED glows and remains glowing for some time and went off . Now I am confused about what to sent , how to sent . I think I have to activate internal pullup resistors to avoid interference from the interrupt function. Another problem that seems to me is I do not know how to send the data , I do not know whether i should use <> or \n or </> in my transmitting code. If any ideas please suggest.

pleas epost the code sending values to the Arduino

if you sending a single byte value, why would you describe in this way, with a "\n". "\n", a line feed is the value 10, or 0xA, a 2nd byte of data

First of all please confirm me should i send these numbers in a text block or a number block . I mean should I send these as numbers or as texts.

what is a text or number block?

please post that code so that we can see

This is for LED button:

This is for triac

"0\n" is ASCII text, send a number and show us the block

The triac part seems OK except maybe the 0-180 limits