elementary math

I would like to use Arduino to permit our students an opportunity to see math in action by asking them to edit code that will power a stepper on a hacked scanner. If successful, we plan to build a robot that will navigate a course based upon their equations. These are elementary students so the numbers must be no greater than three digits or much less for the little ones. The code is a failed attempt to achieve this goal. As you may guess, I am not a programmer but wonder if there is some way to assign a value to a variable that is within “range” and then students can use an appropriate operator to adjust from there. Hope this makes sense?

#include <Stepper.h>

#define motorStep 400
#define motorPin1 8
#define motorPin2 9
#define motorPin3 10
#define motorPin4 11
#define turn 12

int count;

Stepper myStepper(motorStep, motorPin1,motorPin2,motorPin3,motorPin4);

void setup() {
myStepper.setSpeed(40);
Serial.begin(9600);
count = 1;
digitalWrite (turn, HIGH);
}

void loop () {
if (count) {
count–;
Serial.println (“Forward”);
myStepper.step(1500+1400);
delay(500);

Serial.println (“Backward”);
myStepper.step(-1500-1400);
delay(500);
}
}

is there some way to assign a value to a variable that is within “range” and then students can use an appropriate operator to adjust from there. Hope this makes sense?

Hi frank, its not clear to me what you want to do, can you clarify?

Sorry, I should have posted a clear example.

My scanner stepper runs about 1800 steps to push/pull the laser bar from one end to the other. For simplicity, let's say that is the goal; moving the bar across the box from one location to another. For the upper grades, I could assign a value of 1000. They would have to add/subtract a number from 1000 to guide the bar to the goal. The line of code - myStepper.step (-1500 -1400) is an awkward attempt to create an equation. For a robot to cover a distance of just a few feet, we would be using numbers well beyond their ability.

I am questioning whether a variable could be assigned to myStepper, such as x = 10 steps which would limit the scanner trial number to 180 rather than 1800

I have a digital screen so students will see the code they are editing. We have done this successfully with Processing sketches.

Use functions to simplify the programming. Abstraction is a key element in programming.

void move(Stepper a, int n)
{
      //do all the necessary math here to process n to its desired value. The example line below simply multiplies it by 10.
      n = n * 10;
      
      //now just call the step command with processed value n
      a.step(n); 
}

//Example call:
move(myStepper, 6)

The individual programming does not need to understand what the move command does, simply how to use it.

Perhaps the map function will help: http://www.arduino.cc/en/Reference/Map

Are you trying to implement a sort of mechanical "number line"? Stick down an appropriately numbered "scale" (might go from 1 to 20, might go from -50 to +50, depending on age of students), and then have them figure out something like "get to position 7" if your only options are +3 and -1" ?, and then have them watch "real mechanics" to see whether it works? If so, I agree that the "map" function will probably be useful. If you know the "real" range for "number of steps" from zero to the end of the scale, then the number of steps for your "logical number line" would be something like:

nsteps = map(desiredLogicalLocation, logicalStart,
                logicalEnd, realStart, realEnd);

"map" solves a standard straight line equation for you. Given two points (x1, y1) and (x2, y2) you can derive the standard straight-line equation Y = m*X + b, and then plug in a new value of X. That would correspond to Y = map(X, x1, x2, y1, y2); if I understand it correctly...

Neat idea, I think. It'll all go to hell if the stepper misses steps or something, and doesn't actually move to where it's supposed to go :-(

Neat idea, I think. It'll all go to hell if the stepper misses steps or something, and doesn't actually move to where it's supposed to go

Why will all hell break loose? How is the map function responsible for the stepper missing a step? The step function must still be called, and will be called with the exact same value as if you weren't pre-processing an int, hence the likelihood of missing a step is the exact same as if the number wasn't pre processed. Therefore implementing it in such a fashion shouldn't affect the performance of the system.

Update: I think I mis-interpreted. You mean that the program will miss a "large" step as compared to the multitude of individual steps actually invoked upon the motor. If so then, it still won't affect the performance of the system. The "large" step simply exists within the software, but the motor is indifferent to this as it still invoked step by step.

Actually, I mean that if the low-level stepper code were to output the steps too fast, so that the motor PHYSICALLY missed steps. Steppers normally don't have any feedback that they've moved where you told them to move. If you're twiddling them around or moving robot wheels, that's not so bad. If you're dealing with 3rd graders who are going to go "why is 3 + 3 equal only partway between 5 and 6?", that's worse.

Did I have the number-line idea right?

Digesting your advise will take a while but it seems that this is the right path towards the solution. I am sure to have questions and thank all of you who have contributed. Our students will enjoy this. Standardized testing madness in Florida perpetuates a strict 2D approach to education with so few opportunities to move beyond paper. Hopefully Arduino will help bridge the gap between abstract concepts and visible results. Any thoughts you may have concerning this question would be appreciated, though I am not sure this is the proper forum.