Need code for Arduino project- help me - Page 1
Forum section: electronics / beginners

author: Imnarnian
author_rank: Contributor
timestamp: March 29, 2022, 04:38:33 am
content: Hi guys. I want to make an abrasive polish with an Arduino . I already make designs (Reference Image 3) and have not yet bought all the components. I just ordered components today except the Arduino and Stepper motor. Arduino prices have gone higher because of the Atmega 328p shortage. I will buy it ASAP. Before that I would like to prepare other things.Hereby I attached some reference Images. I'm not very good at Arduino coding. I watch some Youtube videos and make notes, not code. If there are any wrong or mis-combinations(Reference image 1)please teach me, friends.Here are the components:1. Nema 17 stepper motor with A4988 DriverOR28BJY-48 Stepper motor with ULN2003 Driver2. LCD 16023. 6mm Tactile switches (4)4. Arduino UNO{Nema 17 Stepper motor is quite expensive. But the 28BJY-48 Stepper motor is a cheap one. In performance comparison Nema 17 wins. So I decided to put both motor mounts in my design to use both of them. I thought both are load only difference is Amps. So the only thing I change is to change pins in the Arduino. Correct me please if I’m wrong. If using Only motor is possible, no problem.}Features that I needed:1. Using Arduino UNO and 1602 LCD2. Those 4 Tactile switches forForward, Backward, Zero, Go to Zero(Reference Image 2).3. The LCD display should show Forward movement in the unit of "mm". For example This stepper motor gives 200 steps/ revolution. We are using a 2mm Trapezoidal pitch threaded rod, So it can able to give the forward or backward movement of 0.01mm. So we can easily calculate how much it moves using steps moved in the Stepper motor. The example of a LCD display as shown in the document(Reference Image 2)4. Every Single press of Tactile switch, stepper motor givesonly one step movement. Long press of the Tactile switch gives continuous rotation, of a certain speed. That speed should be fixed in Coding.Can anyone please write code for this for me.THANKS IN ADVANCE !!!I also post this problem in reddit too.

author: matb
author_rank: Regular Contributor
timestamp: March 29, 2022, 06:33:30 am
content: Hi, first some informations missing :What is the LCD type, I2C driven ?How are you going to power your system ?You could first start with a button. Ground on one side and an analog input with pull-up (check INPUT_PULLUP) on the other side. Watching the state changing on the console.Then you can add the other buttons.Then you add an LCD and start just printing the state of your 4 buttons, then text, then numbers.And finally your motor driver.We can help you with debugging but not write the code from scratch as there are lots of exemple on internet (and in Arduino Exemples)

author: Brumby
author_rank: Unknown
timestamp: March 29, 2022, 07:28:15 am
content: Quote from: Imnarnian on March 29, 2022, 04:38:33 amWe are not a programming service. Someone might feel sorry for you - but don't expect this.We are the sort of crowd that will helpyoubuild such projects. Hints, tips, examples of this kind or the other. Electronics being the main strength here, but coding experience is around as well.We like people to learn. You make the effort and you will quite often find a few members who are more than willing to help you get there - and you will be better prepared for your next project.

author: tooki
author_rank: Super Contributor
timestamp: March 29, 2022, 09:26:03 am
content: Quote from: Imnarnian on March 29, 2022, 04:38:33 amSure. That’ll be $195/h.In all seriousness, none of us were any good at coding in the beginning. It’s why you start at the beginning and practice. Figure out and understand the “blink” sketch. Figure out how to use buttons to turn the LED off and on. Figure out how to write to an LCD. Figure out how to get each piece working. Then you start combining.

author: Imnarnian
author_rank: Contributor
timestamp: March 29, 2022, 10:07:18 am
content: Hi sir Thanks for your reply.It's a I2C driven sir. I powered it with 12V SMPS.Yeah sir. The problem is I dont know how to write or edit codes. I will study some basics and let you know sir. Thanks for your reply.

author: Imnarnian
author_rank: Contributor
timestamp: March 29, 2022, 10:09:04 am
content: Thanks for your reply sir.Sorry putting post like this. I will learn Arduino and try some codes and post for your review sir.

author: Imnarnian
author_rank: Contributor
timestamp: March 29, 2022, 10:11:10 am
content: Thanks for reply sir.This induce me in Arduino coding and understanding how it works. I will learn codes and edit existing codes and post here for your reply sir.

author: Zipdox
author_rank: Regular Contributor
timestamp: March 29, 2022, 11:14:22 am
content: You have example sketches and stackoverflow. Use them.

author: Imnarnian
author_rank: Contributor
timestamp: March 29, 2022, 11:55:04 am
content: Thanks for your help Zipdox

author: rstofer
author_rank: Super Contributor
timestamp: March 29, 2022, 03:14:05 pm
content: In the IDE select File->Examples->Stepper or File->Examples->Liquid Crystal or File->Examples->Digital->ButtonIt's all right there in the IDE but if you have something specific, just Google for 'Arduino <whatever>'Literally, EVERYTHING that can be done with an Arduino has already been done and it's all on the Internet. That's why people select the Arduino in the first place. Most of the work has already been done.Google for 'Arduino CNC' and one of the replies is:https://howtomechatronics.com/tutorials/how-to-setup-grbl-control-cnc-machine-with-arduino/It's all out there, all you have to do is look.

author: SL4P
author_rank: Super Contributor
timestamp: March 29, 2022, 10:50:29 pm
content: You could do a lot worse than visiting the Arduino Forumhttps://forum.arduino.cc/If you go in well prepared, and put some effort in for yourself, I guarantee you can solve your requirements.

author: Brumby
author_rank: Unknown
timestamp: March 30, 2022, 12:39:28 am
content: The key is to start with something simple - like making an LED flash.This is actually avery importantachievement! By getting that to happen, you have set up the development environment - which isthemost important first step - and you have a few "simple to understand" lines of code.I would suggest it is important that you understand how that simple code flows in execution before you start looking at more complex programming - and when you move to more and more complex code, do so one step at a time. By doing this, you will only be dealing with one new feature, so if there are problems, you can keep your investigations confined to that one area. If you add five new features to your code and have problems, then sorting out where the problems have crept in can be really difficult, especially if you are just getting into programming.First learn to crawl, then learn to walkbeforeyou want to get up and run.

author: Brumby
author_rank: Unknown
timestamp: March 30, 2022, 12:45:16 am
content: Quote from: Imnarnian on March 29, 2022, 10:11:10 amExcellent! This is the best way (in my opinion).That's how I started with Arduino programming - and I started writing code professionally last century.

author: RJSV
author_rank: Super Contributor
timestamp: March 30, 2022, 10:30:28 am
content: Those replies are good, and I could add a little:Just doing that first step, (read previous couple of posts, from several people). That first step, I would probably have trouble, with how to type in one of the 'BIT' functions, those (exacting) things can be frustrating, but not so bad.Like, for example, you might need something looking like:. LED1_port2 EQU 04010hBlink_ON EQU (0001000b):SETB LED1 ; Blink_ON...Stuff looking cryptic like that, where you know what to do; it's just to set the LED bit, in the processor system output port. But, regardless of processor type, you have to focus, on getting exact, with those couple lines of your CODE. A couple of lines, in my general example, do the defining, but, instead of thinking:..." SETB 0XXXBXXX b ", you just translate, as you go, instead thinking the common sense way;"That will turn on the one LED bit, in the PORT I want..."Once you get over a couple of those hurdles, you can turn attention to the flow of your code, maybe it loops, just endlessly, to start, (as others have described).A person alone, might struggle a bit, but you, here, can just put the question onto your EEVblog thread. As I said, often it's just a format/ typing issue, with the strange looking characters and numbers mixed.What does need to happen, you need, maybe, some good background, on the so-called Hexadecimal formats, and a little binary math.So you know, for example, '00001000'b That's a '4' in regular decimal. Or, for hexadecimal address, an output port might be: '04000'h ...That defines the PORT location, memory mapped.I couldn't even tell you, directly, what value is 'Four-Thousand, hex'... I think in decimal it's going to be some 16, 440 decimal. (Literally it's 16 X 1024).See what I mean ? The little, pesky format details, that can really distract, when you have some bigger problem to analyze.But don't let 'hex' numbers, and strange definitions deter you: You've got great access to folks all too willing to aid your efforts. Writing code can be hard, but that's how you can earn some bucks, if you take effort to learn!

author: tooki
author_rank: Super Contributor
timestamp: March 30, 2022, 12:51:03 pm
content: RJHayward, do you honestly think that the OP, who’s befuddled by introductory Arduino coding and has said they’ve written no code yet, could even begin to make sense of your post?? And that’s if your writing were clear and coherent, which it sadly is not. You’ve got tons of strange, run-on sentence fragments joined with bizarre comma usage. (Is it supposed to be William Shatner-style pausing between words?)

author: RJSV
author_rank: Super Contributor
timestamp: March 31, 2022, 04:26:41 pm
content: Yeah, all correct, RE: my run-on sentences!But I bet, almost all of us felt that way, when first confronted by 'HEXADECIMAL' numbers, and especially the 'arcane' and highly specific language needed to enter a 'Bit function' character by character.Heck, some newer programmers are always needed, and some encouraging words regarding the strange language needed, to enter CODE.I come from background where I always like to know what the OBJECT code output generates; a sort of LITERAL reading. But that's a hardware vantage.So question, today, I'm wondering, do you have a development system / software package, for editing things, and for de-bug problems ?That might be a whole 'nother set of questions and issues, somebody like me can learn, as others answer your questions.I'm not SUPERMAN, just did 'some' coding...

author: tooki
author_rank: Super Contributor
timestamp: March 31, 2022, 04:35:17 pm
content: The OP says Arduino right in the thread title. Unless otherwise specified, this generally means an Arduino board (or clone), using the Arduino environment (i.e. C++ and the Arduino standard libraries), in the Arduino IDE, without any true debugger.

author: Imnarnian
author_rank: Contributor
timestamp: April 09, 2022, 07:25:42 am
content: Hi guys, In my previous post I asked for code.What I did :1. I found code for Stepper motor controlling with Arduino with LCD and rotary encoder in curious scientist. Link below :https://curiousscientist.tech/blog/stepper-motor-direct-control-rotary-encoder2. I connect the parts as per the code and put it in Arduino. After I connect the power supply, it won't work.3. I don't have a stepper motor. So I didn't connect the motor. I just want to know, Is LCD works or not. Motor side is not a problem It will work when I connect it to driver and Arduino. But at the same time, I have an another doubt. I use a rotary encoder and stepper motor. Is Arduino counts and check the feedback voltage of stepper motor Initially. maybe that's the reason for its not working.4. Finally I check with youtube video. link below :5. The code in youtube and the code in google is not same. So I change the google code to Youtube code. its not working.Can anyone help me please.I attached some reference images.Code in Youtube video ://AccelStepper#include <AccelStepper.h>AccelStepper stepper(1, 9,;// pulses Digital 9 (CLK); Direction Digital 8 (CCW)//16x2 LCD#include <LiquidCrystal_I2C.h> //SDA = A4, SCL = A5LiquidCrystal_I2C lcd(0x27, 16, 2);//Defining pinsconst int RotaryCLK = 2; //CLK pin on the rotary encoderconst int RotaryDT = 4; //DT pin on the rotary encoderconst int RotarySW = 3; //SW pin on the rotary encoder (Button function)//Defining variablesint ButtonCounter = 0;int RotateCounter = 0;//Statusesint CLKNow;int CLKPrevious;int DTNow;int DTPrevious;// Timefloat TimeNow1;float TimeNow2;void setup(){Serial.begin(9600);//------------------------------------------------------lcd.init(); //initialize the lcdlcd.init();lcd.backlight();//------------------------------------------------------lcd.setCursor(0,0); //Defining positon to write from first row,first column .lcd.print("Rotary encoder");lcd.setCursor(0,1);lcd.print("Stepper stepping"); //You can write 16 Characters per line .delay(5000); //wait 1 sec//------------------------------------------------------pinMode(2, INPUT_PULLUP);pinMode(3, INPUT_PULLUP);pinMode(4, INPUT_PULLUP);//Store statesCLKPrevious = digitalRead(RotaryCLK);DTPrevious = digitalRead(RotaryDT);attachInterrupt(digitalPinToInterrupt(RotaryCLK), rotate, CHANGE);attachInterrupt(digitalPinToInterrupt(RotarySW), buttonPressed, FALLING);stepper.setMaxSpeed(1000); //SPEED = Steps / secondstepper.setAcceleration(5000); //ACCELERATION = Steps /(second)^2TimeNow1 = millis(); //Start time}void loop(){printLCD();RunTheMotor();}void buttonPressed(){TimeNow2 = millis();if(TimeNow2 - TimeNow1 < 1000){ButtonCounter++; //increase the counter}TimeNow1 = millis();//You can add something here, like resetting the RotateCounter (e.g. redefine 0 position)}void rotate(){CLKNow = digitalRead(RotaryCLK); //Read the state of the CLK pin// If last and current state of CLK are different, then a pulse occurredif (CLKNow != CLKPrevious && CLKNow == 1){// If the DT state is different than the CLK state then// the encoder is rotating CCW so increaseif (digitalRead(RotaryDT) != CLKNow) {RotateCounter++;} else {// Encoder is rotating CW so decreaseRotateCounter--;}}CLKPrevious = CLKNow; // Store last CLK state}void printLCD(){//lcd.clear();lcd.setCursor(0,0); // Defining positon to write from first row, first column .lcd.print("Clicks: ");lcd.setCursor(8,0);lcd.print(" ");lcd.setCursor(8,0);lcd.print(ButtonCounter); //Print the number of button clickslcd.setCursor(0,1); // Defining positon to write from second row, first column .lcd.print("Position: ");lcd.setCursor(10,1);lcd.print(" ");lcd.setCursor(10,1);lcd.print(RotateCounter); //Print the number of pulses}void RunTheMotor() //function for the motor{stepper.enableOutputs(); //enable pinsstepper.moveTo(-1*RotateCounter); //-1 is to match the rotation of the encoder with the rotation of the stepperwhile(stepper.distanceToGo() != 0){stepper.runToNewPosition(RotateCounter);}}

author: tooki
author_rank: Super Contributor
timestamp: April 10, 2022, 01:38:24 am
content: Multiple people have told you what you need to do: START AT THE BEGINNING. Don’t try to get a complex bit of code running until you can get simple stuff running and understand what it’s doing. Step by step.P.S. fix that atrocious formatting on the code there — or just delete it altogether since it’s just copied from the linked page!

author: rstofer
author_rank: Super Contributor
timestamp: April 10, 2022, 11:13:50 am
content: As tooki points out, it's best to start small.My first order of priority is to get a "hello world" bit of code working. That is vitally important because it proves that the standard libraries are present, the compiler works, the bootloader works and the USB serial port works. That is significant progress!For this project, I would then add the LCD display. Since this will be based on a 3rd party library and specific components (yes, you have to use the exact display that matches the library) I can expect to have to spend time getting this to work. But it must be rock solid before moving to the next step.I might work on pushbuttons next. These are easy but I would still write test code and use serial output to the terminal to indicate a change in button state.Finally, I would work on the stepper code. I would write very simple functions to just run the motor. The fancy position counting can wait until I know the motor steps. I expect to spend quite a bit of time getting this phase to work. Again, the library and components must match perfectly.Finally, I would add all the fluff. Position counting and such would be written near the end of the project, not at the beginning.At every step in the process, I am going to be using serial output to keep track of what is going on. I would leave any test output code in place until the very end of the coding process.Anyway, that's the way I would approach it. What I wouldn't do is download somebody else's code and blindly try to get it to work.

author: Imnarnian
author_rank: Contributor
timestamp: April 11, 2022, 09:53:34 am
content: Yeah ok sir,Thanks for reply @tooki, @rstoferI did hello world, servo tester, blink code, Also did some stepper motor controller with small changes. Then only I go to website and did like this. If this is not enough I will learn and do some fun activities with Arduino and understand how it works.