Arduino Scan Function

This is for another project beside the LED cube i started on. (I messed up real bad so I'm moving on). I need to get my Arduino to run a scan function. But I need it to run only once in the loop. I need it to send variables to other functions. It's kinda hard to explain, so i'll map it out a bit.

void setup(){

All this good setup stuff...

}

 

void loop(){

Scan function (run once in this whole loop, So like once it's run it never runs again) "The scan function provides base measurements"

Scan 2 function (This one is run indefinitely, It constantly scans the area)

Compare function (This compares data from Scan 2 and Scan to see if there is a difference.)

 

}

 

I though about using the for command. for (int i = 0, i <= 1, i++). But then I have problems with transmitting over 20 variables from the Scan function to the Compare function. So I'm stuck. If anyone knows how to run this once or knows how to transmit 20 variables out of a for command than tell me.

Hi RoboDragon,In Arduino

Hi RoboDragon,

In Arduino language commands are run sequentially and only once. So inside your void loop first it runs Scan, then Scan 2, then Compare, at which point it goes back to the top to do them all again. When you say you want Scan to only run once every loop it is already doing that.

Do you mean you want Scan to run only once in the program, until it is reset or loses power?

Or Scan only run when Compare says the data doesn`t match?

Run the Scan() function at

Run the Scan() function at the end of your setup(). Then, in your loop() run Scan2() and Compare().

This way Scan() will run only once at the beginning of the program and Scan2() and Compare() will run repeatedly.

Scan runs once in the

Scan runs once in the program until it resets or loses power

That’s a good idea, but how

That’s a good idea, but how do I get the variables from there to the loop function.

 

Also:

I just tried that with a simple LED code, and it skipped that function, is my code wrong or is the arduino wrong?

void setup() {                

  pinMode(2, OUTPUT);     

  pinMode(3, OUTPUT);

}

 

void scan(){

  digitalWrite(2,HIGH);

  delay (1000);

  digitalWrite(2,LOW);

  delay(1000);

}

 

void loop() {

 

  digitalWrite(3, HIGH); 

  delay(1000);             

  digitalWrite(3, LOW);  

  delay(1000);            

}

Global variables can be

Global variables can be modified from anywhere in the whole program. If you modify them in the setup function you can still use them later in loop.

The program above doesnt call Scan() at all. Sure it is there, but it never gets used in either the setup or loop which is why you dont see pin 2 change.

I realized that the scan

I realized that the scan function has to be in the loop, So this won’t work. I remembered that i needed a button to activate the scan, So i need an if function and a new way to run scan once.

If you need Scan() in the

If you need Scan() in the main loop but only want it to run once you can use this…

int doScan = 1; // declared with other global variables

void loop(){

while(doScan){

Scan();

doScan = 0;

}

Scan2();

Compare();

OtherStuff();

}

Just set doScan back to 1 when you want it to run again. Like in your button interrupt or polling loop. Sorry for the code formatting this text editor is horrible to use.

That’s Perfect! Thank you

That’s Perfect! Thank you very much!

Passing arguments or values or variables around

This problem of initializing vars bit me as well when I started coding in Processing.org. I learnt to define the vars even before setup(){ … } . I tried putting them in a section called init(), but that did not work. Now I just let them “hang up there in no man’s land”.

I think (it’s been a while) it is possible to call your scan() function from no man’s land as well. Just make sure to define your vars first.

// so to be clear:
int var1;
byte[] var2;
scan();

setup() {
// whatever
}

loop() {
// keep doing this
}

scan() {
// the clever bits you need only run once
}

Thanks for a detailed explanation Rik!

Yes Rik, this is the way we do it in Arduino. It’s funny, I always thought it is the intuitive way of programming. Declare your global variables, constants, defines (macros), includes (these should go first), etc. then in the setup() initialize stuff, call what ever functions you need to run once, like sensor calibration, etc. and finally in loop() call the functions that need to run repeatedly. All your custom functions go after that. But I remember programming like this in Bascom-AVR too and same thing in Lego NQC.