Notifications
Clear all

Need Arduino programming help

26 Posts
7 Users
1 Likes
2,626 Views
(@digidug80)
Member
Joined: 4 years ago
Posts: 4
Topic starter  

I need some help with Arduino IDE programming. I’m new to this. I want to control a Nema stepper motor with 2 separate push buttons which will control the direction of the motor at a certain set speed. One button to turn the motor clockwise and the other counterclockwise. I’ll also be using 2 Hall switch sensor modules on each end so that when I press (we’ll say the button on the right) it will move the motor to where the hall sensor is located on the right side and stop. Then when I press the left button it will move the stepper motor to the other side and stop where the left sensor is located. I’ll be using an Arduino uno for this project. I’m starting to learn the program language for Arduino but don’t understand it enough to make this work the way I want. Any help would be greatly appreciated! Thank you SO MUCH!


   
Quote
WolfgangW
(@wolfgangw)
Member
Joined: 4 years ago
Posts: 70
 
Posted by: @digidug80

... I’ll be using an Arduino uno for this project. I’m starting to learn the program language for Arduino but don’t understand it enough to make this work the way I want. 

Hi,

can you be a little more specific in where exactly you have problems with? You are writing about "program language for Arduino" means you asking about C/C++?

And do I understand you right, that your main problem is about understanding this language?
And if so, do you have any experience about programming?

I didn't make use of buttons or Hall switch sensor modules until now, but I think this is not really the problem.

Wolfgang

 

If I am not here, then I am most probably somewhere else


   
ReplyQuote
(@digidug80)
Member
Joined: 4 years ago
Posts: 4
Topic starter  

@wolfgangw

Hey There! Thanks so much for your response. Sorry to confuse you about this. Yes, I don’t know C/C++. I Don’t have much experience with programming besides toying around with examples of sketches or trying to make sense of other people’s projects and take bits and pieces from them. Just to give you an understanding of what I’m trying to do...I am building an arcade where the control panel slides back and forth converting from 1 to 2 players. The controls sit on a rail to allow it to easily move and is pushed and pulled by a single stepper motor. The magnetic hall effect sensors are on either ends to stop the control board at a specific area, and to keep it from traveling too far. As far as the Arduino IDE programming...I know I am to define my components and have a loop, etc. and Everything is set up and ready and I know it’ll work but I just don’t have the programming knowledge to tell it what to do and how to do it . Very frustrating! Hope that all makes sense. Anyway, again...any help on this would be very very much appreciated!


   
ReplyQuote
robotBuilder
(@robotbuilder)
Member
Joined: 5 years ago
Posts: 2042
 

@digidug80

One step at a time.  There are plenty of tutorials that teach electronics and programming.

https://dronebotworkshop.com/stepper-motor-hall-effect/

https://dronebotworkshop.com/big-stepper-motors/

 


   
ReplyQuote
frogandtoad
(@frogandtoad)
Member
Joined: 5 years ago
Posts: 1458
 

@digidug80

Posted by: @digidug80

I’m starting to learn the program language for Arduino but don’t understand it enough to make this work the way I want. Any help would be greatly appreciated! Thank you SO MUCH!

Do you have any programming knowledge?, even in a different language?

Cheers.


   
ReplyQuote
WolfgangW
(@wolfgangw)
Member
Joined: 4 years ago
Posts: 70
 
Posted by: @robotbuilder

One step at a time.  There are plenty of tutorials that teach electronics and programming.

right, but he has to explain where he need help. I'd like to help at least about the programming. If this is what he need. But he has to tell. For beginner it is always difficult to get into something. And if he is new in programming as well as in electronics, then he has double difficulty.

Anyway, it is his (@digidug80)s turn now.

If I am not here, then I am most probably somewhere else


   
ReplyQuote
WolfgangW
(@wolfgangw)
Member
Joined: 4 years ago
Posts: 70
 

@digidug80

I don't know why, but your message just reached me today. Ok so far. I need to agree with RobotBuilder, when he said "One Step at a time".

You have to start from the scratch, there is really no other way for you. If you try to start somewhere in the middle, you are going to fail, because you don't understand what you do. I think this is what robotBuilder wanted to tell you.

Your benefit now: You don't have experience with other languages. So you don't have to think about how would this work in language A and then try transfer to language B.

Now the language you use on the Arduino is only a part of that powerful C++. Most things you don't need to worry about, because you'll never need, is you only focus on an Arduino. This C / C ++ that is used there is actually quite limited, but precisely tailored to this small device. You can expand it within certain limits for your goal. But still, there is something you really have to dive into.

Only few months ago, I was sitting there in front of an Arduino like you did and I asked myself "ok, what can I now do with that?" So I started the most simple thing to try and see how this language works. I came, I saw and I expanded that into 5 blinking LEDs, and I tried everything to work it out. All I can say is try from the very beginning. This will need time, but this way probably brings you the success you want.

Before you start now to try, it is important to understand the basics of such a program.

This is the basic code when you start that Arduino C-Editor.

void setup() {
// put your setup code here, to run once:

}

void loop() {
// put your main code here, to run repeatedly:

}



void - is a reserved word in language which tells you this is a method which is running with the code inside.

These 2 methods, you may not remove. You really need that. The code inside method setup() will setup variables and devices. It is these 2 standardized and established methods that are always required. Otherwise nothing works.

loop() as it say does nothing else than repeat that whole function you can see if your program and circuit is working. 

Another thing here is a function. A function (it is a must) returns a specific value. This, you have to define.
a function does not start with the word void, but with a type, because the programm needs to know what is the expected value of.

int Add()
{
      int value = 0;
      ...
      return value;
}

bool isThisTrue()
{
    bool value = false;
    ...
    return value; 
}

and no matter void or function, you can handover parameters.

int Add(int a, int b)
{
      int value = a + b;
      return value;
}

 

You'll need variables and you need to learn about the types, constants, #defines and so on. You'll have to import other libraries. So you see, there is something to learn and understand. It is rather boring to create a blinking light, but... this will let you understand. And I can tell you from my experience until now - it is great to understand how this or that works.

Try with small steps, read https://www.arduino.cc/reference/en/

or copy a finished circuit with a finished program. But then, you'll never get into it. Except I misunderstand your goal and this is simply to build this only piece and then let hand off electronics and programming for the rest of your life. In any other way, learn to get up and stand stable, before you start running.

I sure try help you more to get into that, if you want to.

Wolfgang

 

If I am not here, then I am most probably somewhere else


   
ReplyQuote
MadMisha
(@madmisha)
Member
Joined: 4 years ago
Posts: 340
 

If you are learning from scratch, I would recommend this playlist from Paul McWhorter. He is good at getting you through the basics.

 

 

If you start watching, I would say that within the first 8 or so you would have a general understanding. I would recommend going further but start creating as soon as possible. Creating things will help you understand and remember.


   
ReplyQuote
(@digidug80)
Member
Joined: 4 years ago
Posts: 4
Topic starter  

@wolfgangw

 

Thanks so much for your advice and input. I really do appreciate the time you’ve taken to try and help me understand things better. I will certainly do some basic hands on experimenting and go from there. Thanks again!


   
ReplyQuote
WolfgangW
(@wolfgangw)
Member
Joined: 4 years ago
Posts: 70
 

@digidug80

Hi

I am glad if I can help, even I know this does not solve your recent problem. But I am sure you find the solution yourself, after some more easy trials with lower level experimenting.

I have another suggestion for you. The original Arduino Programming GUI is quite poor with help. It doesn't support you that much.

Visual Studio Code

https://code.visualstudio.com/

is much better for writing your code, but my best suggestion would be, because there is no workaround...

Visual Studio 2019

https://visualstudio.microsoft.com/de/downloads/

this is a complete environment for developing software. It is for free called Visual Studio Community. We can debate, whether this is the best or not. But there are also free extensions, which allow you to send your program direct to an Arduino. I am working with VS since 10 years in my profession. 
But even though, I never had experience with C/C++ before. 

Nevertheless, there is no way out for your dilemma, you have to learn from the scratch. But you're lucky, there are many people posting videos on youtube for to learn from the very beginning. 

VS and is a huge piece of software that can easily scare you at first, but you can really do a hell of a lot with it.

One more:

if you naming variables or methods/functions. It is bit like to find a matching name for your child and sometimes forces you to be a little inventive. My experience: don't make the name too short. These labels should explain as much as possible. Use the so-called camel-toe nomenclature, such as "SendDataToDatabase". It says clearly what you want to do.
Use comments. You are just getting started. It means you will go through an evolution, you will constantly improve. What you're writing today will seem ridiculous to you in a few months.
The worst thing that can happen to you then is that you can no longer understand what you did back then.

Have some great experiences

Wolfgang

If I am not here, then I am most probably somewhere else


   
ReplyQuote
WolfgangW
(@wolfgangw)
Member
Joined: 4 years ago
Posts: 70
 

@digidug80

Here is a link for Visual Studio Code (light version), which says you can install this even on a Raspberry Pi. I haven't tried yet, but be sure, I will.

https://www.hanselman.com/blog/HowToInstallVisualStudioCodeOnARaspberryPi4InMinutes.aspx

I hope you'll come back when you finish, and when you made some experiments successful.
I am sure there are more experiences I can share with you.

Wolfgang

 

If I am not here, then I am most probably somewhere else


   
ReplyQuote
codecage
(@codecage)
Member Admin
Joined: 5 years ago
Posts: 1037
 
Posted by: @wolfgangw

a function does not start with the word void, but with a type

Help me out here Wolfgangw, I thought the "void" just meant nothing is returned.  But if you use global variables variables that you don't necessarily need to return anything.

I've never written any code I got paid to write, just for myself, so maybe I'm way off base here.

SteveG


   
ReplyQuote
WolfgangW
(@wolfgangw)
Member
Joined: 4 years ago
Posts: 70
 

Hi Steve,

first of all, you are not wrong. Indeed void means, that there is nothing returned. Quit correct so far.

In C# we call this "void" a method. In Pascal/Delphi it is called procedure and I think you see the dilemma. Every language has bit different terminology. Anyway, I didn't wanted to overwhealm the new one with too many things. Because I know they will come.
Void means a method or procedure, which just do something, but nothing is returned. A function on the other hand is returning something. But only the specific value, that is expected and defined.

Posted by: @codecage

But if you use global variables variables that you don't necessarily need to return anything

global variables... now we are already talking about world views. It's getting more spiritual now 😉 

For programmers, global variables are bad. They are not protected, so you can change them from everywhere.
This is sometimes necessary and usefull, but also somehow dangerous. Ever since we have a programming tool called OOP Object Oriented Programming, we can write code which is encapsulated and therefore protected. And the best is, you can use that many times same time as class in several instances.

Just for digidug80 I didn't wanted to talk such at that point, because such is only irritating.

Here, we are talking about Electronics, talking about to program a chip, which is quite different as programming a GUI-Tool for Windows, where OOP is good to be used. But right here, you are using OOP too, even it is less than for windows programming.

There is a library DHT. For me, I see a class. A class which is used in same way in OOP. Made, following the same rules. There are areas, which are open to use and others, which are protected. But now, because this is a class, you can use this for several DHT22 e.g. just giving each sensor a different name. Too much for a beginner. 

For me, the rules of architecture say

- make use of OOP, use and create own classes. Write safe code. Write once, use many times.
- make use of global variables as less as possible.
- try to make your code readable. Means make use of voids and functions and
  take care these methods are not too big. some say not more than 20 lines,
  other say more common, not more than one page on screen...

that is now, what I am talking about world views. In electronics, things are little bit different. In electronic, speed is more necessary than "beautiful" architecture and OOP and the rules of programming. But other hand, if anyone makes use of Delay(1000) and maybe more, speed is not so important at all. 
The reason I am telling this is, each "jump" from one method to another, using classes and everyting cost some computing time for the chip. So in some occasions this time is important. Bill for example say it is not necessary to be a rocket engineer ... So while we are not rocket engineers, we can try to make code which is more easy to understand and maintain. 

I am honestly not through yet with OOP and C++ (C doesn't provide OOP so far I know). But I understand some basics now. And I try to make use of it in the way I am used to it.

Ok, enough for now, I am afraid I push our new Padawan away.

I just hope I could make myself clear for you to answer your question.

Greetings and TGIF

Wolfgang

If I am not here, then I am most probably somewhere else


   
ReplyQuote
codecage
(@codecage)
Member Admin
Joined: 5 years ago
Posts: 1037
 

@wolfgangw

Thanks for the explanation!  Having done just small programming tasks for my own hobbies, I have always just used global variables and for what I have been doing it has not caused me any issues.  You have opened my eyes to the fact that this could indeed lead to problems down the road so to speak.

I am currently working through Paul McWhorter's tutorials on the Elegoo Smart Car and have been working on moving some inline coding he was doing to some functions but using global variables.  You have now given me the challenge to rewrite these functions using local variables and then just returning the values I need.  I'm going to give that a try today!

And in response to your TGIF, being retired Fridays don't have the same meaning as when I was working, but now, at my age, it is more like TGIMITT.  Thank God I Made It To Today! 😎  

SteveG


   
ReplyQuote
frogandtoad
(@frogandtoad)
Member
Joined: 5 years ago
Posts: 1458
 

@wolfgangw

Please allow me to elaborate, and make some corrections.

Posted by: @wolfgangw

a function does not start with the word void, but with a type

Though implemented differently under the hood, in C, C++ and C#, 'void' is actually a language type.

Posted by: @wolfgangw

Hi Steve,
first of all, you are not wrong. Indeed void means, that there is nothing returned. Quit correct so far.

Indeed, this is correct... nothing is returned when the keyword 'void' is placed before a method, procedure or function, but there is a little more to 'void' than that [1].

Posted by: @wolfgangw

In C# we call this "void" a method.

Actually no... "void" refers to a language type, and not a method, for either C, C++ and C#.

Posted by: @wolfgangw

Void means a method or procedure, which just do something, but nothing is returned. A function on the other hand is returning something. But only the specific value, that is expected and defined.

This is the most important thing I want to address.  The keyword "void" does not mean to refer to a method or procedure or function at all. In programming, what is generically known as a block of code, is what we formally refer to as methods, procedures or functions.

Please note that methods, procedures and functions, can return 'void' or some other type, defined by the language, so functions are not any more special in this regard.

[1] - When a variable is declared as a pointer to 'void*', it can act as a generic container, that can store locations of other language types, without knowing their specific type in advance.  This means that void with the help of a pointer, can store data as a variable, and return the data pointed to from methods, procedures or functions too.

Cheers.


   
ReplyQuote
Page 1 / 2