Go Down

Topic: Code Help? (Read 237 times) previous topic - next topic

adam1997gipe

Ok, I'm new to programming. This code is to run an optical tachometer and I'm having trouble tracking down my errors. Can someone help? This code is a combination of many sample codes I found online for a similar task and I've been searching through errors for a few hours now. Can you please show the change and explain how I went wrong so I don't do it again? Thanks!

Code: [Select]


#include <LiquidCrystal.h>

LiquidCrystal (8,10,9);    // DEFINE LCD PINS

volatile byte REV;       //  VOLATILE DATA TYPE TO STORE REVOLUTIONS
 
unsigned long int rpm, maxRPM;  //  DEFINE RPM AND MAXIMUM RPM
 
unsigned long time;         //  DEFINE TIME TAKEN TO COVER ONE REVOLUTION
 
int ledPin = 12;           //   STATUS LED
 
int led = (0),RPMlen , prevRPM;  //  INTEGERS TO STORE LED VALUE AND CURRENT RPM AND PREVIOUS RPM
 
int flag = (0);             //  A VARIABLE TO DETERMINE WHETHER THE LCD NEEDS TO BE CLEARED OR NOT

long prevtime = (0);       //  STORE IDLE TIME TO TOGGLE MENU
   
 void setup();
 }
 
     Serial.begin(9600);   // GET VALUES USING SERIAL MONITOR
     
     LiquidCrystal.begin(16, 2);     // INITIATE LCD
     
     attachInterrupt(0, RPMCount, RISING);     //  ADD A HIGH PRIORITY ACTION ( AN INTERRUPT)  WHEN THE SENSOR GOES FROM LOW TO HIGH
     
     REV = (0);      //  START ALL THE VARIABLES FROM 0
     
     rpm = (0);
     
     time = (0);
     
     pinMode(ledPin, OUTPUT);
     
     pinMode(3, OUTPUT);           
     
     pinMode(4, OUTPUT);
     
     digitalWrite(3, HIGH);             //  VCC PIN FOR SENSOR
     
     digitalWrite(4, LOW);              // GND PIN FOR SENSOR
     
     LiquidCrystal.print("TACHOMETER");           //   STARTUP TEXT
     LiquidCrystal.setCursor(0, 1);
     LiquidCrystal.print("- Adam Gipe");          //  THAT'S ME
     delay(2000);
     LiquidCrystal.clear();
 
 
 void loop();
 {
  long currtime = millis();                 // GET CURRENT TIME
 
  long idletime = currtime - prevtime;        //  CALCULATE IDLE TIME
   
    if(REV >= 5 ) ;                    //  IT WILL UPDATE AFETR EVERY 5 READINGS
   
     
   }         ;
     if(flag==0)  ;                   //  CLEAR THE LCD TO AVOID ANY GARBAGE TEXT
     {
       LiquidCrystal.clear();
       LiquidCrystal.print("SENSOR MEASURING");
       flag=(1);                          //   AFTER FLAG = 1 , THE LOOP WILL NOT EXECUTE AGAIN
     }
     
     rpm = 30*1000/(millis() - time)*REV;       //  CALCULATE  RPM USING REVOLUTIONS AND ELAPSED TIME
     
     if(rpm > maxRPM);
     maxRPM = rpm;                             //  GET THE MAX RPM THROUGHOUT THE RUN
   
     time = millis();                           
     
     REV = 0;
     
     int x= rpm;                                //  CALCULATE NUMBER OF DIGITS IN RPM
     while(x!=0);
     {
       x = x/10;
       RPMlen++;
     } ;     
         
     
     
     if(RPMlen!=prevRPM);                             // IF THE RPM FALLS TO A LOWER NUMBER WITH LESS DIGITS , THE LCD WILL GET CLEARED
     {
       LiquidCrystal.clear();
       prevRPM = RPMlen;
       flag=(0);
       LiquidCrystal.print("SENSOR MEASURING");
     
     
     LiquidCrystal.setCursor(0, 1);
     LiquidCrystal.print(rpm,DEC);                        //  PRINT RPM IN DECIMAL SYSTEM
     
     LiquidCrystal.setCursor(6,1);
     LiquidCrystal.print("RPM");
     delay(500);
     
     prevtime = currtime;                        // RESET IDLETIME
   
   
   
   if(idletime > 5000 );                      //  IF THERE ARE NO READING FOR 5 SEC , THE SCREEN WILL SHOW MAX RPM
   
     
     if(flag==1);                            // CLEAR THE LCD
     {
       LiquidCrystal.clear();
       flag=(0);
     };
     
     LiquidCrystal.clear();
     LiquidCrystal.print("MAXIMUM RPM");
     LiquidCrystal.setCursor(0, 1);
     LiquidCrystal.print(maxRPM,DEC);                     // DISPLAY MAX RPM
     LiquidCrystal.print("   RPM");
     delay(2000);
     LiquidCrystal.clear();
     LiquidCrystal.print("IDLE STATE");
     LiquidCrystal.setCursor(0, 1);
     LiquidCrystal.print("READY TO MEASURE");
     delay(2000);
     prevtime = currtime;
   
     

 
 void RPMCount() ;                               // EVERYTIME WHEN THE SENSOR GOES FROM LOW TO HIGH , THIS FUNCTION WILL BE INVOKED
 {
   REV++;                                         // INCREASE REVOLUTIONS
   
   if (led == LOW);
   {
     
     led = HIGH;                                 //  TOGGLE STATUS LED
   } ;
   
   else
   {
     led = LOW;
   };
   digitalWrite(ledPin, led);
 }

 //////////////////////////////////////////////////////////////  END OF THE PROGRAM  ///////////////////////////////////////////////////////////////////////


UKHeliBob

Code: [Select]

void setup();
 }

Function definitions do not have a semicolon on the end of the function names and the code they contain starts with an opening brace and ends with a closing brace.  Look at the setup() function in any of the Arduino examples and compare it with yours.

You have a similar problem with the loop() function but at least that has an opening brace even if its closing brace is in the wrong place.

Code: [Select]
if(REV >= 5 ) ;
Just because statements in C are terminated by a semicolon it does not mean that every line should be.  In this case if REV equals or is greater than 5 then the only code that will be executed is the semicolon, ie nothing.

I did not look any further but this looks wrong too, although it may depend on the exact library being used
Code: [Select]
LiquidCrystal (8,10,9);    // DEFINE LCD PINS


Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

Go Up