Go Down

Topic: working with classes (Read 422 times) previous topic - next topic

hilukasz

Trying to get classes to work, but for some reason I keep getting this error: "Door does not name a type." Any ideas?

Code: [Select]
class Door {
  public:
    boolean opened;
    int peopleCounter;
   
    Door() {}
   
    void checkState(int inches) {
      isOpen(inches) ? open() : close();
    }
 
  private:
    boolean isOpen(int inches) {
      return !opened && inches < 10;  //return true if passes
    }
   
    void open() {
      opened = true;
      peopleCounter++; 
    }
 
    void close() {
      opened = false;
    }
}

const int trigPin = 2;
const int echoPin = 4;

Door myDoor;

void setup() {
  Serial.begin(9600);
  myDoor = new Door();
}

void loop()
{
  long duration, inches, cm;
  // The sensor is triggered by a HIGH pulse of 10 or more microseconds.
  // Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
  pinMode(trigPin, OUTPUT);
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);

  // Read the signal from the sensor: a HIGH pulse whose
  // duration is the time (in microseconds) from the sending
  // of the ping to the reception of its echo off of an object.
  pinMode(echoPin, INPUT);
  duration = pulseIn(echoPin, HIGH);

  // convert the time into a distance
  inches = microsecondsToInches(duration);
  cm = microsecondsToCentimeters(duration);
 
  for(int i = 0; i < 11; i++){
    Serial.println(inches);
    inches++;
  }

  for(int i = 0; i < 11; i++){
    Serial.println(inches);
    inches--;
  }
 
  Serial.print(inches);
  Serial.print("in, ");
  Serial.print(cm);
  Serial.print("cm");
  Serial.println();
 
  delay(100);
 
  myDoor.checkState(inches);
}

long microsecondsToInches(long microseconds)
{
  // According to Parallax's datasheet for the PING))), there are
  // 73.746 microseconds per inch (i.e. sound travels at 1130 feet per
  // second).  This gives the distance travelled by the ping, outbound
  // and return, so we divide by 2 to get the distance of the obstacle.
  // See: http://www.parallax.com/dl/docs/prod/acc/28015-PING-v1.3.pdf
  return microseconds / 74 / 2;
}

long microsecondsToCentimeters(long microseconds)
{
  // The speed of sound is 340 m/s or 29 microseconds per centimeter.
  // The ping travels out and back, so to find the distance of the
  // object we take half of the distance travelled.
  return microseconds / 29 / 2;
}
for(i = 0, i < 820480075, i++){ Design(); Code(); delay(1000); } // hellowoo.com

Arrch

Code: [Select]
class Door {
  ...
} // <-- Missing a semicolon

...

void setup() {
  Serial.begin(9600);
  myDoor = new Door(); // <-- Remove this line
}


See the comments

hilukasz

ah, had no idea you needed a semicolon there, weird. Is there a way to put this into a header file to keep it cleaner too?

maybe something like moving all the class files to doorClass.h and then including it:
#include "doorClass.h"

this seems to throw same error, that Door doesn't work when I try to put it in a different file. The file is in the same directory as the other one.
for(i = 0, i < 820480075, i++){ Design(); Code(); delay(1000); } // hellowoo.com

Arrch

In the header file, you need to either include "Arduino.h" or change your boolean types to bool types. The former is probably better, particularly if you want to add Arduino specific functions to your class like digitalWrites and such.

hilukasz

it doesn't seems to register the header file correctly, but is including it because I get a error of class is redefined if I copy the class to both files. here is what i would expect works:

main file:
Code: [Select]
/* HC-SR04 Sensor
   https://www.dealextreme.com/p/hc-sr04-ultrasonic-sensor-distance-measuring-module-133696
   
   The circuit:
* VCC connection of the sensor attached to +5V
* GND connection of the sensor attached to ground
* TRIG connection of the sensor attached to digital pin 2
* ECHO connection of the sensor attached to digital pin 4
*/
#include "doorClass.h"

const int trigPin = 2;
const int echoPin = 4;

Door myDoor;

void setup() {
  Serial.begin(9600);
  //myDoor = new Door();
}

void loop()
{
  long duration, inches, cm;
  // The sensor is triggered by a HIGH pulse of 10 or more microseconds.
  // Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
  pinMode(trigPin, OUTPUT);
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);

  // Read the signal from the sensor: a HIGH pulse whose
  // duration is the time (in microseconds) from the sending
  // of the ping to the reception of its echo off of an object.
  pinMode(echoPin, INPUT);
  duration = pulseIn(echoPin, HIGH);

  // convert the time into a distance
  inches = microsecondsToInches(duration);
  cm = microsecondsToCentimeters(duration);
 
  /*
  for(int i = 0; i < 11; i++){
    Serial.println(inches);
    inches++;
  }

  for(int i = 0; i < 11; i++){
    Serial.println(inches);
    inches--;
  }
  */
 
  Serial.print(inches);
  Serial.println("in");

 
  delay(300);
 
  myDoor.checkState(inches);
  Serial.print("doorSwing: ");
  int doorSwings = myDoor.doorSwings();
  Serial.println(doorSwings);
}

long microsecondsToInches(long microseconds)
{
  // According to Parallax's datasheet for the PING))), there are
  // 73.746 microseconds per inch (i.e. sound travels at 1130 feet per
  // second).  This gives the distance travelled by the ping, outbound
  // and return, so we divide by 2 to get the distance of the obstacle.
  // See: http://www.parallax.com/dl/docs/prod/acc/28015-PING-v1.3.pdf
  return microseconds / 74 / 2;
}

long microsecondsToCentimeters(long microseconds)
{
  // The speed of sound is 340 m/s or 29 microseconds per centimeter.
  // The ping travels out and back, so to find the distance of the
  // object we take half of the distance travelled.
  return microseconds / 29 / 2;
}


doorClass.h:

Code: [Select]

#include "Arduino.h"

class Door {
  public:
    boolean opened;
    int peopleCounter;
    int doorSwing;
 
    void checkState(int inches) {
      isOpen(inches) ? open() : close();
    }
    int doorSwings(){
      return doorSwing; 
    }
  private:
    boolean isOpen(int inches) {
      return !opened && inches < 10;  //return true if passes
    }
   
    void open() {
      Serial.print("opened!");
      if(doorSwing % 2 == 0){
        opened = true;
        peopleCounter++; 
        doorSwing++;
      }
    }
    void close() {
      Serial.print("closed!");
      opened = false;
      doorSwing++;
    }
    //Serial.print("doorSwing: ");

};
for(i = 0, i < 820480075, i++){ Design(); Code(); delay(1000); } // hellowoo.com

Arrch

Compiles fine for me.

Open up the Arduino IDE. Post your main code into the window. Click the Down arrow on the right and select New Tab. Name it as doorClass.h. Paste the class portion of the code into that new tab and hit compile.

hilukasz

ah, you know what I just renamed ino file to .h apparently you cannot do this. works when I make a new project. thanks!
for(i = 0, i < 820480075, i++){ Design(); Code(); delay(1000); } // hellowoo.com

PaulS

Quote
doorClass.h:

It's generally a good idea if the name of the file is the same as the name of the class. This file should be called Door.h (and there should be a Door.cpp file, too). The implementation of the class should not be in the header file.

Go Up