0

This is my first programme, which is to control a brushless ESC motor and an IR sensor to read the props' RPM. However I can't seem to figure out why it's not working. The reading keeps giving me 0, and after I edited the programme by changing while (millis()-start < 60000);to while (millis()-start < 1000);, it feeds me expected ';' before '{' token on if (start > millis(){, which makes no sense whatsoever. Here's the programme:

#include <Wire.h> 
#include <LiquidCrystal_I2C.h>
#include <Servo.h>

//Display&sensor
#define Backlight_Pin 13
int initial=0;
//int REV=0;
//int prevtime=0;
LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);

//ESC
Servo ESC;     // create servo object to control the ESC
int potValue;  // value from the analog pin

//Calculation
int sensorvalue;
int state1 = HIGH;
int state2;
float rps;
float rpm;
long prevMillis = 0;
long interval = 200;
long currentTime;
long prevTime = 1;
long diffTime;
int sensorthreshold = 1; 
int Sensor = 8;
int photoPin = 2;               //
int val = 0;
int valold = 0;
int start;
unsigned int count = 0;

void setup() {
  pinMode(Sensor,INPUT);
  valold = digitalRead(photoPin);
  lcd.begin(16, 2);
  lcd.clear();
  ESC.attach(9, 1000, 2000);  
  Serial.begin(9600);
}
void loop() {
  if (initial==0)
  {
    lcd.setCursor (6,0);
    lcd.print("RPM:");
    lcd.setCursor (0,1);
    initial=1;
  }
  //REV= digitalRead(Sensor);
  //lcd.print(REV);

  //Calculation
 start = millis(); // take the start time
 count = 0; // reset couter
 // do the following loop for 60000ms = 1min
 while (millis()-start < 1000);
 {  

    if (start > millis(){
      start = millis();
      count = 0;
    }
   val = digitalRead(photoPin);
   if (val==LOW)
   {
     digitalWrite(ledPin, LOW);
   }
   else
   {
     digitalWrite(ledPin, HIGH);
     if (val <> valold) {
       count ++;
       valold = val;
     }
   }
 }

      lcd.print("RPS = ");
      lcd.setCursor(7,1);
      lcd.print(count);
      delay(1000);
      lcd.setCursor(0,1);
      lcd.print(" ");

//ESC
  potValue = analogRead(A0);   // reads the value of the potentiometer (value between 0 and 1023)
  potValue = map(potValue, 0, 1023, 0, 180);   // scale it to use it with the servo library (value between 0 and 180)
  ESC.write(potValue);    // Send the signal to the ESC
}

And if I put ";" in front of "{", I get this error

expected primary-expression before '{' token

How could I fix this? Any help is appreciated. Thanks!

p.s. I had a rig holding the sensor and rotor stationary and waited 5 minutes for results, no luck, so I thought I would change the time to 1 second and measure RPS, then manually time the result by 60, but then the code broke. I don't think it was hardware failure as almost all joints are soldered, and the wires are solid copper.

  • 3
    You habe a missing closing bracket ) in the first if condition inside the while loop. Likely that caused the problem. Remove the extra semicolon, that you put in. – chrisl Jan 15 at 9:23
  • 2
    When the compiler is giving you errors, it has absolutely nothing to do with hardware. If the compiler shows an error, the sketch doesn't even get uploaded to the board – chrisl Jan 15 at 9:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.