Im trying to define one letter as another

Hi I'm new to Arduino and was wondering how i defined one character as another
Lets say I had an "e" and wanted a program to change that to an "I". How would i do that?

Or consider

using a conditional filter something like

// if (rc = 'e') {  // Aieee! this mistakenly assigns `e` to `rc ` inststead if the intended equality test
if (rc == 'e') {    //  This is the intended equality test (Thanks @alto777)
{   rc = 'I';
}

You could use a pointer, perhaps.

constexpr char e = 'e';
constexpr char I = 'I';

const char *ptr = e;

if (condition)
    *ptr = I;

Fixed that for ya'.

a7

1 Like

Needs work.

At the very least, you need to take the address...

# include <stdio.h>

char e = 'e';
char I = 'I';

int main()
{
    printf("Hello World\n");
    
    char *ptr = &e;

    if (true)
        *ptr = I;
        
    printf("%d (%c)", e, e);

    return 0;
}

I had to keep losing const stuff I don't use and don't understand. To get it to compile and give the desired reslut:

Hello World
73 (I)

...Program finished with exit code 0
Press ENTER to exit console.

a7

Hello nerdytaco

You have to design your own code book for the transformation.

constexpr char codeBook[] {'e','z' ....... and so on 

Do you will design your own Enigma ?

One method:

char char_out;

void encode(char char_in) {
  switch(char_in) {
    case 'a':
    char_out = 'b';
    break;

    case 'b':
    char_out = 'c';
    break;

    // . . . and so on

    default:
    // use a default value, raise an error message, whatever else you'd like
    break;
  }
}

Another way:

char('a' + 1) // = b
1 Like
char('a'+ (char_in - 'a' +1)%26)) 

as in

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

  for (char char_in = 'a'; char_in <= 'z' ; char_in++) {
    Serial.print(char_in);
    Serial.println(char('a' + (char_in - 'a' + 1) % 26));
  }
}

void loop() {
}

Is the compiler smart enough to use optimize that modulo operation into an if?

I like the completeness of your answer.

1 Like

It doesn't look like it's smart enough:

// Excerpt from Wokwi F1/View Compiled Assembly Code Listing:
...
  Serial.begin(115200);

  for (char char_in = 'a'; char_in <= 'z' ; char_in++) {
    Serial.print(char_in);
    Serial.println(char('a' + (char_in - 'a' + 1) % 26));
 510:	0a e1       	ldi	r16, 0x1A	; 26
 512:	10 e0       	ldi	r17, 0x00	; 0
  return write(str);
}

size_t Print::print(char c)
{
  return write(c);
 514:	6c 2f       	mov	r22, r28
 516:	60 5a       	subi	r22, 0xA0	; 160
 518:	86 e1       	ldi	r24, 0x16	; 22
 51a:	91 e0       	ldi	r25, 0x01	; 1
 51c:	0e 94 ff 00 	call	0x1fe	; 0x1fe <_ZN14HardwareSerial5writeEh>
 520:	ce 01       	movw	r24, r28
 522:	b8 01       	movw	r22, r16
 524:	0e 94 de 02 	call	0x5bc	; 0x5bc <__divmodhi4>
 528:	61 e6       	ldi	r22, 0x61	; 97
 52a:	68 0f       	add	r22, r24
 52c:	86 e1       	ldi	r24, 0x16	; 22
 52e:	91 e0       	ldi	r25, 0x01	; 1
 530:	0e 94 ff 00 	call	0x1fe	; 0x1fe <_ZN14HardwareSerial5writeEh>
    void clearWriteError() { setWriteError(0); }
...
1 Like

When possible I avoid the use of division and modulo operations on most of these, unless mission critical, but you are certainly correct in bounding your operation.

char_in += 1;
if (char_in > 'z') char_in -= 26;
1 Like

Well me and my friend are trying to make a morse code translator with one arduino sending morse code and the other receiving it in "." and "-" and that needs to be translated to letters

Standard Morse supports punctuation natively. "." is ".-.-.-"

Add them to your look up table.

Next time you post a question, please reveal up front what you're up to.

I wrote Magic Morse many years ago, take anything you can use:
Magic Morse on Arduino | Trybotics

Some background:
Notes behind Magic Morse | PICAXE Forum

Magic Morse is an algorithm, thus once the Magic number is calculated (or known) one can easily derive Morse or Character.

Code:
(c) Copyright 2011 M. Ray Burnette
ALL COMMERCIAL RIGHTS RESERVED

Example: 0 1 0 = R
               There are 3 elements, therefore bits 1, 2, 4 = 3 (1 / 1 / 0)
               The 1 is in the second character, therefore it is 
               weighted with a bit value of 16 ( 0 / 1/ 0 / 0 / 0 )
               The Index value is 1 + 2 + 16 = 19

Example:  1 1 = M
               There are 2 elements, therefore bits 1, 2, 4 = 2 (0 / 1 / 0)
               The 1 is the first and second character, therefore
               weighting is 8 and 16 = 24 (1 / 1 / x / x / x)
               The Index value is 2 + 24 = 26

Alphabet
	ASCII INDEX	MORSE CODE						# Elements-		---------- weight ---------
										1	2	4	8	16	32	64	128
E	69	1	DIT							1	0	0	0	x	x	x	x
I	73	2	DIT	DIT						0	1	0	0	0	x	x	x
S	83	3	DIT	DIT	DIT					1	1	0	0	0	0	x	x
H	72	4	DIT	DIT	DIT	DIT				0	0	1	0	0	0	0	x
5	53	5	DIT	DIT	DIT	DIT	DIT			1	0	1	0	0	0	0	0
T	84	9	DAH							1	0	0	1	x	x	x	x
N	78	10	DAH	DIT						0	1	0	1	0	x	x	x
D	68	11	DAH	DIT	DIT					1	1	0	1	0	0	x	x
B	66	12	DAH	DIT	DIT	DIT				0	0	1	1	0	0	0	x
6	54	13	DAH	DIT	DIT	DIT	DIT			1	0	1	1	0	0	0	0
A	65	18	DIT	DAH						0	1	0	0	1	x	x	x
R	82	19	DIT	DAH	DIT					1	1	0	0	1	0	x	x
L	76	20	DIT	DAH	DIT	DIT				0	0	1	0	1	0	0	x
M	77	26	DAH	DAH						0	1	0	1	1	x	x	x
G	71	27	DAH	DAH	DIT					1	1	0	1	1	0	x	x
Z	90	28	DAH	DAH	DIT	DIT				0	0	1	1	1	0	0	x
7	55	29	DAH	DAH	DIT	DIT	DIT			1	0	1	1	1	0	0	0
U	85	35	DIT	DIT	DAH					1	1	0	0	0	1	x	x
F	70	36	DIT	DIT	DAH	DIT				0	0	1	0	0	1	0	x
IN	0	41	DAH	DIT	DAH					1	1	0	1	0	1	x	x   <--- Prosign for Invite
K	75	43	DAH	DIT	DAH					1	1	0	1	0	1	x	x
C	67	44	DAH	DIT	DAH	DIT				0	0	1	1	0	1	0	x
__________________________________________________________________________________
Full list of standard ASCII --> Magic Morse
Alpha	
	ASCII
		Magic
0	48	253
1	49	245
2	50	229
3	51	197
4	52	133
5	53	5
6	54	13
7	55	29
8	56	61
9	57	125
A	65	18
B	66	12
C	67	44
D	68	11
E	69	1
F	70	36
G	71	27
H	72	4
I	73	2
J	74	116
K	75	43
L	76	20
M	77	26
N	78	10
O	79	59
P	80	52
Q	81	92
R	82	19
S	83	3
T	84	9
U	85	35
V	86	68
W	87	51
X	88	76
Y	89	108
Z	90	28
1 Like

That's pretty cool.

(To make it compile I had to add some 'const's and move a PROGMEM or two.)

1 Like

Yea... Arduino was a bit different in 10+ years back :upside_down_face:

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.