A bit of help in Modifying the Program

Hi ,

i am trying to use this , but i need to make it so i can store the data i feed to it, as well as i need to add more servos to it i need to have the data even after i have powered it off or reset it , unless i change it , can anyone please help me with this , i dont know anything about programming. link below to the porgram

https://create.arduino.cc/editor/mertarduinotech/9ebd6971-cf25-4da6-815b-a31574f2e661/preview

also if anyone can suggest a better program with less hassle , would be good too , i am using MEGA for this project.

Thank you Very much

So will you be able to understand the help we can offer?
We do not write code for you.

So the internal EEPROM only has 1K of storage, is that enough? Otherwise you need to add an SD card.

Then the software aspect of that is simple just repeat the initialisation you have in the code for another pin. But you will need to revise the way you power it and apply supply decoupling to the power supply.

Hi,
Welcome to the forum.

Please read the post at the start of any forum , entitled "How to use this Forum".

Is there a reason you need to do this particular project as your first Arduino project?

Do you know how to program buttons/switches, LEDs , the basic hardware?

Have you written any code for the Arduino yet?

Thanks.. Tom... :grinning: :+1: :coffee: :australia:

i am a Mechanical engineering student ,
i have never programmed in my life , did a bit of C++ but that was ages ago ,
i was given a robotics project i have all the component of it, i know you can add an SD card to it to store all the data , but i have no idea on how to use it in this situations , i have a separate power supply for the motors, i can move them as well with this program , i added a few servos so i just need to duplicate it in the program.

i need help in adding stuff to the program, so i can add SD reader and then use that data to move the arm i have afterwards , as the project requires to preload the data or sequences of the movement of the arm , (its like a Robotic Arm , just much longer (yes i said longer, it has to cover about 140cm) i already have the build of it , now need to program it , i have very less or no knowledge regarding programming. ( Dont laugh XD , i know i know what kinda engineer am i , but electronics is not my thing)

Hi,
Can you post the code you have and a circuit?
A picture of your mechanical assembly would help to.
What servos and or motors are you using?
Can you post a link to data/spec about your motor/servo and power supply.

I know it is a lot, but this sort of information, especially a picture of your machine will help us a lot.

It is good you have been able to program some motion and have some knowledge of C++. :+1:

Tom..... :grinning: :+1: :coffee: :australia:

1 Like

i have attached a few pictures , the battery is low powered ( i know , i will upgrade it ) , i used an external power supply for the Board,

the first arms motor is to be replaced as it cannot rotate because of weight as they seems very under powered for there rating , tomorrow i am gonna pick up some with 25kg load torque.

i used this video for the circuit , its pretty simple to understand.

Video Link : https://youtu.be/2-u_N13Xfdo
Code link : Arduino Create

Thank you very much for ur help

It would be much easier to increase the number of servos if the author had use arrays instead of four different names.

Did you need the OLED display?

The sketch can only store eight positions. Is that enough? How many positions do you need to store?

Here is the original sketch:

#include <Servo.h>
#include <OLED_I2C.h>

//define the servos
Servo servo1;
Servo servo2;
Servo servo3;
Servo servo4;

OLED  myOLED(SDA, SCL);
extern uint8_t SmallFont[];

//define the buttons
const int button1 = 12;
const int button2 = 13;

//define variable for values of the button
int button1Pressed = 0;
boolean button2Pressed = false;

//define potentiometers
const int pot1 = A1;
const int pot2 = A2;
const int pot3 = A3;
const int pot4 = A4;

//define variable for values of the potentiometers
int pot1Val;
int pot2Val;
int pot3Val;
int pot4Val;

//define variable for angles of the potentiometer
int pot1Angle;
int pot2Angle;
int pot3Angle;
int pot4Angle;

//define variable for saved position of the servos
int servo1PosSave[] = {1, 1, 1, 1, 1, 1, 1, 1};
int servo2PosSave[] = {1, 1, 1, 1, 1, 1, 1, 1};
int servo3PosSave[] = {1, 1, 1, 1, 1, 1, 1, 1};
int servo4PosSave[] = {1, 1, 1, 1, 1, 1, 1, 1};

void setup()
{

  myOLED.begin();
  myOLED.setFont(SmallFont);
  Serial.begin(9600);

  //define attached pins of the servos
  servo1.attach(3);
  servo2.attach(4);
  servo3.attach(5);
  servo4.attach(6);

  //define buttons as input units
  pinMode(button1, INPUT);
  pinMode(button2, INPUT);

  servo1.write(90);
  servo2.write(90);
  servo3.write(90);
  servo4.write(90);

  myOLED.clrScr();
  myOLED.print("MERT ARDUINO AND TECH", CENTER, 8);
  myOLED.print("PRESS TO SAVE BUTTON", CENTER, 42);
  myOLED.update();

}

void loop()
{
  //read the potentiometer values and define the servo angle to
  //the potentiometer value with the map function
  pot1Val = analogRead(pot1);
  pot1Angle = map (pot1Val, 0, 1023, 10, 179);
  pot2Val = analogRead(pot2);
  pot2Angle = map (pot2Val, 0, 1023, 10, 150);
  pot3Val = analogRead(pot3);
  pot3Angle = map (pot3Val, 0, 1023, 10, 170);
  pot4Val = analogRead(pot4);
  pot4Angle = map (pot4Val, 0, 1023, 10, 170);

  //servos move to mapped angles
  servo1.write(pot1Angle);
  servo2.write(pot2Angle);
  servo3.write(pot3Angle);
  servo4.write(pot4Angle);

  //if button1 is pressed (HIGH), save the potentiometers position
  //as long as button1 is pressed
  if (digitalRead(button1) == HIGH)
  {
    button1Pressed++;
    switch (button1Pressed)
    {
      case 1:
        servo1PosSave[0] = pot1Angle;
        servo2PosSave[0] = pot2Angle;
        servo3PosSave[0] = pot3Angle;
        servo4PosSave[0] = pot4Angle;
        Serial.println("Position #1 Saved");
        myOLED.clrScr();
        myOLED.print("MERT ARDUINO AND TECH", CENTER, 8);
        myOLED.print("POSITION #1 SAVED", CENTER, 42);
        myOLED.update();
        delay(1500);
        break;
      case 2:
        servo1PosSave[1] = pot1Angle;
        servo2PosSave[1] = pot2Angle;
        servo3PosSave[1] = pot3Angle;
        servo4PosSave[1] = pot4Angle;
        Serial.println("Position #2 Saved");
        myOLED.clrScr();
        myOLED.print("MERT ARDUINO AND TECH", CENTER, 8);
        myOLED.print("POSITION #2 SAVED", CENTER, 42);
        myOLED.update();
        delay(1500);
        break;
      case 3:
        servo1PosSave[2] = pot1Angle;
        servo2PosSave[2] = pot2Angle;
        servo3PosSave[2] = pot3Angle;
        servo4PosSave[2] = pot4Angle;
        Serial.println("Position #3 Saved");
        myOLED.clrScr();
        myOLED.print("MERT ARDUINO AND TECH", CENTER, 8);
        myOLED.print("POSITION #3 SAVED", CENTER, 42);
        myOLED.update();
        delay(1500);
        break;
      case 4:
        servo1PosSave[3] = pot1Angle;
        servo2PosSave[3] = pot2Angle;
        servo3PosSave[3] = pot3Angle;
        servo4PosSave[3] = pot4Angle;
        Serial.println("Position #4 Saved");
        myOLED.clrScr();
        myOLED.print("MERT ARDUINO AND TECH", CENTER, 8);
        myOLED.print("POSITION #4 SAVED", CENTER, 42);
        myOLED.update();
        delay(1500);
        break;
      case 5:
        servo1PosSave[4] = pot1Angle;
        servo2PosSave[4] = pot2Angle;
        servo3PosSave[4] = pot3Angle;
        servo4PosSave[4] = pot4Angle;
        Serial.println("Position #5 Saved");
        myOLED.clrScr();
        myOLED.print("MERT ARDUINO AND TECH", CENTER, 8);
        myOLED.print("POSITION #5 SAVED", CENTER, 42);
        myOLED.update();
        delay(1500);
        break;
      case 6:
        servo1PosSave[5] = pot1Angle;
        servo2PosSave[5] = pot2Angle;
        servo3PosSave[5] = pot3Angle;
        servo4PosSave[5] = pot4Angle;
        Serial.println("Position #6 Saved");
        myOLED.clrScr();
        myOLED.print("MERT ARDUINO AND TECH", CENTER, 8);
        myOLED.print("POSITION #6 SAVED", CENTER, 42);
        myOLED.update();
        delay(1500);
        break;
      case 7:
        servo1PosSave[6] = pot1Angle;
        servo2PosSave[6] = pot2Angle;
        servo3PosSave[6] = pot3Angle;
        servo4PosSave[6] = pot4Angle;
        Serial.println("Position #7 Saved");
        myOLED.clrScr();
        myOLED.print("MERT ARDUINO AND TECH", CENTER, 8);
        myOLED.print("POSITION #7 SAVED", CENTER, 42);
        myOLED.update();
        delay(1500);
        break;
      case 8:
        servo1PosSave[7] = pot1Angle;
        servo2PosSave[7] = pot2Angle;
        servo3PosSave[7] = pot3Angle;
        servo4PosSave[7] = pot4Angle;
        Serial.println("Position #8 Saved");
        myOLED.clrScr();
        myOLED.print("MERT ARDUINO AND TECH", CENTER, 8);
        myOLED.print("POSITION #8 SAVED", CENTER, 28);
        myOLED.print("PRESS TO MOVE BUTTON", CENTER, 42);
        myOLED.update();
        delay(1500);
        break;

    }
  }
  //if button2 pressed (HIGH), the servos move saved position
  if (digitalRead(button2) == HIGH)
  {
    button2Pressed = true;
  }

  if (button2Pressed)
  {
    for (int i = 0; i < 8; i++)
    {
      servo1.write(servo1PosSave[i]);
      servo2.write(servo2PosSave[i]);
      servo3.write(servo3PosSave[i]);
      servo4.write(servo4PosSave[i]);
      myOLED.clrScr();
      myOLED.print("MERT ARDUINO AND TECH", CENTER, 8);
      myOLED.print("MOVING...", CENTER, 42);
      myOLED.update();
      delay(2000);
    }
  }
  delay(100);
}

i need atleast 8 servos and would need much more positions , as it needs to move 10 packages to different locations ,

i do not need the oled , it is not required , i just bought it cause it was part of the design of the video linked above

i dont mind changing the program , just that i must support all the servos and i would be able to program it once , then i can just reply the same positions with one button press

Thank you , looking forward to a solution for this ^^

Here is the sketch re-written to use arrays. You can easily add as many pot/servo pairs as your hardware can support (16 on the MEGA). You can also adjust the number of positions to save. At one byte per position per servo you can easily fit 350 saved positions for 16 servos in ram on a MEGA.

#include <Servo.h>

const byte ChannelCount = 4;
Servo Servos[ChannelCount];
const byte ServoPins[ChannelCount] = {3, 4, 5, 6};
const byte PotPins[ChannelCount] = {A1, A2, A3, A4};
const byte PotRangeMin[ChannelCount] = {10, 10, 10, 10};
const byte PotRangeMax[ChannelCount] = {179, 150, 170, 170};

//define the buttons
const int SaveButtonPin = 12;
const int ReplayButtonPin = 13;

//define variable for values of the button
boolean SaveButtonWasPressed = false;
boolean ReplayButtonWasPressed = false;

//define variable for saved position of the servos
const unsigned SaveLimit = 100;
unsigned SaveCount = 0;
byte SavedPositions[SaveLimit][ChannelCount];

void setup()
{
  Serial.begin(115200);

  //define attached pins of the servos
  for (int i = 0; i < ChannelCount; i++)
  {
    Servos[i].write(90);
    Servos[i].attach(ServoPins[i]);
  }

  //define buttons as input units
  pinMode(SaveButtonPin, INPUT_PULLUP);
  pinMode(ReplayButtonPin, INPUT_PULLUP);
}

void loop()
{
  byte positions[ChannelCount];

  for (int i = 0; i < ChannelCount; i++)
  {
    positions[i] = map(analogRead(PotPins[i]), 0, 1023, PotRangeMin[i], PotRangeMax[i]);
    Servos[i].write(positions[i]);
  }

  //if SaveButton is pressed (LOW), save the servo positions

  boolean saveButtonIsPressed = digitalRead(SaveButtonPin) == LOW;
  if (saveButtonIsPressed != SaveButtonWasPressed)
  {
    SaveButtonWasPressed = saveButtonIsPressed;
    if (saveButtonIsPressed)
    {
      if (SaveCount < SaveLimit)
      {
        for (int i = 0; i < ChannelCount; i++)
        {
          SavedPositions[SaveCount][i] = positions[i];
        }
        SaveCount++;
      }
    }
  }

  //if ReplayButton is pressed (LOW), play back the saved positions
  boolean replayButtonIsPressed = digitalRead(ReplayButtonPin) == LOW;
  if (replayButtonIsPressed != ReplayButtonWasPressed)
  {
    ReplayButtonWasPressed = replayButtonIsPressed;
    if (replayButtonIsPressed)
    {

      for (unsigned sp = 0; sp < SaveCount; sp++)
      {
        for (int i = 0; i < ChannelCount; i++)
          Servos[i].write(SavedPositions[sp][i]);
        delay(200);
      }
    }
  }
}
1 Like

so basically i can just add the channel counts to it to increase the servos and positions ? , that looks much simpler , i will try that,

question : will it retain the potions after i reset or disconnect power ? also can u please show me an example if i add more servos to it ?

No. To save the positions you will need some place to put them other than RAM. You will have to save them and read them back.

Really? You can't figure that out? Can you figure out how to remove the 4th servo? If so, you just do the opposite to add a 5th servo.

i need to add the A5, and 7 to both the lines respectively ,

as i mentioned i am as far away from programming as i can be , i never really got into it , i am more of a mechanical designer then an electronic one.

but i am tryint to learn it as i go

This may work.

At some point it may make more sense, as well as save time, to take a pause on the rapid gratification hacking affords and find a tutorial series online that matches your learning style, be it reading or watching the pretty chick (or the nice old man) and really learn this stuff.

I think @johnwasser is getting at what will be a key skill no matter your approach: the ability to see patterns in the code and make changes based on the similarities between what you see and what you want to do.

Between maths, common sense and pattern matching, you’d be surprised how far you can get w/o really knowing anything. Trust me. :wink:

a7

i can do like simple stuff , but i need to add two program in one , there is when i get confused , as i am very new to this ,

i need to attach an sd card to it now and record the movements of it so i can use it again without doing it again ,

i know u attached the SD library to it ,m but i have never attached two programs together

Try and read the code like a story. Each line is one thing you are telling the computer to do. Things happen one after the other, you only have to put them in the order you want them to be done.

You are not putting two programs together.

This is what I warned you about in my first post. It is impossible to help you if you can’t understand what people say.

sorry for my lack of knowledge , i am not a programmer by any means, i added the more servos and pins , so that is good i have more then 6 added to the program. i still cannot understand were do i out the code for the sd card and how will it retain it again .

I would start with the SD library example: ReadWrite. Getting that to work will prove that your SD module is connected correctly and your SD card is formatted correctly.

To write your data, I would start by writing the high and low bytes of SaveCount followed by the first "SaveCount" entries of SavedPositions. Use myFile.write() to save each byte.

To read your data, I would start by reading the high and low bytes of SaveCount. Then read the first "SaveCount" entries of SavedPositions. Use myFile.read() to read each byte.