0

there are a few similar questions like this on here, but I can't manage to get it working/ find a solution.

I'm used to java, in which I can construct and call classes fairly easily, but I can't get anything in arduino to work. After a dozen tip's I've constructed it like so:

#include <Servo.h>

Servo CameraServo1;
Servo CameraServo2;
int CameraServo1Pin = 2;
int CameraServo2Pin = 3;
int CameraAngle;
int CameraAngle2;
char incomingCharacter = 0; // Setting up a character variable

void setup() {
  Serial.begin(9600);
  CameraServo1.attach(CameraServo1Pin);
  CameraServo2.attach(CameraServo2Pin);
  //Initialize camera by setting the servo's to the default 90 degree position.
  init.camera();
}

void loop() {
  // Check if something is being received
  if (Serial.available() > 0)
  {
    // read the incoming character:
    incomingCharacter = Serial.read();
    Serial.println("OK!"); // Send OK back to laptop
    switch (incomingCharacter)
    {
      case 'w': // Case block
        ServoControl.forward();
        break;
      case 's': // Case block
        ServoControl.reverse();
        break;
    }
  }
}

class init
{
  public:
    camera();
};

init::camera()
{
  ServoControl.CameraAngle = 90;
  ServoControl.CameraAngle2 = 90;
  CameraServo1.write(90);
  CameraServo2.write(90);
}

class ServoControl
{
    public forward();
    public reverse();
};

ServoControl::reverse()
{
  CameraAngle = CameraAngle - 5;
  CameraAngle2 = CameraAngle2 - 5;
  CameraServo1.write(CameraAngle);
  CameraServo2.write(CameraAngle2);
}

ServoControl::forward()
{
  CameraAngle = CameraAngle + 5;
  CameraAngle2 = CameraAngle2 + 5;
  CameraServo1.write(CameraAngle);
  CameraServo2.write(CameraAngle2);
}

But I can't get it to call the function from the class (for example void camera from class init) without it giving me an error that camera in init is a void. I don't really understand how classes here work and why a void wouldn't go in a class. :/

dandan78
  • 13,328
  • 13
  • 64
  • 78
Novak
  • 15
  • 6
  • Why are you using classes at all? All your functions here are operating on global data, so why not just use global functions? – zstewart Oct 01 '15 at 08:07
  • It seems to met you need to start from the beginning, like a basic C++ tutorial, and then build on that. It's not going to be easy to just jump in and figure it out as you go along. – dandan78 Oct 01 '15 at 08:07
  • Better to start with a [good C++ book](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) rather than some Internet tutorial. – Andrew Oct 01 '15 at 10:21

2 Answers2

0

C++ is very much like Java in some regards, like you having to properly declare functions (member or non-members), including return type (which you don't do for the init::camera function).

In other regards C++ is very different from Java, like you not have to specify access/visibility for each member (like you do in the ServoControl class).

Also, both C++ and Java classes are abstract descriptions, not actual object instances, if you want to use a non-static member function in a class you need to create an instance of that class first.

You might want to check out The Definitive C++ Book Guide and List if you want to learn more about C++.


As noted by zstewart in a comment, in C++ you don't need classes at all. Functions can be stand-alone in the global scope, and in this case it looks like the classes aren't needed at all. If one still want to use some kind of scoping, then using a namespace might be a better idea. Like for example

namespace init
{
    void camera()
    {
        ...
    }
}

This can them be called like

init::camera();
Community
  • 1
  • 1
Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • 2
    None of his methods act on anything besides global data. This looks to me like a bad habit picked up from Java (classes required for everything), as the OP mentions Java as prior programming experience. – zstewart Oct 01 '15 at 08:10
  • Making the data global was simply a futile attempt at making it work (which shouldn't be needed) But indeed, I'm sure I'm carrying over multiple bad habits :p Namespace is exactly what I need! The classes were just for structure! – Novak Oct 01 '15 at 08:49
  • `namespace init { void camera() { CameraAngle = 90; CameraAngle2 = 90; CameraServo1.write(90); CameraServo2.write(90); } }` Will throw an error "init is not a class or namespace" when calling with init::camera(); or even camera::init() (by your example) – Novak Oct 01 '15 at 08:54
  • @Novak Sorry, my example was wrong, updated now. You need to define the namespace (and its containing functions) *before* you use them. Move then to before you call them. Again just like Java, things needs to be declared before you can use them. :) – Some programmer dude Oct 01 '15 at 09:02
-1

Review your syntax.

Here's a brief tutorial on how to write classes in Arduino:

https://www.arduino.cc/en/Hacking/LibraryTutorial