where to learn programing complex operations?

hi i picked up a arduino uno last weekend did a few examples of other peoples tutorials and have them working. now for my next undertaking i want to combine three of the examples to work as one project and i am having trouble trying to figure out how to do it.

i have read the programming sticky on top of this page and i think it made me more lost....

where or what is the best way to learn to program this thing?

I sometimes get the feeling that guys think they can combine sketches in a way similar to combining HTML. Some simple cut and paste and "boom" there it is. The best way to combine the three sketches is to thoroughly understand how each sketch is working and write a fourth sketch using the concepts you've learned. You may be able to cut and paste bits and pieces but, essentially, you have to come up with a new sketch.

The best place to start is to write a description, in plain English (or whatever language you speak), that details the operation of the new sketch. Be very detailed and try to avoid sounding like you know anything about computers. It should make sense to your little sister if she reads it.

To combine 3 programs you can peek the program that is more close to what you want and add things slowly. I think the key is to work slowly and try to understand what your changes do. You have many things online that you can follow that explain what each part of the program do.

From your experience so far start with TWO IDE Examples and try to make them work together and add your own application to it.
For example use Communication "ASCIItable" and the "blink" examples to build a project which blinks the LED 13 converting random ASCII code to Morse code.
Don't just "cut and paste" them together - WRITE a pseudo-code and add code to it as you go.
Comment your code and use Serial print to track you code flow.
Good luck.

the examples that i have built have been singles none that are multi tier programs.
i have one that moves two servos randomly
i did another that i could move a servo with my ir remote
and a couple of examples with the lcd of displaying servo positions but these i couldn't get to work very well

here is the first step of what i am trying to accomplish

program layout

wait for ir remote
"ok" start and stop programs (standby)

if button 1 is pressed run random servo movements
vertical servo 40°-140°
horizontal servo 60°-110°
delay movements 1 second

if button 2 is pressed allow manual movements using up down left and right arrows of ir remote
if up arrow pressed move vertical servo 1°
if down arrow pressed move vertical servo -1°
if right arrow is press move horizontal servo 1°
if left arrow is press move horizontal servo -1°

map servo positions to a 16,2 lcd
line one: display vertical servo pos
line two: display horizontal servo pos

It's kinda of late here , so I'll just do "cut and paste".
You got the start and all three parts so let's just do what was suggested build another sketch:
Than take the "spec" and paste it into your code to build the app.
Not to much worry abut syntax or redundancy, the objective is to build the frame.
It obviously won't compile on first try.

/* 

project spec 

wait for ir remote
"ok" start and stop programs (standby)

if button 1 is pressed run random servo movements
vertical servo 40°-140°
horizontal servo 60°-110°
delay movements 1 second

if button 2 is pressed allow manual movements using up down left and right arrows of ir remote
if up arrow pressed move vertical servo 1°
if down arrow pressed move vertical servo -1°
if right arrow is press move horizontal servo 1°
if left arrow is press move horizontal servo -1°

map servo positions to a 16,2 lcd
line one: display vertical servo pos
line two: display horizontal servo pos  

*/

// project includes
// servos 
Servo servoHorizontal

//LCD library 

// defines 
#define DEBUG 

// global variables 


void setup() {
// initialize debug 
Serial.begin(115200);
Serial.print(__FILE__);


//  wait for ir remote
while(!ir)
{
// optional timeout 
}              


//  "ok" start and stop programs (standby)

....


//if button 1 is pressed run random servo movements
if(button1)
{
random(40,140);
Serial.print(random(40,140)); 
servo.vertical(random(40,140);

....
i
//vertical servo 40°-140°
// horizontal servo 60°-110°
//delay movements 1 second
}

....




}

void loop() {
  // put your main code here, to run repeatedly:

}

.

//if button 1 is pressed run random servo movements
if(button1)
{
random(40,140);
Serial.print(random(40,140));
servo.vertical(random(40,140);

Naturally, it won't compile. You're missing a right parenthesis.

//if button 1 is pressed run random servo movements
if(button1)
{
random(40,140);
Serial.print(random(40,140));
servo.vertical(random(40,140));

Here you're generating three, almost certainly different, random numbers. You're throwing away the first number, displaying the second number, and moving the servo to the position indicated by the third number. And that's if it compiles.

Vaclav is just offering hints. His code isn't meant to be compiled literally. He deliberately introduces syntax errors to force you to think about the underlying concepts.

Or to confuse an already confused beginner.

Programming is hard enough that there is absolutely no need to make it harder.

Feel free to use the "Report to moderator" link.

Look on the bright-side; at least there were code tags in the correct place.
I think that after nearly 1000 posts, we should view that as a positive.

This simple merge code demo may help.

Also have a look at planning and implementing a program to get a sense of how a more complex program is organized.

...R

even if there are errors its better for me to figure them out they look better then what i was trying to do with the copy and paste that was mentioned right off the bat....
i think i may be a little deeper then expected but i really want too learn how to do this properly

here is what i have so far

#include <LiquidCrystal.h>
#include <Servo.h>

LiquidCrystal lcd(12, 11, 5, 4, 3, 2); // initialize the library with the numbers of the interface pins
Servo vert; 
Servo hor;

void setup() 
{
  lcd.begin(16, 2); // define lcd size
  vert.attach(8);  //vertical servo data pin
  hor.attach(9);  // horizontal servo data pin
  lcd.print("vert:"); //static variable line 1 on lcd
  lcd.setCursor(0,1); //Move to second line
  lcd.print("hor:");  //static variable line 2 on lcd
  int pos = vert.read();   
  int pos2 = hor.read(); 
 }
void migrate(Servo & vert, int newPos) 
{
  int wait=random(30,60);
  int pos = vert.read(); //Read the current servo position
  if (pos < newPos) { 
    for (int i=pos; i < newPos; i++) {
      vert.write(i);
      delay(wait); 
  lcd.setCursor(6, 0); // set vert servo position lcd print location
  lcd.print(pos);
  lcd.print(" ");
    }
  } else { 
    for (int i=pos; i > newPos; i--) { 
      vert.write(i);
      delay(wait);
    }
  }
}
void migrate2 (Servo & hor, int newPos2) 
{
  int wait=random(30,60);
  int pos2 = hor.read(); //Read the current servo position
  if (pos2 < newPos2) { 
    for (int i=pos2; i < newPos2; i++) {
      hor.write(i);
      delay(wait); 
  lcd.setCursor(6, 1); // set vert servo position lcd print location
  lcd.print(pos2);
  lcd.print(" ");
    }
  } else { 
    for (int i=pos2; i > newPos2; i--) { 
      hor.write(i);
      delay(wait);
    }
  }
}
 void randomPosition() {
  int rand=random(70,110);
  migrate(hor, rand);
  
  rand=random(40,135); 
  migrate(vert, rand);
}   
  

   
void loop() { 
 randomPosition();
 delay(2000);
}

the lcd is printing corectly to line 1 but i have a static 90 on line 2 but the servo is moving?

If you are new to the Arduino (and even if you are not) I would advise getting the functional code (i.e. the servo stuff) to work first using the Serial Monitor for output and then add the LCD stuff later.

I would also separate the LCD code into its own function so that the servo code and the LCD code can both be tested separately.

...R

c0ryp1:
here is what i have so far

#include <LiquidCrystal.h>

#include <Servo.h>

LiquidCrystal lcd(12, 11, 5, 4, 3, 2); // initialize the library with the numbers of the interface pins
Servo vert;
Servo hor;

void setup()
{
  lcd.begin(16, 2); // define lcd size
  vert.attach(8);  //vertical servo data pin
  hor.attach(9);  // horizontal servo data pin
  lcd.print("vert:"); //static variable line 1 on lcd
  lcd.setCursor(0,1); //Move to second line
  lcd.print("hor:");  //static variable line 2 on lcd
  int pos = vert.read(); 
  int pos2 = hor.read();
}
void migrate(Servo & vert, int newPos)
{
  int wait=random(30,60);
  int pos = vert.read(); //Read the current servo position
  if (pos < newPos) {
    for (int i=pos; i < newPos; i++) {
      vert.write(i);
      delay(wait);
  lcd.setCursor(6, 0); // set vert servo position lcd print location
  lcd.print(pos);
  lcd.print(" ");
    }
  } else {
    for (int i=pos; i > newPos; i--) {
      vert.write(i);
      delay(wait);
    }
  }
}
void migrate2 (Servo & hor, int newPos2)
{
  int wait=random(30,60);
  int pos2 = hor.read(); //Read the current servo position
  if (pos2 < newPos2) {
    for (int i=pos2; i < newPos2; i++) {
      hor.write(i);
      delay(wait);
  lcd.setCursor(6, 1); // set vert servo position lcd print location
  lcd.print(pos2);
  lcd.print(" ");
    }
  } else {
    for (int i=pos2; i > newPos2; i--) {
      hor.write(i);
      delay(wait);
    }
  }
}
void randomPosition() {
  int rand=random(70,110);
  migrate(hor, rand);
 
  rand=random(40,135);
  migrate(vert, rand);
}

void loop() {
randomPosition();
delay(2000);
}




the lcd is printing corectly to line 1 but i have a static 90 on line 2 but the servo is moving?

Looks good, just add some comments. At least generally what the code is doing.
It may seems petty, but you want to learn complex code and when code get complex it is easy to forget what are you trying to accomplish.

I would follow your "spec" , that way you avoid disorganized spaghetti code.
The old - you have to have a direction where you going.
Things are also easier to change if you have a direction.

LCD is great debugging tool, my code has 80% LCD debug code in it!
BTW Hitachi some of the standard LCD "stuff" is "zero" numbered - your two liner have line 0 and line 1.
You may find out later that is is wise to always clear the LCD before you print to it.

Also start looking at your variables "scope" - when to use local variable and when you need globals.
It also looks more "professional " when you use variables of sufficient size just to do the job.
Good luck.

Robin2:
If you are new to the Arduino (and even if you are not) I would advise getting the functional code (i.e. the servo stuff) to work first using the Serial Monitor for output and then add the LCD stuff later.

I would also separate the LCD code into its own function so that the servo code and the LCD code can both be tested separately.

...R

what do you mean by this? the serial monitor servo stuff.

c0ryp1:
the serial monitor servo stuff.

I didn't quite describe it like that.

What I meant is that I would always get my servo code working using the Serial monitor to view any output (such as debug messages) before trying to add in code for an LCD.

When I have a working function to operate my servo I can treat it like a black box and concentrate on the other functions for the rest of the project.

...R

ok my servos are working correctly .. the only issue im faced with now is getting the second servo position to show up

c0ryp1:
ok my servos are working correctly .. the only issue im faced with now is getting the second servo position to show up

I was hoping you can this yourself, but here it is anyway.
Now you can see how important is to keep the direction - get the values first than display them.

Also you can see the display snippet is ripe for function.

I generally keep the development code commented out - you never know when you have senior moment and want the "old stuff " back.

I know darn well that in heat of coding one has a tendency to abbreviate things, but it is a bad habit to acquire.
If it is "horizontal position" name the variable HorizontalPosItion , not pos2.

Sometime it is called "self documenting code" or job security ( the "pos2") , depending on your employment status.

You doing OK.

void setup()
{
  lcd.begin(16, 2); // define lcd size
  vert.attach(8);  //vertical servo data pin
  hor.attach(9);  // horizontal servo data pin

  //Read the current servo position
   
  int pos = vert.read();
  int pos2 = hor.read();

  // display positions
  lcd..clear();
  lcd.print("vert:"); //static variable line 1 on lcd
  lcd.print(pos);
  lcd.setCursor(0, 1); //Move to second line
  lcd.print("hor:");  //static variable line 2 on lcd
  lcd.print(pos2);

  delay(5000);        // show and tell stop

  
  //  int pos = vert.read();
  //  int pos2 = hor.read();
}