Go Down

Topic: new to programming, how would i combine scripts? (Read 1 time) previous topic - next topic

benneh

hey guys, im totally new to arduino, ive played with changing values in the example scripts to alter the affects on my circuits, but i want to combine two example scripts, to learn how i would do such a thing in my own scripts.

basically i want to combine something like the array script, and the loop script, they both use 6 leds so the circuit is already set up for it, but i want to go between the scripts, for example:  show the array script for 2 cycles, and then the loop script for 2 cycles, rinse and repeat

ive done a little coding in a game called "garrys mod" and im thinking this might consist of an if statement, and a value which counts up on each loop of the script and then the if statement would switch between the scripts and then set the value to 0 when the value reaches a certain number

am i on the right lines?

Theelectronicguy

#1
Jan 14, 2012, 02:05 am Last Edit: Jan 14, 2012, 02:09 am by Theelectronicguy Reason: 1
Cmon this is easy!:D Yes a if..else statement works fine or you could use the switch case statement.

Ex:
Code: [Select]
switch (Hello) {
    case 1:
      //do something when Hello equals 1
      break;
    case 2:
      //do something when Hello equals 2
      break;
    default:
      // if nothing else matches, do the default
      // default is optional
  }


So basically when the variable hello = 1, the arduino executes whats in the case 1. You could use the line hello++ to increment "hello" by one when you want the second case to happen.

PaulS

Quote
am i on the right lines?

Without seeing the two scripts that work separately, and your attempt to combine them, how can we say?
The art of getting good answers lies in asking good questions.

Theelectronicguy

I think he means the two led scripts example that come with the arduino. So it should be pretty straightforward.

PaulS

Quote
I think he means the two led scripts example that come with the arduino. So it should be pretty straightforward.

Certainly it should be straightforward. But, lots of examples come with the Arduino IDE. We don't know which two OP is trying to combine, or how they have been mashed up.

We can't really help until we know that.
The art of getting good answers lies in asking good questions.

Theelectronicguy

Quote
show the array script for 2 cycles, and then the loop script for 2 cycles


What i guessed, its the arrays example and the forloopiteration.

benneh


Cmon this is easy!:D Yes a if..else statement works fine or you could use the switch case statement.

Ex:
Code: [Select]
switch (Hello) {
    case 1:
      //do something when Hello equals 1
      break;
    case 2:
      //do something when Hello equals 2
      break;
    default:
      // if nothing else matches, do the default
      // default is optional
  }


So basically when the variable hello = 1, the arduino executes whats in the case 1. You could use the line hello++ to increment "hello" by one when you want the second case to happen.

it probably is easy, but ive never had a strong hand in coding, am i right to assume theres an example script which explains switches? thanks for the advice too :D


Quote
I think he means the two led scripts example that come with the arduino. So it should be pretty straightforward.

Certainly it should be straightforward. But, lots of examples come with the Arduino IDE. We don't know which two OP is trying to combine, or how they have been mashed up.

We can't really help until we know that.


sorry about that, i thought i sounded more clear than that, Theelectronicguy got it spot on, i was refering to the two example scripts int he library as he pointed out, but they were just example scripts

Go Up