Need the void loop to run just once...

Hey guys.. i´m a totally noob to this and i really suck at programing but i would really like some help on this one!

I´ve got the Arduino Mega (AT mega 1280) bord and the JY-MCU BT and an 8 channel relay card.
The vision is to control each channel with my phone and i got that part to work but here´s the problem.
I want each code to run just once for ever function.. how in mordors pink goat cheese do i do that?
heres the code ( that i copied and modyfied)

const int unlock = 2;
const int innebelysning = 13;
const int lock = 3;
const int varmare = 4;
const int bagage = 5;

byte serialA;
void setup()
{
// initialize the serial communication:
Serial.begin(9600); //baud rate - make sure it matches that of the module you got:

pinMode(unlock, OUTPUT);
pinMode(innebelysning, OUTPUT);
pinMode(lock, OUTPUT);
pinMode(varmare, OUTPUT);
pinMode(bagage, OUTPUT);
}

void loop() {

if (Serial.available() > 0) {serialA = Serial.read();Serial.println(serialA);}

switch (serialA) {
case 1:
digitalWrite(unlock, HIGH);
delay(1000); // wait for a second
digitalWrite(unlock, LOW);
digitalWrite(innebelysning, HIGH);
delay(10000);
digitalWrite(innebelysning, LOW);

break;
case 2:
digitalWrite(lock, HIGH);
delay(1000); // wait for a second
digitalWrite(lock, LOW);
digitalWrite(innebelysning, HIGH);
delay(10000);
digitalWrite(innebelysning, LOW);
break;

case 4:
digitalWrite(varmare, HIGH);
break;
case 5:
digitalWrite(bagage, LOW);
break;
}

}

If I understand you correctly, you can move all that stuff from loop to setup, then it will only run once.

You'll need a "dummy" void loop() {} just to shut the compiler up.

Do you mean that once the code for a particular case has run once it should not run again even if the case is subsequently true ?
If so, then set a variable for each case to true when the code for a case has been run and check before running the case code that the variable is false. Pseudo code :

set case1Run = false
set case2Run = false

switch (serialA)
  case 1:
  if case1Run false
    your code here
  set case1Run = true
  end of if
  break;

  case 2:
  if case2Run false
    your code here
  set case2Run = true
  end of if
  break;

and so on

how do i do that exactly.. remember i´m a noob..

Should each case run once only ever? Or is your issue that once you get a character, it's case gets executed over and over? If the latter, set SerialA to zero at the end of loop.

should
set case1Run = false
set case2Run = false
be in the void setup?

You're being asked to clarify your needs and you ignore those requests and ...

for example..
i vant to run pinMode(unlock, OUTPUT);
pinMode(innebelysning, OUTPUT);
only once when i controlling it from the phone but the errors in my code makes it loop until end the northkoreans... hrrmf sorry.. end of time

what do you mean lloyddean?

jompaohh:
should
set case1Run = false
set case2Run = false
be in the void setup?

Declare them as global variables at the top of the program or they will not be valid in the loop() function. Remember, what I posted was not real code, just a skeleton to get you started.

Was what I suggested what you want to happen ? ie each action is allowed to happen only once (in any order)

Incidentally, it is not "in the void setup" but "in the setup function". The word void just indicates to the compiler that the function does not return a value to the main program.

jompaohh:
what do you mean lloyddean?

Replies 2 and 4 go unanswered!

really appreciate your´re trying to help UKHeliBob but i have no idea how global variables works and where to put it:-( like i said i´m a noob. want to help me with this one please

i´m not trying to be rude lloyddean... just posting after the answers... have patience with me...

// global constants exist for all other functions to acces
const int unlock = 2; 
const int innebelysning = 13;
const int lock = 3;
const int varmare = 4;
const int bagage = 5;


// globals variables exist for all other functions to acces

byte serialA;

void setup()
{
    // local variables only exist with in the '{}'
    int count = 0;
}

void loop()
{
    // local variables only exist with in the '{}'
    // this is NOT the same 'count' as specifide in 'setup'
    int count = 0;
}

@jompaohh
The important thing is that you need to know a bit to understand any answer.

but i have no idea how global variables works

That is not very encouraging as it suggests that you do not understand the code you posted one little bit.

All these variables you have declared as global

const int unlock = 2;
const int innebelysning = 13;
const int lock = 3;
const int varmare = 4;
const int bagage = 5;

Because they are declared out side of any function it means that they can be accessed by ALL functions.

You probably need to do a few tutorials to get the hang of things first.

What Part.png

Grumpy_Mike:
All these variables you have declared as global

const int unlock = 2;

const int innebelysning = 13;
const int lock = 3;
const int varmare = 4;
const int bagage = 5;

Actually these are constants and thus not variables ..., just saying.

jompaohh:
really appreciate your´re trying to help UKHeliBob but i have no idea how global variables works and where to put it:-( like i said i´m a noob. want to help me with this one please

When I wrote

Declare them as global variables at the top of the program

it was meant to be a clue

Actually these are constants and thus not variables ..., just saying.

No variable is the name of the thing they are. A constant is a type of variable, it is one that doesn't change its value. It is subject to all the naming restrictions as any variable that changes.

Note that it is not a literal constant which is just a number.
Just saying :wink:

thanx a bunch lloyddean!

have´nt got this all figured out all yet but...

so what exactly should i change in the sketch?