Advanced Useless Machine (5 Switches), can't memorize the position.

Guys, I have a problem. If i am on button 3 ,for example, and I press the button 1, it will not go back to button 1 position, it will go forward like my motorPosition was 0, not 3. Can you help me with that? I monitorized motorPosition counter on the Serial Monitor and it was fine, but I don’t know how to make it to keep the position and from there to calculate the next position I give to it.

I am using a cd rom stepper motor, and a L9110s h-bridge driver. I attached the code because it is bigger than the character limit here.

5Switch_UseLessBox.ino (8.7 KB)

Using a H-bridge looks wrong to me. Use a proper stepper driver.
Do You start by moving the arm to a calibrated position? Just seting the variable "position" to zero is not enough.

Here is the code:

const byte ROWS = 4;
const byte COLS = 4;

// define here where each row and column is connected to
const byte rowPins [ROWS] = {9, 10, 11, 12}; //connect to the row pinouts of the keypad
const byte colPins [COLS] = {5, 4, 3, 2}; //connect to the column pinouts of the keypad

// remember previous setting of each key
bool pressedArray[ROWS][COLS];
bool backuppressedArray[ROWS][COLS];

void setup ()
{
  Serial.begin (115200);
  for (byte i = 0; i < COLS; i++)
    pinMode (colPins [i], INPUT_PULLUP);

}  // end of setup

void loop ()
{
  bool keyState, change_flag;
  // check each row
  for (byte row = 0; row < ROWS; row++)
  {
    // set that row to OUTPUT and LOW
    pinMode (rowPins [row], OUTPUT);
    digitalWrite (rowPins [row], LOW);

    // check each column to see , if the switch has driven that column LOW
    for (byte col = 0; col < COLS; col++)
    {
      keyState = digitalRead (colPins [col]) == LOW; // true means pressed
      pressedArray[row][col] = keyState;
    } // end of for each column

    // put row back to high-impedance (input)
    pinMode (rowPins [row], INPUT_PULLUP);
  }  // end of for each row
  change_flag = false;
  for (byte row = 0; row < ROWS; row++)
    for (byte col = 0; col < COLS; col++)
    {
      if (backuppressedArray[row][col] != pressedArray[row][col])
      {
        Serial.print(row); Serial.print(" "); Serial.print(col); Serial.println(" Changed");
        backuppressedArray[row][col] = pressedArray[row][col];// book keep the change
        change_flag = true;// Notice logic below that a change has been detected
      }
    }
//if a change has heppened, print out all row * col switches
  if (change_flag) {
    for (byte row = 0; row < ROWS; row++)
      for (byte col = 0; col < COLS; col++)
        Serial.print(pressedArray[col][row]);
    Serial.println();
  }
  delay(10);

Sorry. Totally wrong code......... Another project....

Hi,
Welcome to the forum.

Please read the post at the start of any forum , entitled “How to use this Forum”.
OR
http://forum.arduino.cc/index.php/topic,148850.0.html.
As your code is too long leaving it as an attachment is good.

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?
How have you got your buttons wired?

Thanks… Tom… :slight_smile:

here is the circuit. It is not mine, I found it and tried to understand it, but i can't deal with that position problem

If I change the h bridge, what would you recommend me to use? And if you know, please tell me the code to move the stepper right or left with the specific driver you would recommend.

Hi,
Do you have a DMM?
Can you measure and make sure that the inputs from the switches go from 0V to 5V?

Can you post a proper circuit diagram please, not a Fritzy picture. include your power supply and label terminals and part numbers please.

A hand drawn circuit will be fine.

Thanks.. Tom.. :slight_smile:

Yes, I have a DMM and all the switches work. When I press home button, the stepper motor goes left until i turn the home switch off ( and this is very ok - this way I could verify that when I give in code Left it works). When I press the switches (1,2,3,4,5) the stepper goes right (to the correct position of each button - all this if it is maxed to left). If I am in the 4th buton position and press 2, for example, the stepper motor won't move left (as it had to), it will consider the 4th position to be 0 (maxed to left) and will go 2 position to right. I drawed the circuit and I will attach it here. I verified with the DMM the voltage on every component and it is like 4,6V. The hardware works fine, I think. My problem is in the code. Hope you understand what I am trying to tell here, I am new in all this Arduino things.

Put some serial prints in your code to ensure that you're calling the correct functions.

I suspect that you are and that your problem is that your left function doesn't do what you expect to the stepper.

I'd write a simpler version of your code that uses left and right with a delay between them so that you can prove to yourself whether you have directional control of the stepper.

Looking at Your code.... Try something like this!

void correctPositionLeft() {
  Serial.print(" corr P left. Current position = ");Serial.print(motorPosition);
  Serial.print(" Action: ";
  switch (motorPosition) {
    case 3:
      Serial.println("3");
      left(1);
      break;
    case 6:
      Serial.println("6");
      left(1);
      break;
    case 7:
      Serial.println("7");
      left(1);
      break;

default:
      Serial.println("None");

      break;
  }
}

You only take action if the position is 3, 6 or 7. Nothing happends when poisition is 1, 2, 3 or 5.
Is the leftmost position the 0?

Use Serial Monitor in IDE and Serial.print to tell f ex current poisition, action taken etc.
In Setup You use:

Serial.begin(52100);

Very interesting that you've not had more issues that, from post #8, the power for the stepper motors, h bridge, and servo motor comes from the Uno/ mini.

You actually got motor power running through the breadboard? You know those servos can draw an amp per servo? You know how much current those tiny little breadboard runs can carry? Not very much.

The switches do not have a debounce circuit attached?

I know you may have code issues but the power issues beg for attention before smoke is made.

Good luck.

The connection between Nano Vin to driver board looks not good. The driver needs power and You don't get it from Vin.
In Your drawing You feed 5 volt to the Nano 5 volt pin. The don't connect anything to Vin.

I have to agree that the motors power should be from the power supply and not pass through the board.

as a note, since you state that all the motors are working, then your stepper, driver and servo are all working in the application. no need to change them.

I did not get through all your code, but I did not see how you reversed direction.

in my mind you have a liner location for each switch. if you have a HOME positin and switch 1 is 1,000 steps, switch 2 is 2,000 steps,... etc

and you are at 4,000 steps and the next position is to be switch 3, then you have a relational location.
you have to go backwards.

what you could do is to light an LED when the stepper is to spin in reverse, or as was mentioned, use Serial.print("reverse") or some such

Guys, i managed to solve it, it is working correctly (like 80%), I will try your recommendations to make it work 100%. The problem that I asked for was simply in void loop, if you see, there is a line : motorPosition = 0 that made it not to go back. The code have always taken the position 0 at the start of the next loop. I deleted that line, and it works pretty fine, i just have some problems with calibration of the stepper, but you gave me some useful informations here. Thank you very much, I will come with an update if I can't manage to make it work properly. Thanks a lot!

Well done! Small mistakes easily have hughe impacts.