Go Down

Topic: understanding arrays  (Read 317 times) previous topic - next topic

Thomaswates

hello, I have a small issue with my code

Code: [Select]

   do {
    sensorReading = analogRead(A0);
    int DistanceX = (SELETIONX - sensorReading)
    int MotorDirection = ( DistanceX / abs(DistanceX));
    int STEPSX = (SPU * MotorDirection );
    myStepper.setSpeed(250);
    myStepper.step(STEPSX);    // takes 160 steps == one mm, ether clockwise or anticlockwise:
    delay(500);
  } while ( sensorReading != GEARSELETIONX );
  }


this is my code, I want It to Read AnalogPin(A0) then depending on the ( SELETIONX ) move forward or backwards 1mm looping until the ( SELETIONX == AnalogPin(A0) ), then I want the TARGET to change to the next target.

I believe to do this I need to create a array...... but I am having trouble getting SELETIONX to change, and most of the time I get the error code - SELETIONX was not Declared in this scope- although this was when I was using the if else function.

thanks for your help.

Thomaswates

-- UPDATE --

full code --

Code: [Select]
#include <Stepper.h>

// initialize the stepper library on pins 8 through 11:
int in1Pin = 8;
int in2Pin = 9;
int in3Pin = 10;
int in4Pin = 11;
const int SPR = 200;
Stepper myStepper(SPR, in1Pin, in2Pin, in3Pin, in4Pin);

int Pitch = 3; // distance between teeth //
int MS = 4; // mirco steps per step //
float SPU = ((SPR*MS) / Pitch); // steps Per Unit mm //// SPU = 160steps per mm  //

// counters//
int countX = 0;
int countY = 0;

// sensors //
int sensorReading = analogRead(A0);

// CROSSGATE POSITION //
void setup()
{
  Serial.println(MS & Pitch & SPR);
}
int myArrayX[] = {50, 100, 200, 300, 400, 500, 600, 700}; // just example values.

int myArrayY[] = {818, 532, 205};


void loop()
{
XAXIS:
  Serial.println( countX );
  if (countX = 9)
  {
    int countX = 0;
  }
  do {
    sensorReading = analogRead(A0); // reads the sensor and out puts a analog value [ 0 - 1023 ]:
    int DistanceX = (myArrayX[countX] - sensorReading); // analog distance between currunt position and end postion
    int MotorDirection = ( DistanceX / abs(DistanceX));
    int STEPSX = (SPU * MotorDirection ); // this is one mm:
    myStepper.setSpeed(250);   // if analog to step convertion doesnt work i could step one step at a time:
    myStepper.step(STEPSX);    // takes 160 steps == one mm, ethier clockwise or anticlockwise:
    delay(500);
  } while ( sensorReading != myArrayX[countX] ); // might need to use a range here so it isnt going back and forth:
  if ( sensorReading == myArrayX[countX] )
  {
    countX = countX + 1;
    Serial.println( countX );
  }
  else goto XAXIS;

  //-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------//

  // ADD code for Xdirection, for all gears below //
YAXIS:
  Serial.println( countY );
  if ( countY = 9)
  {
    int CountY = 0;
  }
  do {
    sensorReading = analogRead(A1); // reads the sensor and out puts a analog value [ 0 - 1023 ]:
    int DistanceY = ( myArrayY[countY] - sensorReading );
    int motorSpeed = 200; // sets motor speed to 200
    int STEPSY = (DistanceY * 31.28055); //31.28055 converts analogReading to number of steps -- 200mm of movement i think this line needs to change but i can work why -- analog to number of steps is really hard to understand..//
    myStepper.setSpeed(motorSpeed);
    myStepper.step(STEPSY);
    delay(STEPSY * 2);
  } while ( sensorReading != myArrayX[countX]);
  if ( sensorReading == myArrayX[countX] )
  {
    countY = countY + 1;
    Serial.println( countY );
  }
  else goto YAXIS;

  //-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------//
CrossGate:
  do {
    sensorReading = analogRead(A1); // reads the sensor and out puts a analog value [ 0 - 1023 ]:
    int DistanceY = ( myArrayY[2] - sensorReading );
    int motorSpeed = 200; // sets motor speed to 200
    int STEPSC = (DistanceY * 31.28055); //31.28055 converts analogReading to number of steps -- 200mm of movement i think this line needs to change but i can work why -- analog to number of steps is really hard to understand..//
    myStepper.setSpeed(motorSpeed);
    myStepper.step(STEPSC);
    delay(STEPSC * 2);
  } while ( sensorReading != myArrayY[2]);
  if ( sensorReading == myArrayY[2] )
  {
    exit;
  }
  else goto CrossGate ;
}


sterretje

#2
Dec 07, 2017, 11:29 am Last Edit: Dec 07, 2017, 11:33 am by sterretje
Nevermind, did not undestand question properly.
If you understand an example, use it.
If you don't understand an example, don't use it.

Electronics engineer by trade, software engineer by profession. Trying to get back into electronics after 15 years absence.

septillion

First ting I notice, I see goto... There is just barley justification for using them in C++... And it makes code just hard to debug and follow. In C/C++ there are function calls, just use them.

And about the problem, I don't see the relation with arrays...
Use fricking code tags!!!!
I want x => I would like x, I need help => I would like help, Need fast => Go and pay someone to do the job...

NEW Library to make fading leds a piece of cake
https://github.com/septillion-git/FadeLed

Thomaswates

firstly -- I've spoken to a couple of people about the use of goto, and for a program of this size using goto isn't an issue, how would I change my goto commands to function calls ?? 

secondly-- i was using series of if else function to change the value of the x and y targets depending on what the value of the count. however this didn't work as i was getting an error saying that the target was not defined in the scope.

this is the code i was getting an error with.

Code: [Select]
// GEAR POSITION X AXIS//
int REX = 50;
int firstX = 100;
int secondX = 200;
int thirdX  = 300;
int fourthX  = 400;
int fithX = 500;
int sixthX = 600;
int seventhX = 700;




void setup() {
  Serial.println(MS & Pitch & SPR);
}


void GEARSELETIONX ()
{
  if (countX = 0) {
    int SELETIONX = REX;
  }
  else if (countX = 1) {
    int SELETIONX = firstX;
  }
  else if (countX = 2) {
    int SELETIONX = secondX;
  }
  else if ( countX = 3) {
    int SELETIONX = thirdX;
  }
  else if ( countX = 4) {
    int SELETIONX = fourthX;
  }
  else if ( countX = 5) {
    int SELETIONX = fithX;
  }
  else if ( countX = 6 ) {
    int SELETIONX = sixthX;
  }
  else if ( countX = 7) {
    int SELETIONX = seventhX;
  }
}



void loop()
{
XAXIS:
  Serial.println( countX );
  if (countX = 9)
  {
    int countX = 0;
  }
  do {
    sensorReading = analogRead(A0); // reads the sensor and out puts a analog value [ 0 - 1023 ]:
    int DistanceX = (SELETIONX - sensorReading); // analog distance between currunt position and end postion
    int MotorDirection = ( DistanceX / abs(DistanceX));
    int STEPSX = (SPU * MotorDirection ); // this is one mm:
    myStepper.setSpeed(250);
    myStepper.step(STEPSX);   
    delay(500);
  } while ( sensorReading != GEARSELETIONX );
  if ( sensorReading == GEARSELETIONX )
  {
    countX = countX + 1;
    Serial.println( countX );
  }
  else goto XAXIS;



one of my friends suggested changing the if else functions to an array... after adding the array i don't get any errors...

sorry if this is hard to follow i am not good at expressing my self in words.

Thomas


Budvar10

#5
Dec 07, 2017, 12:38 pm Last Edit: Dec 07, 2017, 12:55 pm by Budvar10
If we are talking about the last code you've sent, the SELETIONX should be defined just once as global variable.
It is multiply defined in the GEARSELETIONX function. It is not reachable from the loop().
Another thing is that the SELETIONX should have some required value before its first use (as global will have 0) and the GEARSELETIONX is a function so its use is "GEARSELETIONX()".

Forgot that countX is not defined in the GEARSELETIONX and in the loop is used prior its definition.

I think, according your code, you are really beginner in C/C++.
About the goto statement: nothing wrong with its use. Sometimes it is necessary, but in the most of cases you can completely avoid of its use. I fully agree with septillion.
Even I can see a benefit of goto in more effective code other techniques have advantage in lucidity of logical structure which is definitely better for programmer especially for the beginners.
Arduino clone with ATmega1284P   http://forum.arduino.cc/index.php?topic=277260.0

septillion

You might be able to follow it yes, but even for a program this size, if you mess them up it already is a lot harder to debug. And without any reason.

You just use the goto's as loops. Why not just replace them with a while-loop (or do-while) and break on the if condition. Much neater.

But I doubt de debug info of the program is very useful:
Code: [Select]
 Serial.println(MS & Pitch & SPR);
So print the bitwise AND of MS, Pitch and SPR (whatever those variables may mean).
0b00000100
0b00000011 AND
0b11001000 AND
----------------------
0b00000000

Aka just 0...

And and if and an array have no similarities whatsoever... First is a decision making routine where the second is an data storage element...

Code: [Select]
else if ( countX = 5) {
    int SELETIONX = fithX;
  }

That will create a local variable SELETIONX but the next line that local scope end and thus that variable is destroyed... Check the difference between variable declaration, defining, initializing and assigning. And check about variable scope.
Use fricking code tags!!!!
I want x => I would like x, I need help => I would like help, Need fast => Go and pay someone to do the job...

NEW Library to make fading leds a piece of cake
https://github.com/septillion-git/FadeLed

groundFungus

Serial prints are a great debugging tool, but useless without a Serial.begin somewhere.

Code: [Select]
void setup() {
  Serial.println(MS & Pitch & SPR);
}

Go Up