How do I pass parameters from loop to setup function?

Please forgive my noobness, but how do I pass parameters from loop to setup function? eg:

void setup() { int Y; Y = X; // Y will use the parameter which is being passed from void loop }

void loop() { int X; X = 5; // the parameter to be passed on to void setup }

Basically I want to pass the parameter of X from from void loop to setup loop.

Read the doc again - your are confused ... setup is running only once, before the first loop so what you are asking makes no sense. Whatever you initialise in the loop will happen after the setup, so would not have the intended result anyway.

Setup is for setting things up, so do X=5 in the setup and The setup does not loop, as its name implies, what loops is the void loop() function

If you want to share variables between setup() and loop() then make them global (read about variables’ scope)

J-M-L: Read the doc again - your are confused ... setup is running only once, before the first loop so what you are asking makes no sense. Whatever you initialise in the loop will happen after the setup, so would not have the intended result anyway.

Setup is for setting things up, so do X=5 in the setup and The setup does not loop, as its name implies, what loops is the void loop() function

If you want to share variables between setup() and loop() then make them global (read about variables’ scope)

Thank you for the clarification. maybe i should provide some background on my current project which i hope can provide a better understanding.

im currently doing a project to control brushless DC motos using Arduino MEGA2560 and the PWM frequency determines the speed of the motors. with that, ive placed the following code into the setup function().

void setup(){

int f1; f1=8000; //inut freq of PWM

// setup for Timer4: change PWM frequency to 8000Hz for motor 1

TCCR4A = 0; TCCR4B = 0; TCNT4 = 0;

// Mode 10: phase correct PWM with ICR4 as Top (= F_CPU/2/8000) // OC4C as Non-Inverted PWM output ICR4 = (F_CPU/f1)/2; // F_CPU = 16Mega OCR4C = ICR4/2; // default: about 50:50 TCCR4A = _BV(COM4C1) | _BV(WGM41); TCCR4B = _BV(WGM43) | _BV(CS40); }

the following code allows me to vary the speed of motor by manually changing the value of f1 only, but unable to use values calculated in void loop(). since im unable to vary the speed using values from main loop, i would like to seek alternatives in varying the speed of the motors which requires the change of PWM frequency.

if you want to change the PWM frequency in the loop, then you need to re-issue the commands you had in the setup to control the timer. it won't happen auto-magically


Now that you are a seasoned member of the forum with 2 posts :) time to do the right thing - Please correct your posts above and add code tags around your code: [code][color=blue]// your code is here[/color][/code].

It should look like this:// your code is here (Also press ctrl-T (PC) or cmd-T (Mac) in the IDE before copying to indent your code properly)

Put your code into a function and then you can call the function from setup() and from loop()

...R