 # Quick question regarding variables

I am trying to use a single word * the name of the gear * to adjust the stepper motor to certain positions, labeled with the gear for visual aid.

In naming of my variables, how would I correctly "declare" the variable so that i may use the word "reverse" "firstGear" etc.

``````reverse = myservo.write(0);
firstGear = myservo.write(60);
secondGear = myservo.write(90);
thirdGear = myservo.write(120);
fourthGear = myservo.write(150);
fifthGear = myservo.write(180);
``````

In its most basic form, I think this is what you're trying to ask for

``````int reverse = 0;
int firstGear = 60;
int secondGear = 90;
int thirdGear = 120;
int fourthGear = 150;
int fifthGear = 180;

myservo.write(reverse);
myservo.write(firstGear);
..etc.
``````

Or you could declare a function for each gear name:

``````void reverse() {myservo.write(0);}
void firstGear() {myservo.write(60);}
void secondGear() {myservo.write(90);}
void thirdGear() {myservo.write(120);}
void fourthGear() {myservo.write(150);}
void fifthGear() {myservo.write(180);}
``````

Thanks guys. I am having trouble logically working through this problem.
They say the best thing to do is step away for a little and return. I will post this before I do just that.

I am trying to have 3 or 4 conditions be true, for the “gear” to shift.

I am trying to read a button input - pressing of the button - during a given time frame, with the potMeasure value map’d (1,1023,1,3) equal to 1 for a “good shift”.

ie. i need the button to be pressed within 1 second and the pot value to be map’d equal to 1. *This is after a counter variable reaching 768, meaning all 3 RED LEDS have reached their full PWM.

shiftButtonGood is a boolean expression

``````shiftButtonGood = int shiftButtonFunction()
{
digitalWrite(greenP, HIGH);
currentMillis = millis();
goodShiftUpper = currentMillis+upperShiftTime;
do
{
}while(goodShiftUpper<=currentMillis);
``````
``````shiftButtonGood = int shiftButtonFunction()
``````

That line is pretty much nonsense. What's the "int" there for? Where's that function defined?

They say the best thing to do is step away for a little and return.

I think a MUCH better plan would be to put this all aside for a day or two and work your way through a good C++ tutorial. Once you know the basics of the language so you aren't just guessing at the syntax then the whole world will open up for you.

I was defining the function. I had the shiftButtonGood infront of it to give me a boolean expression based on the parameters being met. in the loop {} i call that function

shiftButtonGood = shiftButtonFunction();

``````// psuedo code
#include <Servo.h>

// time control logic
unsigned long previousMillis = 0;
unsigned long currentMillis = 0;

//potentiometer = throttle
const int potP = A0;
int potMeasure = 0;
const int measurePotTime = 200;
boolean shiftbuttonGood = false;

// lights and shifting
const int blueP = 7;
const int yellowP = 12;
const int shiftButtonP = 2;
int shiftbuttonState = 0;
int prevshiftbuttonState = 0;
int pwmIncrease = 0; // global
int counter = 0; // global count of 0 - 767 to determine which light to add illumination too
int gearIndicator = 1; // to let the program know where to hold the stepper motor and the time avaiable for good shifts

//pushbutton to start the program
const int pushButton = 13;
int prevbuttonState = 0;
int buttonState = 0;

//3 red lights / lcd screen = engine rmps
const int redP1 = 11;
const int redP2 = 10;
const int redP3 = 9;
int redM1 = 0;
int redM2 = 0;
int redM3 = 0;
const int

// 4th light . green / lcd screen = shift indicator
const int greenP = 5;
int greenM = 0;

// piezo
const int pizP = 4;
int goodSound = 50;

void setup()
{
pinMode(pushButton, INPUT);
pinMode(shiftButtonP, INPUT);
pinMode(redP1, OUTPUT);
pinMode(redP2, OUTPUT);
pinMode(redP3, OUTPUT);
pinMode(greenP, OUTPUT);
pinMode(blueP, OUTPUT);
pinMode(yellowP, OUTPUT);
tone(pizP, goodSound, 50);

// servo motor
Servo myservo;
int servoPOS = 0;
myservo.attach(8);
// R - 1 - 2 - 3 - 4 - 5 = gears
reverse = myservo.write(0);
firstGear = myservo.write(60);
secondGear = myservo.write(90);
thirdGear = myservo.write(120);
fourthGear = myservo.write(150);
fifthGear = myservo.write(180);

}

void loop()
{
startProgram();
goodShiftUpper = goodShift(gearIndicator);
potMeasure = measurePot();
shiftButtonGood = shiftButtonFunction();
}

//start of program . flashing lights and welcome on monitor
void startProgram()
{
digitalWrite(redP1, 255);
digitalWrite(redP2, 0);
digitalWrite(redP3, 255);
delay(200);
digitalWrite(redP1, 0);
digitalWrite(redP2, 255);
digitalWrite(redP3, 0);
delay(200);
digitalWrite(redP1, 255);
digitalWrite(redP2, 0);
digitalWrite(redP3, 255);
delay(200);
digitalWrite(redP1, 0);
digitalWrite(redP2, 255);
digitalWrite(redP3, 0);
delay(200);
digitalWrite(redP1, 255);
digitalWrite(redP2, 0);
digitalWrite(redP3, 255);
delay(200);
digitalWrite(redP1, 0);
digitalWrite(redP2, 255);
digitalWrite(redP3, 0);
delay(200);
digitalWrite(redP1, 255);
digitalWrite(redP2, 0);
digitalWrite(redP3, 255);
delay(200);
digitalWrite(redP1, 0);
digitalWrite(redP2, 255);
digitalWrite(redP3, 0);
delay(200);

//all red lights off
digitalWrite(redP1, 0);
digitalWrite(redP2, 0);
digitalWrite(redP3, 0);

// green light for start + push button reading
Serial.println("Welcome to the shift simulator.");
Serial.println("Instructions:");
Serial.println("There are 3 RED lights, measuring the gas on throttle".);
Serial.println("The green light indicates shift point.");
Serial.println("The blue light indicates a good shift.  The yellow, a bad shift.");
Serial.println("The round instrument creates audio depending on good shifts or bad shifts.");
Serial.println("The button closest to round insturment is the button to shift. Press this when green LED lit.");
Serial.println("Turning the POT adjusts the amount of throttle.  Use this to lite the RED LEDs slower or faster.");
Serial.println("Remember, there must be little throttle while green LED lit and pressing the shift button.");
Serial.println("Holding the POT at lowest point for 1.25 seconds will shift into reverse.");

Serial.println("Pressing the button closeest to the lights will start the game."

digitalWrite(greenP, 255);
}

//mesaure the POT and map the value, then return
int measurePot()
{
currentMillis = millis();
reverseShiftTime = currentMillis + 1250;
if (currentMillis - previousMillis >= measurePotTime)
{
previoiusMillis = currentMillis;
if (potMeasure == 0) // need help measuring time of hold potentiometer @
// 0 resistance for 1.25 seconds to shift into reverse
{
int reverseMeasureMillis = currentMillis;
if (reverseMeasureMillis >= reverseShiftTime)
{
// step motor to reverse
// gear shift indicator to reverse
// motor to spin in reverse
}
}
potMeasure =  map(potMeasure, 1, 1023, 1, 3);
}
}
return potMeasure;
}

// take the mapped value of potMeasure and determine how much to PWM increase the 3 RED LEDs
int adjustLightAmount(potMeasure) // function returning pwmIncrease, which is used with counter to illiminate the lights
if (currentMillis - previousMillis >= adjustLightsTime)
{
previousMillis = currentMillis;
switch (potMeasure)
{
case 1:
pwmIncrease = 1;
break;
case 2:
pwmIncrease = 3;
break;
case 3:
pwmIncrease = 7;
}
return pwmIncrease;
}

// a do loop to adjust the lights from L1 = 0 - 255 L2 =  256 - 511 L3 = 512 - 768 on the counter variable
{
do  // a do loop to start a new gear with pwmIncrease increasing the given light
{

if (counter >= 0 && counter <= 255)
{
digitalWrite(redP1, redM1 + pwmIncrease);
digitalWrite(redP2, 0);
digitalWrite(redP3, 0);
counter += pwmIncrease;
potMeasure = measurePot();
}
if (counter >= 256 && counter <= 511)
{
digitalWrite(redP1, 255);
digitalWrite(redP2, redM2 + pwmIncrease);
digitalWrite(redP3, 0);
counter += pwmIncrease;
potMeasure = measurePot();
}
if (counter >= 512 && counter <= 767)
{
digitalWrite(redP1, 255);
digitalWrite(redP2, 255);
digitalWrite(redP3, redM3 + pwmIncrease);
counter += pwmIncrease;
potMeasure = measurePot();
}
} while (counter < 768); // not sure which while to operate the loop while(currentMillis>=goodShiftLower&&currentMillis<=goodShiftUpper);
// need to make a switch case for current gear to know what goodshift limits to insert
}

int goodShift(gearIndicator) // function returning the goodShiftUpper time to be added for the "time to shift" by pressing button
{
switch (gearIndicator)
{
case 1:
goodShiftUpper = 1500;
break;
case 2:
goodShiftUpper = 1300;
break;
case 3:
goodShiftUpper = 1000;
break;
case 4:
goodShiftUpper = 750;
break;
case 5:
goodShiftUpper = 750;
break;
}
return goodShiftUpper;
}

int shiftButtonFunction()
{
digitalWrite(greenP, HIGH);
currentMillis = millis();
goodShiftUpper = currentMillis + upperShiftTime;
do
{
potMeasure =  map(potMeasure, 1, 1023, 1, 3);
if (potMeasure == 1)
{
}
} while (goodShiftUpper <= currentMillis);
}
``````

Thanks for the encouragement. Like i said I am just starting and this is my first attempt at trying to place everything into 1.

Well that's not how you do it. Again, would strongly recommend a C++ tutorial instead of just guessing at this. It's not hard, but you do need to go through and learn how it is done. It won't take more than a day or two to learn the basics.

If you're defining a function then you don't put anything = in front of it. And if it is going to return int then it needs a return statement. Only a void function doesn't need a return.

Also all of this timing stuff that you are doing with addition is broken / buggy.

You never want to calculate a time in the future. You should always deal with time in the past using subtraction. Instead of trying to calculate what the time will be at some future point and waiting for millis to get there, which is buggy, you should instead remember the time something started and subtract from millis to see how long it has been and use that number. That way is safe.