Compile two sketches

Hi everyone. Newbie here and sorry for disturbing and if you mind can help me :smiley:
I was trying to compile two sketches(LCD & Servo ) into 1 Arduino UNO board. Anyone can help how to do that?
This is the sketch for LCD :

#include <LiquidCrystal.h>

LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

void setup() {
lcd.begin(16, 2);
lcd.print("Sunny Is A Panda");
}

void loop() {
lcd.setCursor(0, 1);
lcd.print(millis()/1000);
}

This is the sketch for Servo :

#include <Servo.h>

Servo myservo;

int pos = 0;

void setup()
{
myservo.attach(9);
}

void loop()
{
for(pos = 0; pos < 180; pos += 1)
{
myservo.write(pos);
delay(15);
}
for(pos = 180; pos>=1; pos-=1)
{
myservo.write(pos);
delay(15);
}
}

Thanks in advance !

You can't. You can write a sketch that does both things in the way you want. Of course you must decide what it is you want.

Mark

Means two function only for one accessory only? Cannot run 2 sketch in one board ?

You can't upload 2 different sketches,
But you can combine both sketches or create some header file to add multiple sketches

How to combine? mind teaching? sry for disturbing

You don't really ever combine sketches. What you do is to decide what things you want your sketch to do and then write it. Some times you can borrow code from other sketches.

The type of sketches you have posted show the way to control servos, leds, ect

Mark

Read Grumpy Mike's tutorial on merging code: Merging Code.

So merging code is the option right? Any errors below this?

#include <LiquidCrystal.h>

LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
Servo myservo;

int pos = 0;
void setup() {
lcd.begin(16, 2);
lcd.print("Sunny Is A Panda");
myservo.attach(9);
}

void loop() {
lcd.setCursor(0, 1);
lcd.print(millis()/1000);
for(pos = 0; pos < 180; pos += 1)
{
myservo.write(pos);
delay(15);
}
for(pos = 180; pos>=1; pos-=1)
{
myservo.write(pos);
delay(15);
}

Try it!. As you have not said (in any meaningful way) what you want we can't tell.

Mark

Any errors below this?

Yes. You used quote tags, not code tags. Do NOT use the stupid Copy For Forum option in the IDE. Ctrl-A (select all), Ctrl-C (copy), and Ctrl-V (paste) (after pressing the # icon above the happy faces) are all that you need.

PaulS:

Any errors below this?

Yes. You used quote tags, not code tags. Do NOT use the stupid Copy For Forum option in the IDE. Ctrl-A (select all), Ctrl-C (copy), and Ctrl-V (paste) (after pressing the # icon above the happy faces) are all that you need.

Okay thanks. I tried to compile and upload the sketch to my R3, but get this error :
avrdude: stk500_getsync(): not in sync: resp=0x00
I've checked everything, Correct port, Usb is plugged.
The sketch i used :

#include <LiquidCrystal.h>

LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
#include <Servo.h> 

Servo myservo;  

int pos = 0;     
void setup() {
  lcd.begin(16, 2);
  lcd.print("Sunny Is A Panda");
 myservo.attach(9);
}

void loop() {
  lcd.setCursor(0, 1);
  lcd.print(millis()/1000);
  for(pos = 0; pos < 180; pos += 1) 
  {                                   
    myservo.write(pos);               
    delay(15);                        
  } 
  for(pos = 180; pos>=1; pos-=1)      
  {                                
    myservo.write(pos);              
    delay(15);     }
}

Refer Trouble shoot problem FOr the above error.

Ok guys the sync error has solved. Successfully uploaded the sketch and both RX and TX flashed awhile after uploaded. But the current situation still the same as before uploading the sketch (LCD blank, servo motor moving). The LCD Screen didn't show what i typed, as the servo motor still works the same before the sketch is uploaded

Any idea?

Here's the sketch im using

#include <LiquidCrystal.h>

LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
#include <Servo.h> 

Servo myservo;  

int pos = 0;     
void setup() {
  lcd.begin(16, 2);
  lcd.print("Sunny Is A Panda");
 myservo.attach(9);
}

void loop() {
  lcd.setCursor(0, 1);
  lcd.print(millis()/1000);
  for(pos = 0; pos < 180; pos += 1) 
  {                                   
    myservo.write(pos);               
    delay(15);                        
  } 
  for(pos = 180; pos>=1; pos-=1)      
  {                                
    myservo.write(pos);              
    delay(15);     }
}

What does it do?
What did you expect it do do?
Did either of the two sketches work before you combined them?

tmd3:
What does it do?
What did you expect it do do?
Did either of the two sketches work before you combined them?

Hi there, thanks for the reply.
I'm trying to make the LCD displays the word " Sunny is a panda" at the same time making the servo motor functions and delay awhile, which both of the components are connected to a breadboard and then to the board.
The situation is still the same as before upload, which is the LCD still lights up but nothing was displayed, the servo motor keeps turning.
This is the components I'm using (note that some the shield pins isn't connected due to lacking of male- female pins)
The LCD lights up, and the Cytron servo motor is moving

A test for you to do. Take everything out of the loop() function and run the program. Does the LCD display anything ?

The image that I assume you posted is not visible but if you have removed some links to the LCD then all bets are off.

UKHeliBob:
A test for you to do. Take everything out of the loop() function and run the program. Does the LCD display anything ?

The image that I assume you posted is not visible but if you have removed some links to the LCD then all bets are off.

Ok, i took everything out from loop, and the LCD still remains blank but the servo motor stops moving. What's next?

Okay guys i tried to rejoin the pins but this time i just plugged the LCD Keypad Shield straight to the board. It works perfectly after i upload a LCD sketch. But when i rejoin them back to the breadboard, it just showing the white boxes. Any idea?

Dannysj:
,,, plugged the LCD Keypad Shield straight to the board. It works perfectly ,,, when i rejoin them back to the breadboard, it can't function again ... Any idea?

My first guess: wiring error.

Are you routing connections between the Arduino and the LCD via the breadboard? Can you tell us why?

tmd3:

Dannysj:
,,, plugged the LCD Keypad Shield straight to the board. It works perfectly ,,, when i rejoin them back to the breadboard, it can't function again ... Any idea?

My first guess: wiring error.

Are you routing connections between the Arduino and the LCD via the breadboard? Can you tell us why?

Wiring error? Mind to explain ?> @@

Yes I'm trying to build a connection between the LCD keypad, sensor shield, and the Uno via breadboard. The main reason I'm doing this to make a simple controller from the LCD keypad to input commands to the sensor shield, and before that I'm trying to simplify by routing them first.