Jump to content

Arduinos, signal transmission and Morse codes


Delay

Recommended Posts

Well, I didn't come up with that switch-statement, my unimaginably limited knowledge of C syntax (as I make a good job of showing that) didn't include this.
I will happily take the credit, though, so thanks I guess?

 

@Green Baron, so if I understood correctly, my cases in the switch shouldn't work, since "case 'dot': " is not one single character. So I guess I'll have to use ifs?

"if(signal == 'dot')", maybe?

 

As for memory and storing things correctly - I don't want to pick up bad habits after all and fill up memory, though that would be quite fun - , you'll have to start at the very beginning. I have no idea what pointers are; heck, I don't even know how a computer stores things at all.
Remember that I've never been given any clear explanation of how C works, much less how a computer does. What I've shown in this thread is based on the assumption that some things in C are similar to Python in some way, hence "Text = Text + Something", which I now know is nonsense. And despite this, my own code is the minority of what I present. Most of this is coming from this very forum, modified slightly to fit into the project.

Edited by Delay
Link to comment
Share on other sites

2 hours ago, Delay said:

 

@Green Baron, so if I understood correctly, my cases in the switch shouldn't work, since "case 'dot': " is not one single character. So I guess I'll have to use ifs?

"if(signal == 'dot')", maybe?

 

 

The single quotation is for single chars only. A char is nothing but an integer data type, a byte in 2s complement, with a range of -128 to 127.

The double quotation marks are for the initialization of char arrays only, or for constant char arrays (waiting here for correction because i have done only c++ lately and may have forgotten something about the c fundamentals).

You can compare single chars just like other basic data types because they are int values. <, >, ==, != operators are the same as those for short, int, long int or long long int.

But these operators do not work for arrays of chars in C. For these, you need routines that start at the first position (which is a char) and walk all the way along the array, take each single char (via either a pointer or the notation array[position]) and compare it with the char at the same position of the other array.

So, if signal is a variable 3 char array, and "dot" (watch double quotations) is the constant array you want to compare it with, you set out on a loop and compare signal[0] with 'd', signal[1] with 'o', .... and so on. This is what the strstr() or strcmp() functions do (*). They work with pointers because pointers are extremely efficient to handle (pointer arithmetic), and once you've understood them you will do everything with pointers. You will use a pointer to go the toilet !

Understanding arrays in C and pointers is the basis, and probably the biggest step in programming you'll ever do. Apart from multithreading maybe, and template programming in C++.

:-)

(*) They might use much more efficient methods, like logic operations on the two memory regions, but that doesn't interest us here.

Edited by Green Baron
Link to comment
Share on other sites

My head hurts.
What does C have against using strings? No, I have to compare every single member with every single other member? Individually?

I don't see how that is supposed to make sense. In any context. When would you not want to be able to compare strings directly?

 

What if, instead of using dot, dash, etc,  I simply use ints 0, 1, 2, 3 and explain what they represent in comments next to it?
That's a lot faster, both for me and the program (though for the latter it's pretty much a meaningless change).

Edited by Delay
Link to comment
Share on other sites

10 minutes ago, Delay said:

My head hurts.
What does C have against using strings? No, I have to compare every single member with every single other member? Individually?

I tried to explain how stuff works because i find it important to look behind the scenes :-) In the end, you use strcmp() to compare two strings. That does the heavy lifting for you.

Quote

I don't see how that is supposed to make sense. In any context. When would you not want to be able to compare strings directly?

C does not know strings as a data type. Only int or float. Chars are a special form of int, with the length of 1 byte. The header <string.h> contains functions to work on such arrays. That isn't really a disadvantage, it is efficient.

Quote

What if, instead of using dot, dash, etc,  I simply use ints 0, 1, 2, 3 and explain what they represent in comments next to it?
That's a lot faster, both for me and the program (though for the latter it's pretty much a meaningless change).

In terms of speed it would not really matter. A computers heartbeat is billions/second and it uses pipelining, vectoring and parallelism. Whether you sort a 1 million elements long c array in a thousandth of a second or a hundredth isn't that important when waiting for user input. After all, chars are ints.

:-)

Edited by Green Baron
Link to comment
Share on other sites

4 minutes ago, Delay said:

When would you not want to be able to compare strings directly?

There's one of the reasons I've turned to Python more and more.

 

5 minutes ago, Delay said:

What if, instead of using dot, dash, etc,  I simply use ints 0, 1, 2, 3 and explain what they represent in comments next to it?

You could use chars.  Literally use '.', '-', ' ', and '_' or '/'.

Link to comment
Share on other sites

8 minutes ago, razark said:

There's one of the reasons I've turned to Python more and more.

And Python is written in C :-)

 

No problem, everything has its advantages and disadvantages ;-)
 

Edited by Green Baron
Link to comment
Share on other sites

10 minutes ago, razark said:

There's one of the reasons I've turned to Python more and more.

Haha, Arduinos don't work with Python. Believe me; if they did I this 4-page thread wouldn't exist at all and I'd already be done. Nothing to see here; move on to more productive things.

Instead I'm forced to torment myself, tasked with the impossible of "learn the basics behind how a computer works, how to allocate memory and overcome problems that shouldn't even be problems in 2 days!".

I literally only have this weekend until the next CS lesson on Monday, and I don't want to waste that.

Edited by Delay
Typo. Look, it's 12:30 AM. I'm tired.
Link to comment
Share on other sites

23 minutes ago, Delay said:

Arduinos don't work with Python.

I've never worked with Arduinos before.  I've been thinking of picking one up, though.  I like the Pi, because I can use python on it.

 

23 minutes ago, Delay said:

tasked with the impossible of "learn the basics behind how a computer works, how to allocate memory and overcome problems that shouldn't even be problems in 2 days!".

Just remember, you could be working in assembly.

(Or COBOL.  I hated that class.  Loved the professor, though.)

Edited by razark
Link to comment
Share on other sites

2 minutes ago, razark said:

Just remember, you could be working in assembly.

that doesn't make things better, it just shows it could be worse.

The only exposure I had to Assembly as of now is - strangely enough - arbitrary code execution on the Super Nintendo, or rather explanations of why and how they work. Didn't understand a thing. (But it was still informative and interesting)

Link to comment
Share on other sites

It seems to me that the main problem here was a didactic one. Being thrown into char arrays and pointers before clearing the syntax and some basic stuff to build on causes frustration. A good book and some private studies can help out i think :-)

Edited by Green Baron
Link to comment
Share on other sites

@Green Baron 2 problems with getting a book.

1. It's 10:22 PM. No store is opened anymore.
2. Tomorrow is Sunday, when all stores are closed for the entire day.

The next time stores are open is Monday, and that's when I have the next CS class.
Usually it's Tuesday, but this week it got shifted to Monday.

(Now's the perfect time to play a soundclip of a crying baby)

Since yesterday I learned pretty much nothing other than what it feels like to be really frustrated because the complexity of a topic just shot up to infinity. It all seems like Monday's class will be an utter waste with 67 1/2 minutes of nothing productive happening.
Am I just dumb or is programming in C really as difficult as I think it is?

Edited by Delay
PM, not AM. Makes sense now.
Link to comment
Share on other sites

1 hour ago, Delay said:

@Green Baron 2 problems with getting a book.

1. It's 10:22 PM. No store is opened anymore.
2. Tomorrow is Sunday, when all stores are closed for the entire day.

The next time stores are open is Monday, and that's when I have the next CS class.
Usually it's Tuesday, but this week it got shifted to Monday.

(Now's the perfect time to play a soundclip of a crying baby)

Since yesterday I learned pretty much nothing other than what it feels like to be really frustrated because the complexity of a topic just shot up to infinity. It all seems like Monday's class will be an utter waste with 67 1/2 minutes of nothing productive happening.
Am I just dumb or is programming in C really as difficult as I think it is?

Hi !

No problem, you can buy it online and directly download the pdf version and start right away:

https://www.rheinwerk-verlag.de/c-von-a-bis-z_2132/

In contrary to my hint above it is C99, not C11. But that does not really matter right now. What is missing is high level stuff, oop things like function overloading for example. When you're that far that you think about methods in structs and function overloading you're probably a C++ candidate anyway :-).

They also have an Arduino book

https://www.rheinwerk-verlag.de/arduino_3797/

but i do not know it. It probably covers the library that comes with Arduino, like the functions to read interfaces and switch on and off lights etc. They are not basic C. Maybe somewhat superficial ....

Looks like the next nights are planned, eh ? But C basics until Monday is sporty.

 

Edit: and if you have questions concerning C we will help, i am sure. If your questions concern Arduino specific functions calls and things it may be a good idea to provide us with the function declarations.

 

Edited by Green Baron
Link to comment
Share on other sites

I love my teacher so much.

I open up the file and modify it slightly, according to the results here. Lo and behold, I get bombarded with errors nonetheless.

Now; As a teacher, seeing one of the few students who genuinely try to work, do you:

A) Help the student?
B) Ignore the student?
C) Rather talk and listen to someone who clearly doesn't have any problems with the task and is instead merely boasting about how they already managed to use DirectX to display a cube?

As a responsible teacher he of course knew what answer was the correct one. C, of course.

Don't get me wrong: Good for the other student. It's impressive that he's already able to use a 3D engine to display something, and that's not even sarcastic. I'm genuinely congratulating him here.
However, of course I do start wondering why he even is in the class if our Einstein already knows everything anyone ever needs to know about computing, or at the very least why he doesn't help anyone. Other than his friends, of course. Those are privileged.

 

In the end the teacher actually did manage to fix my error. Not by explaining why it works now and not with my original concept.
The response to my question of "Shouldn't this (his solution) leak?", with my - and keep this in mind - very limited knowledge and my tendency to severely overextrapolate, was - how could it be anything else - technobabble no one outside of him and Einstein over here could ever understand.

Edited by Delay
Link to comment
Share on other sites

Your Einstein might just have typed down one of the numerous examples from the web, like i did some time ago with OpenGL and Vulkan. Don't be distracted by that.

If your teacher isn't helpful there is still the own initiative to learn independently. There is always the freedom to decide "Now i'll do it better !" and dig into it. It might take some time, effort and nerves. Would be the first time if not ;-)

Your English is far better than mine !

Edited by Green Baron
Link to comment
Share on other sites

41 minutes ago, Green Baron said:

Your Einstein might just have typed down one of the numerous examples from the web

I doubt that. It really seems like he knows what he's talking about.

In fact, I don't want what you said to be true. I want him to have come up with it himself.

Edited by Delay
Link to comment
Share on other sites

@Green BaronI now have the book. It is the variant on C99, not C11, but aside from texts being written like {" "} in the book, there doesn't seem to be any absolutely massive difference between the two that would make the book utterly useless. Even after looking it up it seems like the changes are smaller ones and not absolute game changers.

Skimmed through the Array chapter, most importantly string handling. There were some things in there that I didn't think of before, like simply testing until "\0" occurs in for loops.

And I think I now (sort of) understand why strcat() doesn't work when adding an indexed character to a char array. (I'll pretend they are strings for simplicity).
What I think is happening is that the character resulting from an index is not a string, but instead just one character. Since strcat() combines two strings, not strings and chars, I'd get an error for attempting so.

Edited by Delay
Link to comment
Share on other sites

You are right, nothing will break. In all the times of C/C++ there apparently never was something that broke anything that was before.

Work your way through it. There are lot of concepts you need to understand, data types, operators, conversions, control structures, functions, .... That will be your solid foundation and you will always need it. Have fun :-)

Link to comment
Share on other sites

I asked my teacher if the solution of his would leak. As already mentioned, the response was unintelligible. So why not ask here?

char Text_to_add_to[] = "";				//ignore the fact that this line would very quickly run into unallocated areas in memory.
char Characters[] = "some arrangement of characters";
int index = 0;

// inside the switch which uses the line in question.

  
	case 2:
		char temp[] = " ";			//couldn't I change this to "char temp[2] = " "? One byte for the space and one byte for \0.
		temp[0] = Characters[index];
		strcat(Text_to_add_to, temp);
		break;

"temp[0] = ..." is the line in question.
This, upon a space (case 2) being entered (and thus a character needs to be added), constantly changes to supply the correct character for strcat() to add. Apparently this does work just fine; no errors while compiling.
However, I might just be overapplying the "pointer to char array gets lost -> memory leak"-rule. I always tend to assume the worst possible case.

That's why I want to ask whether or not this would/wouldn't leak and why. Though it probably wouldn't matter that much anyways, it's one byte at a time.

Edited by Delay
Link to comment
Share on other sites

There are missing semicolons, sure it compiles ?

Usually a leak describes the situation that an allocated space is not freed correctly. Since there are no visible allocations (and btw. no pointers) i can't see any leaks. But of course you must allocate space for text_to add_to before using it in strcat().

So, no, i don't see leaks in these lines, but it is undefined as long as you don't allocate space. I am sorry but i cannot say any more before seeing the whole program ...

Snippet to deal with the allocation thing (Level 0 in terms of gaming ;-), it is quite a time since i last did this):

 

#include <string.h>
#include <stdlib.h>

#define MAX_SIZE 100

int main() {
	char *texttoadd = (char *)malloc( MAX_SIZE );
	
	/*	Do things, but remember not to exceed 99 chars + \0. Depending on the functions you use
  		\0 may be added by them or you must do so by hand.
  		-> man 3 strcat() didplays the help, if you're in a terminal ... */
	
	free( texttoadd );
}

 

Link to comment
Share on other sites

I have to guess, since you haven't told us what the program shall do.

You want to create a new string by concatenating two strings, correct ? You don't need a temp variable for this, you need the source string, the dest string and a pointer var for the return value of the function. Work through the first chapters of the book you bought, then you will catch up with "Einstein" :-)

Or do you want to append a single char to a string ? These are different things ...

 

Edit: i have spent more time i wanted to prepare a few examples of how things work. If somebody finds an error, pls tell me, it is quick and dirty, and of course there are more than one ways. There are two kinds of mem handling you can use by switching remarks.


#include <string.h>	// strcpy(), strcat()
#include <stdlib.h> // malloc(), free(), EXIT_SUCCESS
#include <stdio.h>  // puts(), stdout

#define MAX_SIZE 100

int main() {
	// Version 1: dynamic allocation
	// char *texttoadd = (char *)malloc( MAX_SIZE );
	// Version 2: static allocation
	char texttoadd[MAX_SIZE];

	// concatenate two strings:
	const char Characters[] = "some arrangement of charactersX";
	// copy const string into working string, including trailing \0
	// 'Characters' decays implicitly into a pointer to its first element: &Characters[0]
	char *retval0 = strcpy( texttoadd, Characters );
	puts( retval0 );

	// concat the string from position 10 to the end to the existing string, append \0
	char *retval1 = strcat( retval0, &Characters[10] );
	puts( retval1 );

	// append a single char at the end of a string:
	const char c = 'P';
	char *retval2 = strncat( retval0, &c, 1 );
	puts( retval2 );
	// or, if you are absolutely sure that there is enough space in the string:
	size_t l = strlen( texttoadd );
	texttoadd[l] = c;		// simply assign the position at the end of the string
	texttoadd[l+1] = '\0';	// do not forget !
	puts( texttoadd );
	// equivalent:
	puts( retval2 );

	// Version 1 cleanup: free dynamically allocated memory
	// free( texttoadd );
	puts( "\n\nEnding normally\n" );
	// Version 2 will be cleaned up when leaving the scope
	return EXIT_SUCCESS;
}

 

Edited by Green Baron
Link to comment
Share on other sites

This thread is quite old. Please consider starting a new thread rather than reviving this one.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...