Go Down

Topic: Having trouble finding the error(s) (Read 728 times) previous topic - next topic

tacbag

Hi there,
I am trying to write my first code after copy pasting for a couple of days. I am having trouble finding the errors I have left behind in this script and was wondering if you guys could help me out a bit.
much appreciated.

this is the error:
error: expected unqualified-id before '{' token

Here is the code:

Code: [Select]
unsigned long time;

const int PIR_0 = 1;
const int PIR_1 = 2;
const int PIR_2 = 3;
const int uSonic = 6;

const int PWMPin = 4;
const int constantPower = 5;

//Random modus when people are not in a certain radius
int timer = random(40, 800);
int uValue = 400;
int pwmTimer = random(0, 100);
int pwmtimer2 = random (200, 255);


void Setup() {
  Serial.begin(9600);     

  pinMode(PIR_0, INPUT);
  pinMode(PIR_1, INPUT);
  pinMode(PIR_2, INPUT);

  pinMode(PWMPin, OUTPUT); //DCmotor pin for PWM the direction. PWM 126 = no movement, PWM 255 = backward PWM 0= forward
  pinMode(constantPower, OUTPUT); //DCmotor pin for activating the IC (LMD18200)

  digitalWrite (constantPower, HIGH);

}

void Loop() {

  long duration, cm;

  //calibration of the Ultrasonic sensor
  pinMode(uSonic, OUTPUT);
  digitalWrite(uSonic, LOW);
  delayMicroseconds(2);
  digitalWrite(uSonic, HIGH);
  delayMicroseconds(5);
  digitalWrite(uSonic, LOW);

  //the same pin is used to read the signal
  pinMode(uSonic,INPUT);
  duration = pulseIn(uSonic, HIGH);

  cm = microsecondsToCentimeters(duration);

  Serial.print(cm);
  Serial.print("cm");
  Serial.println();
  delay (200);
}
long microsecondsToCentimeters(long microseconds)
{
  return microseconds / 29 / 2;
}

//------------------------------------------------------------

int zahl = random(2);

{
if(PIR_0 == HIGH || zahl == 0) {
  analogWrite(PWMPin, 127);
  }
else if(uSonic <= uValue) {
  analogWrite(PWMPin, 255);

  }
else if(PIR_0 == HIGH || zahl ==1) {
  analogWrite(PWMPin, 0);
  }
else if(uSonic =< uValue) {
  analogWrite(PWMPin, 255);
  }
}
{
if (PIR_1 == HIGH) {
  analogWrite(PWMPin, 0);
  }
else if (uSonic <= uValue) {
  analogWrite(PWMPin, 127);
  }
}
{
if PIR_2 == HIGH {
  analogWrite(PWMPin, 255);
  }
else if (uSonic <= uValue) {
  analogWrite(PWMPin, 127);
  }
}


int moving = random(2);

{
if (moving = 0 || uSonic <= uValue) {
  analogWrite(pwmTimer);
  }
delay(timer);
analogWrite(PWMPin, 127);

if else (moving= 1 || uSonic <= uValue) {
  analogWrite(pwmTimer2);
  }
delay(timer);
analogWrite(PWMPin, 127);
}


best regards,
Tom

tobyb121

The problem is everything after //-------- is not in a function.
The way your arduino runs the code is it starts with the setup function, then runs the loop function repeatedly, all of the code you have there is outside of a function, so you haven't defined a place for it to run. Code can only really be run inside of functions, C/C++ is not like scripting languages where it just goes through your file one line at a time.
One last thing your setup and loop function names needs to be lowercase (you currently have them as Setup and Loop).

Hope this helps,

Tobyb121

tacbag

Hi Tobyb121,

that helped a lot!! thanks for your help. I was in the assumption that it was in the loop function. I now made a new function and the errors are solved. Is that the way to do it or would it be better to put in the the loop function?

Tom


nickgammon

Typically you put related stuff into their own functions, and call them as required. Think of functions as chapters in a book, they make it easier to read.
Please post technical questions on the forum, not by personal message. Thanks!

More info: http://www.gammon.com.au/electronics

Go Up