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:
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;
}
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;
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.
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
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.
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
// 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:
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
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