Hey guys, im pretty new to arduino. Working on a program to keep an object at the same height regardless of how much weight is on it - using an air spring. I have a rotary height sensor (headlight leveling sensor) to measure the height and im using two selonoid valves hooked up to a relay module. There is also a switch (I/O household lightswitch) wired in to tell the system when to recalibrate. I've had the program working somewhat okay before, but it wasn't 100% right and it was glitchy, so i wanted to include a few libraries(?) to simplify the code. I have reached a point where in a function within Equalize.cpp i need to call a function from UpDown.cpp. I don't have much experience other than 2-3 days with google. I've spent hours searching, but either the examples are bad or i don't even know what i need to be googling.
// ---------------- UpDown.h --------------------
#ifndef UpDown_H
#define UpDown_H
//#define Equalize_h
#include <Arduino.h>
#include "Equalize.h"
class UpDown{
public:
UpDown(const int, const int, const int, const int);
~UpDown();
//void Equalize(const int, const int);
void increaseRR();
void decreaseRR();
void equalRR();
void increaseLR();
void decreaseLR();
void equalLR();
private:
int RRIPin;
int RROPin;
};
#endif
// -------------- UpDown.cpp -----------------
/* HIGH = no power @ relay !!! */
#include "UpDown.h"
#include "Equalize.h"
int _RRIPin;
int _RROPin;
int _LRIPin;
int _LROPin;
//int _RRHeightPin;
// int _LRHeightPin;
UpDown::UpDown(const int RRIPin, const int RROPin, const int LRIPin, const int LROPin){
_RRIPin = RRIPin;
_RROPin = RROPin;
_LRIPin = LRIPin;
_LROPin = LROPin;
pinMode(RRIPin, OUTPUT); //Right Rear (air) In Pin
pinMode(RROPin, OUTPUT); //Right Rear (air) Out Pin
pinMode(LRIPin, OUTPUT); //Left Rear (air) In Pin
pinMode(LROPin, OUTPUT); //Left Rear (air) Out Pin
digitalWrite(RRIPin, HIGH); //No Power @ AirIN relay
digitalWrite(RROPin, HIGH); //No Power @ AirOUT relay
digitalWrite(LRIPin, HIGH); //No Power @ AirIN relay
digitalWrite(LROPin, HIGH); //No Power @ AirOUT relay
}
UpDown::~UpDown(){/* nothing to destruct*/}
void UpDown::increaseRR(){ //RIGHT
digitalWrite(_RRIPin, LOW); //Power @ AirIN relay
digitalWrite(_RROPin, HIGH); //No Power @ AirOUT relay
Serial.println("increaseRR");
Serial.println(_RROPin);
delay(500);
}
void UpDown::increaseLR(){ //LEFT
digitalWrite(_LRIPin, LOW); //Power @ AirIN relay
digitalWrite(_LROPin, HIGH); //No Power @ AirOUT relay
Serial.println(_LRIPin);
delay(500);
}
void UpDown::decreaseRR(){ //RIGHT
digitalWrite(_RROPin, LOW); //Power @ AirOUT relay
digitalWrite(_RRIPin, HIGH); //No Power @ AirIN relay
Serial.println("decreaseRR");
delay(500);
}
void UpDown::decreaseLR(){ //LEFT
digitalWrite(_LROPin, LOW); //Power @ AirOUT relay
digitalWrite(_LRIPin, HIGH); //No Power @ AirIN relay
Serial.println(_LROPin);
delay(500);
}
void UpDown::equalRR(){ //RIGHT
digitalWrite(_RRIPin, HIGH); //No Power @ AirIN relay
digitalWrite(_RROPin, HIGH); //No Power @ AirOUT relay
Serial.println("equalRR");
}
void UpDown::equalLR(){ //LEFT
digitalWrite(_LRIPin, HIGH); //No Power @ AirIN relay
digitalWrite(_LROPin, HIGH); //No Power @ AirOUT relay
}
// -------------- Equalize.h -----------------
#ifndef Equalize_H
#define Equalize_H
//#define UpDown_h
#include <Arduino.h>
#include "UpDown.h"
void increaseRR();
void decreaseRR();
void equalRR();
void increaseLR();
void decreaseLR();
void equalLR();
class Equalize{
public:
//int _RRHeightPin;
//int _LRHeightPin;
Equalize(const int, const int);
~Equalize();
void makeMoves(int);
private:
int rr_height;
int num;
};
#endif
// -------------- Equalize.cpp -----------------
#include "Equalize.h"
#include "UpDown.h"
int _RRHeightPin;
int _LRHeightPin;
int _setHeight;
Equalize::Equalize(const int RRHeightPin, const int LRHeightPin){
_RRHeightPin = RRHeightPin;
_LRHeightPin = LRHeightPin;
pinMode(_RRHeightPin, INPUT);
pinMode(_LRHeightPin, INPUT);
}
Equalize::~Equalize(){}
void Equalize::makeMoves(int setHeight){
_setHeight = setHeight;
rr_height = analogRead(_RRHeightPin);
num = rr_height / 10;
//Serial.println("not =");
if (num == _setHeight){ // if sensor = set height
Serial.println("equal");
//Serial.println(num);
//Serial.println(_setHeight);
UpDown equalRR(); // *************************
}
else{
while (num != _setHeight){
if (num < _setHeight){
//lcd.clear();
Serial.println("High");
//Serial.println(num);
//Serial.println(_setHeight);
UpDown increaseRR(); // *************************
}
else if (num > _setHeight){ //if sensor > set height
Serial.println("Low");
//Serial.println(num);
//Serial.println(_setHeight);
UpDown decreaseRR(); // *************************
}
_setHeight = setHeight;
rr_height = analogRead(_RRHeightPin);
num = rr_height / 10;
}
}
}
// --------------------- main .ino file -----------------
#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2); //Declare LCD Pins
#include "Equalize.h"
Equalize eq(A0,A1);
#include "UpDown.h"
UpDown upDown(22,23,24,25) ; // upDown(Right Rear 'airIN' relay, Right Rear 'airOUT' relay)
#include "DoSwitch.h"
DoSwitch action;
int rideHeight = 65;
boolean party = false;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
lcd.begin(20,4);
pinMode(41, OUTPUT);
}
void loop() {
// put your main code here, to run repeatedly:
party = action.switchIt();
//Serial.println(party);
if(party == false){ //equalize here
digitalWrite(41, LOW);
//Serial.println("Blink");
eq.makeMoves(rideHeight);
upDown.increaseRR();
upDown.decreaseRR();
delay(500);
//upDown.equalRR();
}
else{ //defer to here once setup is proper
digitalWrite(41, HIGH);
upDown.equalRR();
}
//delay(500);
}
I am trying to use functions equalRR(); increaseRR(); and decreaseRR() from UpDown.cpp in my Equalize.cpp (library), but i can't get them to work. i've included both .cpp's both .h's, and my main file.
I've attached a (rough) Fritzing layout of my system.
Curious if there is an easy fix to this, or if i'm wasting my time going about it this way.
thanks for your time!
Mike