Go Down

Topic: Iron Man: Two servos - CW and CCW at the same time with capacitive touch sensor! (Read 148 times) previous topic - next topic

H3M4N

Hey there, thanks for looking! I'd like some fresh eyes to help me find what I keep overlooking! First time posting, hope I understood the guidelines well enough!

The project I'm working on is an Iron Man helmet that uses two servos to open and close the face plate using an MPR121 capacitive touch sensor.

This is being edited from code used in this project:

https://create.arduino.cc/projecthub/29111/avenger-assembled-iron-man-helmet-arc-reactor-9db6ff

I thought I could duplicate the majority of the code (took out arc reactor parts from OG code) and create my oppositely running servo by adding an R (reverse) to the duplicates identifiers (e.g. helmetServo and helmetServoR for the other one). So far it seems like it should be just about there. I've ironed out issues for a few hours and am finally stuck with this error about classes:

exit status 1
'class HELMETR' has no member named 'UpdateR'

Without the duplicate R lines, the program compiled and ran. But I needed another servo on the other side for smoother rotation / stability. I could try to add another gear in the helmet to offset the rotations, but I'd like to solve it in the code if I can before going that extreme! Thank you for your time and patience with me, folks!

EDIT: fixed first error, thanks for catching that TheMemberFormerlyKnownAsAWOL!
Here is the most recent error:

exit status 1
'expected unqualified-id before numeric constant' highlighting '#define helmetPinR 10'

Code: [Select]
#include <Wire.h>
#include <Servo.h>
#include <Adafruit_MPR121.h>
/////////////////////////////////
#define helmetPin 9
#define helmetPinR 10
#define DOWNANGLE 0
#define UPANGLE 160
#define DOWNANGLER 0
#define UPANGLER 160
#define UP 1
#define DOWN 0
#define UPR 1
#define DOWNR 0
////////////////////////
Servo helmetServo;
int helmetPin;
boolean helmetUp;
long lastSwitched;
int downAngle;
int upAngle;

int directionality;
long lastFlipped;
boolean flip, flipping;

int currentPos;
long lastMoved;
//////////////////////////
Servo helmetServoR;
int helmetPinR;
boolean helmetUpR;
long lastSwitchedR;
int downAngleR;
int upAngleR;

int directionalityR;
long lastFlippedR;

int currentPosR;
long lastMovedR;
///////////////////////////
class HELMET
{
  public:

    HELMET(int pinNumber, boolean helmetState, int down, int up)
    {
      helmetPin = pinNumber;
      helmetUp = helmetState;
      downAngle = down;
      upAngle = up;
    }

    int getHelmetPin() {
      return helmetPin;
    }
    void setHelmetPin(int pinNumber) {
      helmetPin = pinNumber;
    }

    boolean getHelmetUp() {
      return helmetUp;
    }
    void setHelmetUp(boolean helmetState) {
      helmetUp = helmetState;
    }

    long getLastSwitched() {
      return lastSwitched;
    }
    void setLastSwitched(long timeSwitched) {
      lastSwitched = timeSwitched;
    }

    int getDownAngle() {
      return downAngle;
    }
    void setDownAngle(int angle) {
      downAngle = angle;
    }

    int getUpAngle() {
      return upAngle;
    }
    void setUpAngle(int angle) {
      upAngle = angle;
    }
    ////////////////////////////////////////////////////
    void init()
    {
      helmetServo.attach(helmetPin);
      helmetServo.write(upAngle);
      lastSwitched = millis();
      flip = false;
      flipping = false;
      lastFlipped = 0;
      lastMoved = 0;
    }
    ///////////////////////////////////////////////////
    void flipFaceplate()
    {
      if ((millis() - lastFlipped) > 500 )
      {
        lastFlipped = millis();
        if (flip)
        {
          flipping = true;
          currentPos = 0;
          directionality = UP;
        }
        else
        {
          flipping = true;
          currentPos = 160;
          directionality = DOWN;
        }
        flip = !flip;
      }
    }
    //////////////////////////////////////////////////////
    void Update()
    {
      if ( flipping & (millis() - lastMoved) >= 15 )
      {
        lastMoved = millis();
        if ( directionality == UP )
        {
          currentPos = currentPos + 3;
        }
        else
        {
          currentPos = currentPos - 3;
        }
        Serial.print("current position: "); Serial.print(currentPos);
        helmetServo.write(currentPos);
      }
      if ((currentPos >= upAngle) || (currentPos <= downAngle)) {
        flipping = false;
      }

    }
};
///////////////////////////////////////////////////////////////////////////////////////

///////////////////////////////////////////////////////////////////////////////////////
class HELMETR
{
  public:

    HELMETR(int pinNumberR, boolean helmetStateR, int downR, int upR)
    {
      helmetPinR = pinNumberR;
      helmetUpR = helmetStateR;
      downAngleR = downR;
      upAngleR = upR;
    }

    int getHelmetPinR() {
      return helmetPinR;
    }
    void setHelmetPinR(int pinNumberR) {
      helmetPinR = pinNumberR;
    }

    boolean getHelmetUpR() {
      return helmetUpR;
    }
    void setHelmetUpR(boolean helmetStateR) {
      helmetUpR = helmetStateR;
    }

    long getLastSwitchedR() {
      return lastSwitchedR;
    }
    void setLastSwitchedR(long timeSwitchedR) {
      lastSwitchedR = timeSwitchedR;
    }

    int getDownAngleR() {
      return downAngleR;
    }
    void setDownAngleR(int angleR) {
      downAngleR = angleR;
    }

    int getUpAngleR() {
      return upAngleR;
    }
    void setUpAngleR(int angle) {
      upAngleR = angle;
    }
    //////////////////////////////////////////////////////////
    void initR()
    {
      helmetServoR.attach(helmetPinR);
      helmetServoR.write(upAngleR);
      lastSwitchedR = millis();
      flip = false;
      flipping = false;
      lastFlippedR = 0;
      lastMovedR = 0;
    }
    //////////////////////////////////////////////////////////
    void flipFaceplateR()
    {
      int directionalityR;
      long lastFlippedR;
      boolean flip, flipping;
      int currentPosR;
      long lastMovedR;
      {
        if ((millis() - lastFlippedR) > 500 )
        {
          lastFlippedR = millis();
          if (flip)
          {
            flipping = true;
            currentPosR = 0;
            directionalityR = UPR;
          }
          else
          {
            flipping = true;
            currentPosR = 160;
            directionalityR = DOWNR;
          }
          flip = !flip;
        }
      }
      /////////////////////////////////////////////////////////
      void UpdateR()
      {
      if ( flipping & (millis() - lastMovedR) >= 15 )

      {
        lastMovedR = millis();
        if ( directionalityR == UP )
        {
          currentPosR = currentPosR + 3;
        }
        else
        {
          currentPosR = currentPosR - 3;
        }
        Serial.print("current position: "); Serial.print(currentPosR);
        Servo helmetServoR;
        helmetServoR.write(currentPosR);
      }
      int downAngleR;
      int upAngleR;
      if ((currentPosR >= upAngleR) || (currentPosR <= downAngleR)) {
        flipping = false;
      }
    }
};
///////////////////////////////////////////////////////////////
HELMET helmet(helmetPin, true, DOWNANGLE, UPANGLE);
HELMETR helmetR(helmetPinR, true, DOWNANGLER, UPANGLER);
/////////////////////////////////////////////////////////
Adafruit_MPR121 touchControl = Adafruit_MPR121();
uint16_t currTouched = 0;
///////////////////////////////////////////////////////////////
void setup(void)
{ Serial.begin(9600);
  helmet.init();
  if (!touchControl.begin(0x5A)) {
    Serial.println("MPR121 not found, check wiring?");
    while (1);
  }
  Serial.println("MPR121 found!");
}

void loop(void)
{
  currTouched = touchControl.touched();
  Serial.print("touched: "); Serial.println(currTouched);

  switch (currTouched)
  {
    case 1:
      helmet.flipFaceplate();
      helmetR.flipFaceplateR();
      break;
    case 2:
      break;
  }
  helmet.Update();
  helmetR.UpdateR();
};

TheMemberFormerlyKnownAsAWOL

#1
Aug 24, 2019, 11:45 pm Last Edit: Aug 24, 2019, 11:51 pm by TheMemberFormerlyKnownAsAWOL
Check your indentation - it looks to me like you've got functions defined within other functions.

It isn't really clear to me why you have two classes

Edit:
Code: [Select]
void UpdateR();
      if ( flipping & (millis() - lastMovedR) >= 15 )


Oops

H3M4N

I won't say it's the most efficient code by any means! Would it be better to combine classes instead of having them separate? Or can it be fixed as is, just uglier?


H3M4N

Ah, I see, oops is right. So with that fixed, I'm back at an earlier error I thought was fixed:

exit status 1
'helmetPin' is not a type

Do you know where these lines

HELMET helmet(helmetPin, true, DOWNANGLE, UPANGLE);
HELMETR helmetR(helmetPinR, true, DOWNANGLER, UPANGLER);

should be inside for this?


H3M4N

Ok, cool. Now I'm getting an error: 'expected unqualified-id before numeric constant' for this #define helmetPinR 10.

I don't understand this error, sorry and thanks so much, you're awesome.

EDIT: can't see code on the main post?



H3M4N

My bad, what do I need to do for you to see it so I don't mess anything else up

TheMemberFormerlyKnownAsAWOL

#10
Aug 25, 2019, 12:19 am Last Edit: Aug 25, 2019, 12:19 am by TheMemberFormerlyKnownAsAWOL
Post your code as it looks now.
In a new post.

Along with all of the error messages.

H3M4N

Quick repost cause I'm still learning how this works. Thanks, TheMemberFormerlyKnownAsAWOL!

Hey there, thanks for looking! I'd like some fresh eyes to help me find what I keep overlooking! First time posting, hope I understood the guidelines well enough!

The project I'm working on is an Iron Man helmet that uses two servos to open and close the face plate using an MPR121 capacitive touch sensor.

This is being edited from code used in this project:

https://create.arduino.cc/projecthub/29111/avenger-assembled-iron-man-helmet-arc-reactor-9db6ff

I thought I could duplicate the majority of the code (took out arc reactor parts from OG code) and create my oppositely running servo by adding an R (reverse) to the duplicates identifiers (e.g. helmetServo and helmetServoR for the other one). So far it seems like it should be just about there. I've ironed out issues for a few hours and am finally stuck with this error

exit status 1
'expected unqualified-id before numeric constant' highlighting '#define helmetPinR 10'


Without the duplicate R lines, the program compiled and ran. But I needed another servo on the other side for smoother rotation / stability. I could try to add another gear in the helmet to offset the rotations, but I'd like to solve it in the code if I can before going that extreme! Thank you for your time and patience with me, folks!

Code: [Select]
#include <Wire.h>
#include <Servo.h>
#include <Adafruit_MPR121.h>
/////////////////////////////////
#define helmetPin 9
#define helmetPinR 10
#define DOWNANGLE 0
#define UPANGLE 160
#define DOWNANGLER 0
#define UPANGLER 160
#define UP 1
#define DOWN 0
#define UPR 1
#define DOWNR 0
////////////////////////
Servo helmetServo;
int helmetPin;
boolean helmetUp;
long lastSwitched;
int downAngle;
int upAngle;

int directionality;
long lastFlipped;
boolean flip, flipping;

int currentPos;
long lastMoved;
//////////////////////////
Servo helmetServoR;
int helmetPinR;
boolean helmetUpR;
long lastSwitchedR;
int downAngleR;
int upAngleR;

int directionalityR;
long lastFlippedR;

int currentPosR;
long lastMovedR;
///////////////////////////
class HELMET
{
  public:

    HELMET(int pinNumber, boolean helmetState, int down, int up)
    {
      helmetPin = pinNumber;
      helmetUp = helmetState;
      downAngle = down;
      upAngle = up;
    }

    int getHelmetPin() {
      return helmetPin;
    }
    void setHelmetPin(int pinNumber) {
      helmetPin = pinNumber;
    }

    boolean getHelmetUp() {
      return helmetUp;
    }
    void setHelmetUp(boolean helmetState) {
      helmetUp = helmetState;
    }

    long getLastSwitched() {
      return lastSwitched;
    }
    void setLastSwitched(long timeSwitched) {
      lastSwitched = timeSwitched;
    }

    int getDownAngle() {
      return downAngle;
    }
    void setDownAngle(int angle) {
      downAngle = angle;
    }

    int getUpAngle() {
      return upAngle;
    }
    void setUpAngle(int angle) {
      upAngle = angle;
    }
    ////////////////////////////////////////////////////
    void init()
    {
      helmetServo.attach(helmetPin);
      helmetServo.write(upAngle);
      lastSwitched = millis();
      flip = false;
      flipping = false;
      lastFlipped = 0;
      lastMoved = 0;
    }
    ///////////////////////////////////////////////////
    void flipFaceplate()
    {
      if ((millis() - lastFlipped) > 500 )
      {
        lastFlipped = millis();
        if (flip)
        {
          flipping = true;
          currentPos = 0;
          directionality = UP;
        }
        else
        {
          flipping = true;
          currentPos = 160;
          directionality = DOWN;
        }
        flip = !flip;
      }
    }
    //////////////////////////////////////////////////////
    void Update()
    {
      if ( flipping & (millis() - lastMoved) >= 15 )
      {
        lastMoved = millis();
        if ( directionality == UP )
        {
          currentPos = currentPos + 3;
        }
        else
        {
          currentPos = currentPos - 3;
        }
        Serial.print("current position: "); Serial.print(currentPos);
        helmetServo.write(currentPos);
      }
      if ((currentPos >= upAngle) || (currentPos <= downAngle)) {
        flipping = false;
      }

    }
};
///////////////////////////////////////////////////////////////////////////////////////

///////////////////////////////////////////////////////////////////////////////////////
class HELMETR
{
  public:

    HELMETR(int pinNumberR, boolean helmetStateR, int downR, int upR)
    {
      helmetPinR = pinNumberR;
      helmetUpR = helmetStateR;
      downAngleR = downR;
      upAngleR = upR;
    }

    int getHelmetPinR() {
      return helmetPinR;
    }
    void setHelmetPinR(int pinNumberR) {
      helmetPinR = pinNumberR;
    }

    boolean getHelmetUpR() {
      return helmetUpR;
    }
    void setHelmetUpR(boolean helmetStateR) {
      helmetUpR = helmetStateR;
    }

    long getLastSwitchedR() {
      return lastSwitchedR;
    }
    void setLastSwitchedR(long timeSwitchedR) {
      lastSwitchedR = timeSwitchedR;
    }

    int getDownAngleR() {
      return downAngleR;
    }
    void setDownAngleR(int angleR) {
      downAngleR = angleR;
    }

    int getUpAngleR() {
      return upAngleR;
    }
    void setUpAngleR(int angle) {
      upAngleR = angle;
    }
    //////////////////////////////////////////////////////////
    void initR()
    {
      helmetServoR.attach(helmetPinR);
      helmetServoR.write(upAngleR);
      lastSwitchedR = millis();
      flip = false;
      flipping = false;
      lastFlippedR = 0;
      lastMovedR = 0;
    }
    //////////////////////////////////////////////////////////
    void flipFaceplateR()
    {
      int directionalityR;
      long lastFlippedR;
      boolean flip, flipping;
      int currentPosR;
      long lastMovedR;
      {
        if ((millis() - lastFlippedR) > 500 )
        {
          lastFlippedR = millis();
          if (flip)
          {
            flipping = true;
            currentPosR = 0;
            directionalityR = UPR;
          }
          else
          {
            flipping = true;
            currentPosR = 160;
            directionalityR = DOWNR;
          }
          flip = !flip;
        }
      }
      /////////////////////////////////////////////////////////
      void UpdateR()
      {
      if ( flipping & (millis() - lastMovedR) >= 15 )

      {
        lastMovedR = millis();
        if ( directionalityR == UP )
        {
          currentPosR = currentPosR + 3;
        }
        else
        {
          currentPosR = currentPosR - 3;
        }
        Serial.print("current position: "); Serial.print(currentPosR);
        Servo helmetServoR;
        helmetServoR.write(currentPosR);
      }
      int downAngleR;
      int upAngleR;
      if ((currentPosR >= upAngleR) || (currentPosR <= downAngleR)) {
        flipping = false;
      }
    }
};
///////////////////////////////////////////////////////////////
HELMET helmet(helmetPin, true, DOWNANGLE, UPANGLE);
HELMETR helmetR(helmetPinR, true, DOWNANGLER, UPANGLER);
/////////////////////////////////////////////////////////
Adafruit_MPR121 touchControl = Adafruit_MPR121();
uint16_t currTouched = 0;
///////////////////////////////////////////////////////////////
void setup(void)
{ Serial.begin(9600);
  helmet.init();
  if (!touchControl.begin(0x5A)) {
    Serial.println("MPR121 not found, check wiring?");
    while (1);
  }
  Serial.println("MPR121 found!");
}

void loop(void)
{
  currTouched = touchControl.touched();
  Serial.print("touched: "); Serial.println(currTouched);

  switch (currTouched)
  {
    case 1:
      helmet.flipFaceplate();
      helmetR.flipFaceplateR();
      break;
    case 2:
      break;
  }
  helmet.Update();
  helmetR.UpdateR();
};

TheMemberFormerlyKnownAsAWOL

#12
Aug 25, 2019, 12:27 am Last Edit: Aug 25, 2019, 12:28 am by TheMemberFormerlyKnownAsAWOL
No, not a new topic, a new post in your other thread.

Did you read the "how to use the forum" topic?

Why not?

You have a macro helmetPinR and a variable with the same name.

H3M4N

Really screwing this up, thanks for your patience.


Which lines would need to be replaced with a new name? I'm not sure in some lines where Arduino is calling for the variable or Macro...

TheMemberFormerlyKnownAsAWOL

Well, the preprocessor runs first, so the macro has precedence.

Go Up