Controll LED with Serial Write and/or push button - Help needed

I was able to make one code for turning off and on one led with the same button.
And one code to turn off and on led with serial read.
Now I want to put them together, can any one help me with this?

Code for one button controll on/off one led:

const int LED = 2;
const int LED2 = 3;
const int BUTTON = 22;
const int BUTTONB = 24;
boolean lastButton = LOW;
boolean lastButtonb = LOW;
boolean currentButton = LOW;
boolean currentButtonb = LOW;
boolean ledOn = false;
boolean ledOnb = false;


void setup()
{
  Serial.begin(9600);
  pinMode (LED, OUTPUT);
  pinMode (LED2, OUTPUT);
  pinMode (BUTTON, INPUT);
  pinMode (BUTTONB, INPUT);
}

boolean debounce(boolean last)
{
  boolean current = digitalRead(BUTTON);
   if (last != current)
   {
     delay(5);
     current = digitalRead(BUTTON);
   }
    return current;
}

boolean debounceb(boolean lastb)
{
  boolean currentb = digitalRead(BUTTONB);
   if (lastb != currentb)
   {
     delay(5);
     currentb = digitalRead(BUTTONB);
   }
    return currentb;
}

void loop()
{
  currentButton = debounce(lastButton);
   if (lastButton == LOW && currentButton == HIGH)
   {
     ledOn = !ledOn;
   }
   lastButton = currentButton;
   
   digitalWrite(LED, ledOn);
   
   currentButtonb = debounceb(lastButtonb);
   if (lastButtonb == LOW && currentButtonb == HIGH)
   {
     ledOnb = !ledOnb;
   }
   lastButtonb = currentButtonb;
   
   digitalWrite(LED2, ledOnb);
}

Code for controlling led on/off true serial read:

int LED= 2; 
int LED2 = 3; 
int Data = 0;

void setup()                    
{
  pinMode(LED, OUTPUT);
  pinMode(LED2, OUTPUT);
  Serial.begin(9600);
  digitalWrite(LED, LOW);
  digitalWrite(LED2, LOW);
  
}

void loop() 
{
  Data=Serial.read();            
  
  if (Data==0x31)                
  {
    digitalWrite(LED,HIGH);
    delay(10);
    Serial.println("LED pa");
  }
   else if (Data==0x32)         
   {
     digitalWrite(LED, LOW);
     delay(10);
     Serial.println("LED av");
   }
  
  if (Data==0x33)                
  {
    digitalWrite(LED2,HIGH);
    delay(10);
    Serial.println("LED2 pa");
  }
   else if (Data==0x34)         
   {
     digitalWrite(LED2, LOW);
     delay(10);
     Serial.println("LED2 av");
   }

}

Hi kimlorentz and welcome.

Please edit your post above and put your sketches inside code tags. Use the # button on the edit screen. Then we will take a look.

Paul

There you go, got in in Code tags 8)

Thankyou.

Should be pretty easy. If you just merge the two sketches (i.e. merge the variables, merge the setup() and loop() functions, removing duplicate code lines) does that work?

Have a go, if it does not work, post your merged sketch.

Tryed to merge them together but did not go well.
Buttons work god, but serial com. did not work. And if the lights are on and you start serial com. the lights go off by them selv.

const int LED1 = 2;
const int LED2 = 3;
const int BUTTONA = 22;
const int BUTTONB = 24;
boolean lastButtonA = LOW;
boolean lastButtonB = LOW;
boolean currentButtonA = LOW;
boolean currentButtonB = LOW;
boolean ledOna = false;
boolean ledOnb = false;
int Data = 0;


void setup()
{
  Serial.begin(9600);
  pinMode (LED1, OUTPUT);
  pinMode (LED2, OUTPUT);
  pinMode (BUTTONA, INPUT);
  pinMode (BUTTONB, INPUT);
}


boolean debounce(boolean last)
{
  boolean currenta = digitalRead(BUTTONA);
   if (last != currenta)
   {
     delay(5);
     currenta = digitalRead(BUTTONA);
   }
    return currenta;
}

boolean debounceb(boolean lastb)
{
  boolean currentb = digitalRead(BUTTONB);
   if (lastb != currentb)
   {
     delay(5);
     currentb = digitalRead(BUTTONB);
   }
    return currentb;
}


void loop()
{
  Data=Serial.read();            
  
  if (Data==0x31)                
  {
    digitalWrite(LED1,HIGH);
    delay(10);
    Serial.println("LED pa");
  }
   else if (Data==0x32)         
   {
     digitalWrite(LED1, LOW);
     delay(10);
     Serial.println("LED av");
   }
  
  if (Data==0x33)                
  {
    digitalWrite(LED2,HIGH);
    delay(10);
    Serial.println("LED2 pa");
  }
   else if (Data==0x34)         
   {
     digitalWrite(LED2, LOW);
     delay(10);
     Serial.println("LED2 av");
   }
  
  currentButtonA = debounce(lastButtonA);
   if (lastButtonA == LOW && currentButtonA == HIGH)
   {
     ledOna = !ledOna;
   }
   lastButtonA = currentButtonA;
   
   digitalWrite(LED1, ledOna);
   
   currentButtonB = debounceb(lastButtonB);
   if (lastButtonB == LOW && currentButtonB == HIGH)
   {
     ledOnb = !ledOnb;
   }
   lastButtonB = currentButtonB;
   
   digitalWrite(LED2, ledOnb);

}

At the moment, your serial and switch parts of the sketch are trying to control the leds independently instead of cooperating with each other.

I think you should try changing your sketch so that when serial codes are received, the variables ledOna and ledOnb are updated, instead of directly setting the leds using digitalWrite.

I am new at this, how do I do that?

As an example, you would change:

  if (Data==0x31)                
  {
    digitalWrite(LED1,HIGH);
    delay(10);
    Serial.println("LED pa");
  }

to

  if (Data==0x31)                
  {
    ledOna = True;
    delay(10);
    Serial.println("LED pa");
  }

Did not know I could do that, thank you.

Here is my code:

const int LED1 = 2;
const int LED2 = 3;
const int BUTTONA = 22;
const int BUTTONB = 24;
boolean lastButtonA = LOW;
boolean lastButtonB = LOW;
boolean currentButtonA = LOW;
boolean currentButtonB = LOW;
boolean ledOna = false;
boolean ledOnb = false;
int Data = 0;


void setup()
{
  Serial.begin(9600);
  pinMode (LED1, OUTPUT);
  pinMode (LED2, OUTPUT);
  pinMode (BUTTONA, INPUT);
  pinMode (BUTTONB, INPUT);
}


boolean debounce(boolean last)
{
  boolean currenta = digitalRead(BUTTONA);
   if (last != currenta)
   {
     delay(5);
     currenta = digitalRead(BUTTONA);
   }
    return currenta;
}

boolean debounceb(boolean lastb)
{
  boolean currentb = digitalRead(BUTTONB);
   if (lastb != currentb)
   {
     delay(5);
     currentb = digitalRead(BUTTONB);
   }
    return currentb;
}


void loop()
{
  Serial.println("\nLED 1\tLED 2");
  Serial.print(ledOna);
  Serial.print("\t");
  Serial.println(ledOnb);
  
  Data=Serial.read();            
  
  if (Data==0x31)                
  {
    ledOna = true;
  }
   else if (Data==0x32)         
   {
     ledOna = false;
   }
  
  if (Data==0x33)                
  {
    ledOnb = true;
  }
   else if (Data==0x34)         
   {
     ledOnb = false;
   }
  
  currentButtonA = debounce(lastButtonA);
   if (lastButtonA == LOW && currentButtonA == HIGH)
   {
     ledOna = !ledOna;
   }
   lastButtonA = currentButtonA;
   
   digitalWrite(LED1, ledOna);
   
   currentButtonB = debounceb(lastButtonB);
   if (lastButtonB == LOW && currentButtonB == HIGH)
   {
     ledOnb = !ledOnb;
   }
   lastButtonB = currentButtonB;
   
   digitalWrite(LED2, ledOnb);

}

Only one thing remaining, stopping it from reseting its settings when serial com. starts.

Tricky. When serial comms start up, the Arduino gets reset and its ram memory (where variables like ledOna are stored) gets wiped. You will need to store those variables in a place that does not get wiped. Have a look at the EEPROM library. You would use EEPROM.write to store the values of ledOna and ledOnb each time and only when you update them (EEPROM memory can only be written about 100,000 times). You can then use EEPROM.read to get the values back into ledOna and ledOnb in your startup function.

Ahh ok, them I leave it.
But are going to make the arduino restart when serial com. ends.

No, I don't think so.

Another option for you would be to disable the reset line. You can do this by placing a large cap like 10uF from reset to gnd. You will then have to remove this each time you want to upload your sketch.

ah ok, will make a function on the app for that problem.
Thank you for all your help.