Help with project using Nested User Defined Functions and calling Arrays

This code is for a class but the problem statement isn’t that important but I’ve included it anyway in case clarification is needed. The reason I’m doing this using user defined functions is because there is a requirement for using at least two.

The ultimate purpose of this code is to move an hour hand on a 24hr clock to a set of numbers encoded in an array. The code is made up of 2 functions. One motormove is given an integer and runs the hand to that location. The other motorcoord is given an array and runs the values of that array through motormove sequentially to have the hand move to each number. I’m having trouble getting this setup to work. I have feeling its calling the array that I’m doing wrong.

This code doesn’t do anything starting at function motorcoord(coord) in void loop. I think there is a problem with my syntax in calling the array or maybe I’m just missing something larger about nesting functions in general. The code verifies and everything. So if someone more knowledgeable than me feels like taking a look i’d greatly appreciate it :blush: :blush:

The problem statement is, "Suppose you are provided with a two-motor circuit that is connected to the Arduino’s Digital Pins 5 and 6. The motors are connected to a 24-hour clock hand. When the pin 5 motor
receives power, it rotates the hand 2 “hours” per second clockwise. When the pin 6 motor
receives power, it rotates the hand 2 “hours” per second counterclockwise. When neither
motor is receiving power, or both are, the hand is stationary.
Suppose the hand starts at 0 and you have to program the Arduino to move the hand in this
sequence: 18-10-23-8-0. Assuming you need to stop at each number for half a second,
write a complete Arduino sketch to input the sequence including setup(). You must use
at least two custom functions. "

int pos=0 ;

int coord[] = {18,10,23,8,0}; //specific Hours on a 24 hour Clock


void setup() {
Serial.begin(9600);
pinMode(5, OUTPUT); //Clockwise spin pin
pinMode(6, OUTPUT); //Counter Clockwise spin pin
}

void loop() {
Serial.println("BEGAN");

motorcoord(coord); // This function should take care of everything

while(1) { }        //To only run the code once

}

//BELOW are user defined functions that make up the entire functionality of the code

void motormove(int n){      //This function should take in a single integer that equates to an hour location 
                            //on the 24hr clock face and will run the hand to that number, this integer will 
                            //ultimately come 
                            //from the coord array. Once there it should delay for a half second and end.
int unit=500;

if (pos <n) {
for (int j=pos; j < n; j++){
digitalWrite(5, HIGH);
delay(unit);
digitalWrite(5, LOW);
    }
}
else if (pos >n){
for (int j=pos; j > n; j--){
digitalWrite(6, HIGH);
delay(unit);
digitalWrite(6, LOW);
      }
}
else{
pos=n;
Serial.println(pos);
delay(unit/2);
    }
}

void motorcoord(int N[5]){      //This function should take in an array of values that equate to hour 
                              //locations 
                              //on the 24hr clock face and run them each separately through the motormove 
                              //function so 
                              //that 
                              //the hand moves to each hour value that is in the array. 


N=coord;

for (int i=0; i <= 4; i++){

motormove(N[i]);

}
}

15wileyr:
The issue with my code is that, haha, it doesn't work.

Haha, what does haha it actually hahahaha do and haha how does hohehehahaha that differ from hehehehahaha what you want?

Very constructive, That really helped!

15wileyr:
Very constructive, That really helped!

It was a serious question. How can we help you if you don't describe the problem? Or was it the hahaha's that you didn't like? Do you think they were more helpful when you wrote them? I was just trying to play along.

yeah sorry I got a little defensive, it was the hahaha's :slight_smile: but you have a point of course. I've edited the post and added comments to the code.

I think that perhaps my for loops in function motormove may actually be the problem because if make motorcoord just display the array values it does just fine.

Yeah Its definitely motormove, that function just doesn't work on its own but i don't know why.

Okay so I’ve mostly fixed motormove but the if statement that is suppose to deincrement down isn’t working.

void motormove(int n){      //This function should take in a single integer that equates to an hour location 
                            //on the 24hr clock face and run the hand to that number, this will ultimately come 
Serial.println(pos);
Serial.println(n);
Serial.println("STARTING OVER");//from the coord array. Once there it should delay for a half second.
int unit=500;

if (pos < n) {
for (int pos; pos < n; pos++){
digitalWrite(5, HIGH);
delay(unit);
digitalWrite(5, LOW);
Serial.println("pos is smaller");
Serial.println(pos);
    }
}
else if (pos > n){
for (int pos; pos > n; pos--){
digitalWrite(6, HIGH);
delay(unit);
digitalWrite(6, LOW);
Serial.println("pos is BIGGER");
Serial.println(pos);
      }
}

pos=n;
Serial.println(pos);
Serial.println("pos ISSSS now = n, good");
delay(unit/2);
 
}

so my value for pos always goes back to pos=0 after motormove runs. like it doesn't change the global variable. how do I make the Function actually change it

Okay I totally figured it out, Yes I know how stupid this thread is ill post the working code in case anyone needs this lol

int pos=0 ;

int coord[] = {18,10,23,8,0}; //specific Hours on a 24 hour Clock


void setup() {
Serial.begin(9600);
pinMode(5, OUTPUT); //Clockwise spin pin
pinMode(6, OUTPUT); //Counter Clockwise spin pin
}

void loop() {
Serial.println("BEGAN");

motorcoord(coord); // This function should take care of everything

while(1) { }

}

//BELOW are user defined functions that make the entirety of the code

void motormove(int n){      //This function should take in a single integer that equates to an hour location 
                            //on the 24hr clock face and run the hand to that number, this will ultimately come 
                            //from the coord array. Once there it should delay for a half second.
int unit=500;

if (pos < n) {
for (pos; pos < n; pos++){
digitalWrite(5, HIGH);
delay(unit);
digitalWrite(5, LOW);
Serial.println("pos is smaller");
Serial.println(pos);
    }
}
else if (pos > n){
for (pos; pos > n; pos--){
digitalWrite(6, HIGH);
delay(unit);
digitalWrite(6, LOW);
Serial.println("pos is BIGGER");
Serial.println(pos);
      }
}

pos=n;

Serial.println("YOUR AT N WOOOOOOH ************** *******  ** * *   * *");
Serial.println(pos);
delay(unit/2);
}

void motorcoord(int N[5]){      //This function should take in an array of values that equate to hour locations 
                              //on the 24hr clock face and run them each through the motormove function so that 
                              //the hand move to each hour value that is in the array. 


N=coord;

for (int i=0; i <= 4; i++){

motormove(N[i]);

}
}
void motorcoord(int N[5])

The array does not have an element 5, but that is not what you meant anyway, and you can't pass an array to a function like that.

There is actually no need to pass the array to the function as it is declared globally

for (int pos; pos < n; pos++){

In the first iteration of this for loop, what will the value of pos be? You can NOT be assured that it will be 0. How many times will this loop iterate? The name of the variable, n, does not give a clue.