[Boards: 3 / a / aco / adv / an / asp / b / biz / c / cgl / ck / cm / co / d / diy / e / fa / fit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mu / n / news / o / out / p / po / pol / qa / r / r9k / s / s4s / sci / soc / sp / t / tg / toy / trash / trv / tv / u / v / vg / vp / vr / w / wg / wsg / wsr / x / y ] [Home]
4chanarchives logo
Hey I'm trying to program a digispark arduino to produc
Images are sometimes not shown due to bandwidth/network limitations. Refreshing the page usually helps.

You are currently reading a thread in /diy/ - Do It yourself

Thread replies: 21
Thread images: 2
Hey I'm trying to program a digispark arduino to produce a joystick output to usb, but the documents and tutorials don't seem to really explain how to use it.

They have this example code:
http://pastebin.com/spVJxZGP

Specifically I'm talking about those DigiJoystick.setX() functions. Just from looking at the function in the master file
>void setY(byte value) {
> last_built_report[1] = value;
> }
The <byte> argument doesn't seem to do anything. When I am using this function do I actually pass the <byte> datatype itself as the first argument? Or do I have to supply a variable to the byte type, and if so what is this variable supposed to represent?

In my own project all I am trying to do is derive a value from a sensor I have and output a relative single axis joystick signal.

Oh and sorry if /diy/ isn't really the place for arduino programming advice.
>>
>>947760
>The <byte> argument doesn't seem to do anything.
Bollocks doesn't it.

You just listed above exactly what it does: it updates a data structure which gets read and acted-on by the USB library every time you call delay() or update().
>When I am using this function do I actually pass the <byte> datatype itself as the first argument?
Why would you ever need to tell a function what type its own parameters are? I think you need to go read a book on C, or procedural programming in general.

If you're getting confused by the sample code, the "(byte)"s are explicit casts to byte, so that expression gets converted into the type the function expects.
> Or do I have to supply a variable to the byte type,
I think you mean "of the byte type"
>and if so what is this variable supposed to represent?
Not a fucking clue. RTFM.
>>
>>947760
Holy shit, I just worked out what you're doing.

You're misreading the type declaration in the function definition as if it were an additional parameter.

When you say

int OPIsATit(int nosrsly)

That's only one parameter. The first token is the type (int), the second token is the name (nosrsly). If you want to call OPIsATit, you have to provide one parameter, and that parameter has to be an int.

Go read a babby's-first-C-book right now. How can you possibly hope to write when you can't even read yet?
>>
>>947792
ah ok that makes sense. I've just never seen typecasting where the type is in parenthesis like that. I do code in other languages, but I'm really only just learning how to program arduinos for this project. In any case that really explains it and, yeah a bonehead mistake.
>>
>>947800
thats the only type of typecasting.
int a = 0; declars a new variable of int type named a.
int a = 0;
printf("%c", (char) a); // says that even though a is an integer you're going to take it like its a char and you're gonna like it even if it rips you wide open.

I second the other anon, go learn C then try to make something. how the fuck have you even heard of type casting yet?
>>
>>947800
Go read Head-First Java.

Yes, Java is not C, but Java is sufficiently Cish that it'll set you in good stead for learning C properly, not just C-that-works.
>>
>>948897
>s, Java is not C, b
you might as well tell him to learn python first.
op dont listen to this bullshit. java is not used to program microcontrollers and if your goal is to program a microcontroller then you shouldnt waste your time on java.
>>
>>948919
No, you might as well pull your head out your arse.

Java is a strongly-typed curly-braces language.
C is a strongly-typed curly-braces language.
Python is a duck-typed whitespace language.

Java and C syntax is near-identical, to the point where if I were to show you a short snippet out of context, you wouldn't be able to tell them apart. The standard libraries, too, are broadly the same in level and scope.

Java absolutely is a stepping-stone to C, because it teaches pointers, C syntax and method-invocation OO, without the complexities of pointer arithmetic, memory management, and multiple inheritance.

Whatever the language, good code is good code, and bad code is bad code. The Head-first series teaches normal people to write good code, in a language-agnostic way. Yes, it uses Java, but that's because (as discussed earlier) Java is an excellent language to learn the fundamentals of procedural programming in.
>>
>>948944
I wouldn't say C is strongly typed. You can do whatever with whatever type if you want.
And Java does NOT have real pointers. Nor will it let you mess around with low level stuff.
Java is not a stepping stone to anything and it doesn't teach you anything about how things actually work.
C (and lisp if you're insane) is the stepping stone to all other languages.

Learn C. It will teach you a bit of how things happen on the hardware and that's what you can use for programming microcontrollers (unlike Java).
There's also the option of Pascal, which is literally a programming language for learning programming. It's horrible, useless and disgusting.
>>
>>948964
>I wouldn't say C is strongly typed.
Then you're not qualified to be opining on this matter, because "strongly typed" is a term of art, and C being strongly typed is a fact not open to debate.
>>
>>948964
>And Java does NOT have real pointers

>A C++ pointer does not allow you to just point to an arbitrary memory address and read the contents of it. It does not allow you to increment a pointer past the end of an array.

>The language just has no way to detect if you do those things, and no way to catch the error when you do it. But that doesn't mean it's allowed. It's undefined behavior, and in short, could do anything.

>Java very nearly does have pointers. Here's a nearly complete list of what you can legally do with a pointer in C++:

> - dereference it to get the pointed-to object
> - reseat it so it points to a different object
> - make it a null pointer
> - if it points into an array, you may perform pointer arithmetics, adding to, or subtracting from the pointer, as long as the result still points into the same array, or taking the difference between two pointers if they both point into the same array.
>The last point is really the only one that isn't possible in Java.

-- http://stackoverflow.com/a/8080788
>>
>>948944
>Java and C syntax is near-identical
HAH
>>
>>948944
> and method-invocation OO
Java is object oriented, C is not.
Also, Java teaches improper and lazy programming methods. Memory management can be lazily done without any real consequences and not a lot of tutorials go in depth about memory management at all.
The rest is more of a discussion if you think you should learn abstract first, and then go into more detail and lower level, or whether to first learn how it works low level so that when you go to higher level languages, you know what happens when you program certain things.
Also, if you want to program microcontrollers, there is no excuse to learn java. C is easy enough to understand because it lacks classes and the object oriented way of thinking, which is imho detrimental to the learner (i.e. too much at once).
Java is practically useless for microcontrollers, and thus can be considered a waste of time.
If you cannot learn C, then you should rethink whether programming is something for you.
And finally, if you have learned C, the step up to C++ is a lot safer than from java to C++, since you only need to learn object oriented programming, but you are used to working with a language that can fuck your shit up if you write shitty code.
>>948988
Who was talking about C++? He said learn C, that is something different.
>>
>>948944
java is based off of C. java is not a "strongly-typed 'curly-braces' language" you fucking autist, its a "C-family language", because it is literally based of of C.

if op is going to Nuremberg why would you tell him to learn English first? this is the exact same situation. he needs C not java.
>>
>>948984
https://en.wikipedia.org/wiki/C_%28programming_language%29
"Typing discipline: static, weak"
Guess you're not qualified yourself then.

>>948988
Java has references, not pointers. There's a difference.
>>
>>949003
Java is only OO if you write OO code.

There's absolutely nothing stopping you from staying in a static context and writing only static code, just as there's nothing stopping you from writing conformant C and giving it to a conformant C++ compiler.
>>
>>949201
Yes, there's a difference, and that one and only difference is that you can't perform pointer arithmetic on them.

Everything else that C/C++ lets you do to pointers is undefined behaviour and just happens to work on some implementations.
>>
>>947760
Dylan?
>>
File: 3c0.jpg (188 KB, 650x650) Image search: [Google]
3c0.jpg
188 KB, 650x650
>>949209
>Java is only OO if you write OO code.
Stop the damage control please. You know just as well as i do that that does not matter at all. If you are learning java you are also learning how to program in an object oriented way, so what you are saying is completely irrelevant. Also, even if you want to write static code, you have to write it within a class, you CANNOT write java without classes. C++ you can, but in java you cant.
It takes a big man to prove someone is wrong, but an even bigger man to admit he is wrong.
>>
>>949428
Yeah, that "class" is public static void main.

Literally one line of boilerplate.

It's you that's being ridiculous, implying that a person can only ever know one language, that a person who uses C will never, ever, ever use C++, that one cannot use OO techniques in C (for instance by embedding function pointers in structs*), or that one cannot learn memory management at any point after his first month picking up a compiler.

There are problems where OO is the best solution. This is orthogonal to what implementation language is the best solution. If you dogmatically avoid OO, your solution will not be as good as if you hadn't, whether it's in C, Java, C++ or Befunge.

It is easy to go from Java to C, and it is easy to go from Java to C++. In both cases you will end up writing better code, because the Java compiler's static analysis will have taught you not to write cowboy code that doesn't initialise variables, reads past the end of arrays, etc. You'll have no problem with pointers, because they're just object references you can add to if you're careful. You'll have no problem with malloc, because it's just the object lifecycle, except you do it yourself.

You'll have no problem picking up other popular languages like JS, C# or Objective C, because again, they're fundamentally the same, each with only a few new things to learn.

Oh, and Java is never used in embedded? Android.

* look at Linux, ffs.
>>
>>949488
>Java compiler's static analysis will have taught you not to write cowboy code that doesn't initialise variables, reads past the end of arrays
no, java will teach you that you dont have to worry about that stuff, making java programmers come blundering into a C environment like an ogre on crack.

linux is made with C you fucking cunt
Thread replies: 21
Thread images: 2

banner
banner
[Boards: 3 / a / aco / adv / an / asp / b / biz / c / cgl / ck / cm / co / d / diy / e / fa / fit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mu / n / news / o / out / p / po / pol / qa / r / r9k / s / s4s / sci / soc / sp / t / tg / toy / trash / trv / tv / u / v / vg / vp / vr / w / wg / wsg / wsr / x / y] [Home]

All trademarks and copyrights on this page are owned by their respective parties. Images uploaded are the responsibility of the Poster. Comments are owned by the Poster.
If a post contains personal/copyrighted/illegal content you can contact me at [email protected] with that post and thread number and it will be removed as soon as possible.
DMCA Content Takedown via dmca.com
All images are hosted on imgur.com, send takedown notices to them.
This is a 4chan archive - all of the content originated from them. If you need IP information for a Poster - you need to contact them. This website shows only archived content.