0

here is the code.

PROBLEM:- if I use for the loop the error comes "expected unqualified-id before for .......but if I use the void loop instead of for loop...the code is run without any error. Meanwhile, I needed for loop .

long int Distance = 0;  // Record the number of steps we've taken
//LOW LOGIC MOVEMENT BACKWARD and GEARS MOVE clocwise
// PIN 18 FOR DIRECTIRON
// PIN 19 FOR STEP  INPUT
int y= 10^-6;
long int k;
long int h= 1600;
void setup() {              
Serial.begin(9600);
  pinMode(18, OUTPUT);     
  pinMode(19, OUTPUT);
  pinMode(14, OUTPUT);
  pinMode(15, OUTPUT); 
  digitalWrite(18,LOW);
  digitalWrite(19, LOW);
 digitalWrite(14, LOW);
 digitalWrite(15, LOW);
}
for ( k=0; k<=h; k++)
{
  //if we are at the end of our move
  digitalWrite(19, HIGH);
  delayMicroseconds(100);        
  digitalWrite(19, LOW); 
  delayMicroseconds(100);
  Distance = Distance + 1;   // record this step
  //k=Distance;
//   Check to see 
  if (Distance ==h)
 {
   // We are! Reverse direction (invert DIR signal)
   if (digitalRead(18) == LOW)
    {
     digitalWrite(18, HIGH);
   }
   else
   {
     digitalWrite(18, LOW);
   }
//    Reset our distance back to zero since we're
 //   starting a new move
   Distance = 0;
  //   Now pause for half a second
    delay(1000);
  }
  } 
}
0

You need the void loop() function on your sketch. This function gets called directly after the setup. If it's not there, then the program cannot compile. You cannot put any execution code (means code other than declarations and such) outside of a function.

Depending on what exactly you want to achieve, you have 2 options:

  1. If you want the program to only run once and then stop until the next reset, you can put your code inside the setup() function and leave the void loop() function empty ( with this: void loop(){ } ).
  2. If you want the program to repeat itself indefinitely after the for loop, you can put the code inside the void loop() function.

Whatever you choose, you need the void loop() function.

Note: The void loop() function is part of the Arduino world. In general all programs for microcontroller in C/C++ have a function called main(). It is the function, that starts the execution of the program. Without it no program would actually run. The Arduino framework wraps this main() function up and gives you directly one function to execute the initialization code (void setup()) and one function, that runs after that and loops indefinitely (void loop()). This infinite loop is an important thing, because the microcontroller has no OS or such that he can return to. If you don't have an infinite loop there, the execution can go on to the next address in the program, where you don't know what it contains.


Also the line int y= 10^-6; does not, what you might think it does. In C/C++ the ^ is not the power function, but will do a bitwise OR operation. You will have to use a function for this, for example the pow(value1, value2) function. You can use google to find one of the many examples, how to use it.

And in the same line, (I assume as above) you want to tell Arduino to put a value much smaller than 1 (a value between 0 and 1) into an integer variable. It will contain 0. int means integer and cannot hold decimal numbers (number with something after the decimal point). It can hold numbers like -1,0,1,2,3 in the range from -32768 to 32767 (because it is 16 bits wide on the Arduino UNO and Mega). You will have to use the float type for this.

  • 1
    “10^-6” should be written 1e-6 rather than pow(10, -6). – Edgar Bonet Nov 29 '18 at 21:48
0

It looks like you intended the for-loop to run to completion once. If that is correct, just include it and its entire block of code within and at the end of the setup() function. You will still need to provide a loop() function but it can be empty:

void loop(){
   ;
}

If, instead, you wanted the for() loop to be run repeatedly, enclose it in the loop() function:

void loop(){
   for ( k=0; k<=h; k++ )
   {
   ...
   }
}

The setup() function runs once after a power up or reset. The loop function then gets called repeatedly, forever. Put your functional code in whichever one fits your purpose.

The reason for the error message is that, as you've written the for-loop, it is outside of a function, but executable code (as opposed to defining global variables) is only valid within a function.

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.