Go Down

Topic: I Need Help With My Code (Read 215 times)previous topic - next topic

SetSuko

Dec 10, 2018, 07:30 pm
This Is A Code I Grabbed From A Tutorial Online And I Have Trouble With It.
when i want to upload the code to my arduino uno it says
Quote
expected primary-expression before 'int'
i included a comment where i have that problem, can someone fix it for me?
i am not that good of a programmer and i am new to arduino

Code: [Select]
`/* Using an Arduino with a 120v RelayParts used:--ultrasonic sensor--desk lamp--120v relay--desk lightingThis sketch uses an ultrasonic sensor to detect whethera person in near or sitting near a desk, and sends a signal to a 120V relay to turn desk lighting on or off.*/#include <NewPing.h>const int LIGHT = 10;const int TRIGGER = 12;const int ECHO = 11;NewPing sonar(TRIGGER, ECHO);void setup(){  pinMode(LIGHT, OUTPUT); }/* The main loop iterates every 2.5 seconds. */void loop(){      /*In order to get a accurate measure of wether  someone is near, we read the ultrasonic sensor  30 times and take the average. That way, if the  ultrasonic sensor reads sporadically as it   occasionaly does, those values will be averaged out.    This takes 0.05 * 30 = 1.5 seconds to get a value  from the sensor. */    while (int i, i < 30, i++){                                     //!!!!!!!!!!THIS LINE IS MY PROBLEM!!!!!!!!!!    sum = sum + sonar.ping_cm();    delay(50);  }  int average_distance = sum/30;       /* Using the value, we send a signal to the relay  depending on whether someone is detected less than   130 cm away. We also pause for a second for good   measure. */    if (average_distance < 130){    digitalWrite(LIGHT, HIGH);    delay(1000);  }    else{       digitalWrite(LIGHT, LOW);    delay(1000);  }}`

PaulS

#1
Dec 10, 2018, 07:33 pm
Code: [Select]
`  while (int i, i < 30, i++){   `
This is nowhere near the correct format for a while statement.

It looks like you tried to use the syntax for a for loop, but changed the semicolons to commas. Look up what a while statement is supposed to look like.

terryking228

#2
Dec 10, 2018, 07:37 pm
Hi, Something missing here:

Code: [Select]
`while (int i, i < 30, i++){    `

think you need:

Code: [Select]
`while (int i=0; i < 30; i++){    `
Regards, Terry King terry@yourduino.com  - Check great prices, devices and Arduino-related boards at http://YourDuino.com
HOW-TO: http://ArduinoInfo.Info

UKHeliBob

#3
Dec 10, 2018, 07:38 pm
Quote
This Is A Code I Grabbed From A Tutorial Online
In view of the problems with it I would be interested in a link to where you got it from.
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

PaulS

#4
Dec 10, 2018, 07:39 pm
Quote
think you need
And I think YOU need to RTFM, too.

SetSuko

#5
Dec 10, 2018, 07:50 pmLast Edit: Dec 10, 2018, 07:51 pm by SetSuko
Hi, Something missing here:

Code: [Select]
`while (int i, i < 30, i++){    `

think you need:

Code: [Select]
`while (int i=0; i < 30; i++){    `

now it says "expected ')' before ';' token" at that same line in my code

AWOL

#6
Dec 10, 2018, 07:54 pm
Hi, Something missing here:

Code: [Select]
`while (int i, i < 30, i++){    `

think you need:

Code: [Select]
`while (int i=0; i < 30; i++){    `

That's syntax for a for loop, not a while.

Perehama

#7
Dec 10, 2018, 08:07 pmLast Edit: Dec 10, 2018, 08:08 pm by Perehama
Code: [Select]
`int i = 0;do {    sum = sum + sonar.ping_cm();    delay(50);   } while (++i < 30);`
The slender snake hissed as she slithered slowly over the rocks, grass and leaves.

Go Up