Unable to debug the programme

Hi,

I've been trying to execute, the below mentioned program for some reason I'm getting the error:

  1. expected primary-expression before '=' token
  2. error: expected ';' before '}' token
    3.Compilation error: expected primary-expression before '=' token
    and the code is
/*

*/

#define rotA  6
#define rotB  7
#define speed 


//define variables for the rotary function
int speedLimit = 0;
int posA;
int posB;

const int led = 13;
const int buttonPin = 2;
const int buzzer = 3;


//Variables for the Ultra Sensor 1
const int trigPin1 = 4;
const int echoPin1 = 5;
float duration1, distance1;

//Variables for Ultra Sensor 2
const int trigPin2 = 9;
const int echoPin2 = 10;
float duration2, distance2;

//Variable for calculating Speed
unsigned long time1;
unsigned long time2;
int delT;
float opp1, opp2, opp3;
float adj1, adj2, adj3;
float hyp, deladj, addopp;

float speed;



void setup() 
{
  pinMode(rotA, INPUT);
  pinMode(rotB, INPUT);
  
  pinMode(trigPin1, OUTPUT);
  pinMode(echoPin1, INPUT);
  
  pinMode(buzzer, OUTPUT);
  pinMode(led, OUTPUT);
  
  pinMode(buttonPin, INPUT);
  pinMode(led, OUTPUT);
  pinMode(buzzer, OUTPUT);
  
  Serial.begin(9600);
}

void loop() 
{
  posA = digitalRead(rotA);
  
  if(rotA != posB)
  {
    noTone(buzzer);
    if(digitalRead(rotB) != posA)
    {
      speedLimit = speedLimit + 5;
      tone(buzzer,349);
      delay(6);
      noTone(buzzer);
    }
    else
    {
      speedLimit = speedLimit - 5;
      tone(buzzer,330);
      delay(6);
      noTone(buzzer);
    }
    if(speedLimit < 0)
    {
      speedLimit = 0;
      tone(buzzer,440);
      delay(6);
      noTone(buzzer);
    }
    else if(speedLimit > 120)
    {
      speedLimit = 120;
      tone(buzzer,440);
      delay(6);
      noTone(buzzer);
    }
    noTone(buzzer);
     Serial.print("Speed Limit: ");
     Serial.println(speedLimit);
     Serial.println("kmph");

  }
  
  posB = posA;
  
  digitalWrite(trigPin1, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin1, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin1, LOW);

  duration1 = pulseIn(echoPin1, HIGH);
  distance1 = (duration1*.0343)/2;
  Serial.print("Distance: ");
  Serial.println(distance1);
  delay(100);
  
  digitalWrite(trigPin2, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin2, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin2, LOW);

  duration2 = pulseIn(echoPin2, HIGH);
  distance2 = (duration2*.0343)/2;
  Serial.print("Distance: ");
  Serial.println(distance2);
  delay(100);
  
  noTone(buzzer);
  if(distance1 < 180)
  {
    tone(buzzer,261);
    delay(300);
    noTone(buzzer);
    digitalWrite(led, HIGH);  // turn the LED on (HIGH is the voltage level)
    delay(300);                      // wait for a second
    digitalWrite(led, LOW);   // turn the LED off by making the voltage LOW
    delay(300); 
    digitalWrite(led, HIGH);  // turn the LED on (HIGH is the voltage level)
    delay(300);                      // wait for a second
    digitalWrite(led, LOW);   // turn the LED off by making the voltage LOW
    delay(300); 
    
    
    time1 = millis();
    delay(1000);     //we might have to remove this if the second sensor is not getting triggered
  }
  if(distance2 < 180)
  {
    tone(buzzer,392);
    delay(300);
    noTone(buzzer);
    digitalWrite(led, HIGH);  // turn the LED on (HIGH is the voltage level)
    delay(300);                      // wait for a second
    digitalWrite(led, LOW);   // turn the LED off by making the voltage LOW
    delay(300); 
    digitalWrite(led, HIGH);  // turn the LED on (HIGH is the voltage level)
    delay(300);                      // wait for a second
    digitalWrite(led, LOW);   // turn the LED off by making the voltage LOW
    delay(300); 
    
    time2 = millis();
    delay(1000);
  }
  
  if(time2 > time1)
  {
      delT = time2 - time1;
      delT = delT*3600000; //converting it to seconds
      
      opp1 = distance1/2; //opposite side, distance1, is hypoteneus
      opp1 = sq(opp1);
      distance1 = sq(distance1);
      adj1 =sqrt(distance1-opp1);
      distance1 = sqrt(distance1);
      opp1 = sqrt(opp1);
      
      opp2 = distance2/2; //opposite side, distance1, is hypoteneus
      opp2 = sq(opp2);
      distance2 = sq(distance2);
      adj2 =sqrt(distance2-opp2);
      distance2 = sqrt(distance2);
      opp2 = sqrt(opp2);
      
      if(adj1 > adj2)
      {
        adj3 = adj1-adj2;
        opp3 = opp1+opp2;
        adj3 = sq(adj3);
        opp3 = sq(opp3);
        hyp = sqrt(adj3+opp3);
      }
      else
      {
        adj3 = adj2-adj1;
        opp3 = opp1+opp2;
        adj3 = sq(adj3);
        opp3 = sq(opp3);
        hyp = sqrt(adj3+opp3);        
      }  
    speed = hyp / delT; 
  }
  else
  {
      delT = time1 - time2;
      delT = delT*3600000; //converting it to seconds
      opp1 = distance1/2; //opposite side, distance1, is hypoteneus
      opp1 = sq(opp1);
      distance1 = sq(distance1);
      adj1 =sqrt(distance1-opp1);
      distance1 = sqrt(distance1);
      opp1 = sqrt(opp1);
      
      opp2 = distance2/2; //opposite side, distance1, is hypoteneus
      opp2 = sq(opp2);
      distance2 = sq(distance2);
      adj2 =sqrt(distance2-opp2);
      distance2 = sqrt(distance2);
      opp2 = sqrt(opp2);
      
      if(adj1>adj2)
      {
        adj3 = adj1-adj2;
        opp3 = opp1+opp2;
        adj3 = sq(adj3);
        opp3 = sq(opp3);
        hyp = sqrt(adj3+opp3);
      }
      else
      {
        adj3 = adj2-adj1;
        opp3 = opp1+opp2;
        adj3 = sq(adj3);
        opp3 = sq(opp3);
        hyp = sqrt(adj3+opp3);        
      }
    
  }
  
  speed = hyp/delT;

  if(speed > speedLimit)
  {
    tone(buzzer,261);
    delay(300);
    tone(buzzer,392);
    delay(300);
    digitalWrite(led, HIGH);  // turn the LED on (HIGH is the voltage level)
    delay(300);                      // wait for a second
    digitalWrite(led, LOW);   // turn the LED off by making the voltage LOW
    delay(300);   
    
  }
  else
  {
    noTone(buzzer);
    digitalWrite(led, LOW)
  }
  

  
  Serial.print("Speed Limit: ");
  Serial.println(speedLimit);
  Serial.print("Speed: ");
  Serial.println(speed);

}


What is the problem with this code?
please help

This is the part I'm getting error

speed is #defined but you do not give it a value

#define speed

so the line that gives the error becomes

= hyp / delT;

Hence the error

Did you intend speed to be a variable rather than a definition ?

And this is why

#define speed 

maybe. That means replace all occurrences of speed with nothing.

Remove that line and see what happens. There may be other issues, but that's a show stopper.

a7

I removed define, the error went, but the new error is speed was not declared

As I said

If so then you have to declare it

I think I've defined and declared it as float, does that cause an error. I changed the name, the code works

Review the meanings of the words you are using, and post the code that now works.

Declaring and defining variables do not usually involve the use of the preprocessor

# define

directive.

a7

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.