Pages: 1 [2] 3   Go Down
Author Topic: Need the void loop to run just once...  (Read 1882 times)
0 Members and 1 Guest are viewing this topic.
Des Moines, WA - USA
Offline Offline
God Member
*****
Karma: 25
Posts: 779
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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;

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

East Anglia (UK)
Offline Offline
Faraday Member
**
Karma: 108
Posts: 4013
May all of your blinks be without delay()
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
When I wrote
Quote
Declare them as global variables at the top of the program
it was meant to be a clue
Logged

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

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

Quote
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  smiley-wink
Logged

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

thanx a bunch lloyddean!

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

Logged

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

so what exactly should i change in the sketch?
Logged

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

so what exactly should i change in the sketch?
Declare the new variables at the top of the program before anything else then turn my psuedo-code into real code.

Did you write the code that you posted ?  If so, then you will know how to write an if statement to test whether a variable is true or false to determine whether a section of code should be run or not because it has ifs in it already.

One thing to look out for is if case 2 is true before case 1 and the case 2 code runs then it will not run again even after case 1 has been subsequently run when using the logic in my code.  After each case has run you will need to make sure that the next case can run by setting its 'flag' variable to false.
Logged

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

Mid-Atlantic, USA
Offline Offline
God Member
*****
Karma: 30
Posts: 514
"Remember kids, the only difference between Science and screwing around is writing it down." - Adam Savage
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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  smiley-wink

I know this is rapidly getting way out of scope for the question of the OP, but what a constant is and what a variable is really depends on the compiler. I don't know how this compiler works, but if I were designing one (not that I ever have, but I have coded directly in opcodes...) when translating down to opcodes, a variable could be translated to a memory pointer and a constant could be translated as a literal value. (Using the compiler to put that literal value everywhere the constant name is referenced.) Thus in this case you literally can't change the constant's value during execution without doing dangerous self-modifying code tricks, but the value at the memory location the variable pointer references can change. Two advantages to using constants are: any good compiler will yell at you if you try to change a constant, and usually at the opcode level it takes less clock cycles to act on a literal than a memory pointer because of the extra clock cycles it takes to actually look at some place other than the next location in the program counter.
Just saying smiley-wink
Logged


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

i´m getting nowhere with this :-( i´m stuck.

i know i´m asking alot here but can anyone please correct the sketch and explain please.. coffee´s on me ;-)
Logged

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

Apart from your original code have you got any where you have tried to do what has been suggested ?  Even if it does not work post it here and we will help you some more.
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

I have tried to laborate with the code you gave me and i´m not sure where to put it and gets all these error codes.
Logged

Offline Offline
Faraday Member
**
Karma: 57
Posts: 2764
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Use the juche idea.
Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 473
Posts: 18695
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

I have tried to laborate with the code you gave me and i´m not sure where to put it and gets all these error codes.

What is your code? What are the error codes?


How to use this forum
Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 473
Posts: 18695
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.

It is subject to naming restrictions, but a variable that is constant is like a moving thing that is fixed.
Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 473
Posts: 18695
Lua rocks!
View Profile
WWW
 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

What errors in your code? If you aren't prepared to read the "sticky" about how to post questions, I'm afraid we can't help you.
Logged

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

I´m sorry for being a bit of an handful but i´m not very good at programing yet and yeah i know this is way over my head but here is the idea

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);
      // want the loop to end here and go back to listening for commands from the phone
      break;
    case 2:
      digitalWrite(lock, HIGH);
      delay(1000);               // wait for a second
      digitalWrite(lock, LOW);
      digitalWrite(innebelysning, HIGH);
      delay(10000);               
      digitalWrite(innebelysning, LOW);
      break;
     // want the loop to end here and go back to listening for commands from the phone
      case 4:
      digitalWrite(varmare, HIGH);
      break;
      case 5:
      digitalWrite(bagage, LOW);
      break;
  }

}

Logged

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