Pages: [1]   Go Down
Author Topic: Controll LED with Serial Write and/or push button - Help needed  (Read 763 times)
0 Members and 1 Guest are viewing this topic.
Norway
Offline Offline
Newbie
*
Karma: 0
Posts: 29
Hobby Project for working dogs and home.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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:

Code:
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:
Code:
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");
   }

}
« Last Edit: June 09, 2014, 03:04:48 pm by Kimlorentz » Logged

West Yorkshire, UK
Offline Offline
Edison Member
*
Karma: 43
Posts: 1372
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Norway
Offline Offline
Newbie
*
Karma: 0
Posts: 29
Hobby Project for working dogs and home.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

There you go, got in in Code tags smiley-cool
Logged

West Yorkshire, UK
Offline Offline
Edison Member
*
Karma: 43
Posts: 1372
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Norway
Offline Offline
Newbie
*
Karma: 0
Posts: 29
Hobby Project for working dogs and home.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.


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()
{
  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);

}
Logged

West Yorkshire, UK
Offline Offline
Edison Member
*
Karma: 43
Posts: 1372
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Norway
Offline Offline
Newbie
*
Karma: 0
Posts: 29
Hobby Project for working dogs and home.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

West Yorkshire, UK
Offline Offline
Edison Member
*
Karma: 43
Posts: 1372
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

As an example, you would change:
Code:
  if (Data==0x31)               
  {
    digitalWrite(LED1,HIGH);
    delay(10);
    Serial.println("LED pa");
  }

to

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

Norway
Offline Offline
Newbie
*
Karma: 0
Posts: 29
Hobby Project for working dogs and home.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Did not know I could do that, thank you.

Here is my code:

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.
« Last Edit: June 14, 2014, 03:50:53 am by Kimlorentz » Logged

West Yorkshire, UK
Offline Offline
Edison Member
*
Karma: 43
Posts: 1372
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
« Last Edit: June 14, 2014, 04:14:03 am by PaulRB » Logged

Norway
Offline Offline
Newbie
*
Karma: 0
Posts: 29
Hobby Project for working dogs and home.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

West Yorkshire, UK
Offline Offline
Edison Member
*
Karma: 43
Posts: 1372
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Norway
Offline Offline
Newbie
*
Karma: 0
Posts: 29
Hobby Project for working dogs and home.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Pages: [1]   Go Up
Jump to: