Pages: [1] 2 3   Go Down
Author Topic: Need the void loop to run just once...  (Read 2395 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 15
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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;
  }

}
Logged

Johannesburg. UTC+2
Offline Offline
Faraday Member
**
Karma: 108
Posts: 4743
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

The Elders of the Internet know who I am
I'm on LinkedIn: http://www.linkedin.com/in/jimbrownza

East Anglia (UK)
Offline Offline
Faraday Member
**
Karma: 118
Posts: 4352
May all of your blinks be without delay()
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 :

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
Logged

Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

Offline Offline
Newbie
*
Karma: 0
Posts: 15
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

New Jersey
Offline Offline
Faraday Member
**
Karma: 70
Posts: 3743
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 15
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Des Moines, WA - USA
Offline Offline
God Member
*****
Karma: 25
Posts: 779
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Offline Offline
Newbie
*
Karma: 0
Posts: 15
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 15
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

what do you mean lloyddean?
Logged

East Anglia (UK)
Offline Offline
Faraday Member
**
Karma: 118
Posts: 4352
May all of your blinks be without delay()
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

Des Moines, WA - USA
Offline Offline
God Member
*****
Karma: 25
Posts: 779
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

what do you mean lloyddean?

Replies 2 and 4 go unanswered!
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 15
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 15
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Des Moines, WA - USA
Offline Offline
God Member
*****
Karma: 25
Posts: 779
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Manchester (England England)
Offline Offline
Brattain Member
*****
Karma: 637
Posts: 34581
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

@jompaohh
The important thing is that you need to know a bit to understand any answer.
Quote
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
Code:
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 (226.76 KB, 480x373 - viewed 35 times.)
Logged

Pages: [1] 2 3   Go Up
Jump to: