*Beginner* Help for interfacing from a C Program.

Hello,

I am a beginner at Adruino programming with prior knowledge on C procedural programming and I am hoping you could clarify some things for me.

Project:

  • Input text;
  • Convert into Morse Code;
  • Display output by turning an LED on and off for specific time spans;

What I have done so far:

I have learnt how to turn on and off an LED on my Adruino and implemented it successfully using the following code:

void setup() {
// initialize digital pin LED_BUILTIN as an output.
pinMode(LED_BUILTIN, OUTPUT);
}

void loop() {
digitalWrite(LED_BUILTIN, HIGH); // turn the LED on (HIGH is the voltage level)
delay(1000); // wait for a second
digitalWrite(LED_BUILTIN, LOW); // turn the LED off by making the voltage LOW
delay(1000); // wait for a second
}

I have a .c source file for a console application/executable that successfully converts a an input string of a specific format into morse code and displays it:

(Runs successfully on Devc++ IDE and Visual Studio)

#include <stdio.h>
#include <stdlib.h>

char* convertString(char*);

char inputString(FILE fp, size_t size){
//The size is extended by the input with the value of the provisional (using memory re allocation functions)
char *str;
int ch;
size_t len = 0;
str = realloc(NULL, sizeof(char)size);//size is start size
if(!str)return str;
while(EOF!=(ch=fgetc(fp)) && ch != '\n'){
str[len++]=ch;
if(len==size){
str = realloc(str, sizeof(char)
(size+=16));
if(!str)return str;
}
}
str[len++]='\0';

return realloc(str, sizeof(char)*len);
}

int main(void){
// Main function to create basic UI display, request input and do function calls
char *m;

printf("###############Morse Code Encryption####################\n\n\n\n");
printf("Input a string (UPPERCASEANDWITHNOSPACING): ");
m = inputString(stdin, 10);
//printf("%s\n", m); // test

convertString(m);

printf("\n\nThank you for using PSprograms inc.\n");
printf("We will bill you the total sum of 667 GBP in the next 3 working days.\n");
printf("Take care!");

free(m); //memory deallocation
getch();
return 0;
}

char* convertString(char *m)
{

const char * arrayalphabet[26] = {"A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"};
const char * arraymorse[26] = {".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--.."};
const char * output[900]; //think on size and what happens when SPACE

int i;
int z;
int len = strlen( m );
char *temp;

printf("\nEncrypted results:\n\n");

for (i = 0; i < len; i++)
{
temp = m*;*

  • printf("%c = ", temp); // test*
  • for (z = 0; z < 26; z++)*
  • {*
    _ if (temp == *arrayalphabet[z])_
  • {*
  • printf("%s\n", arraymorse[z]);*
  • } *
  • } *
  • }*

}
My question is then: How do I bridge the gap and interface with the Adruino from my C program.
I do realize i might be looking at this whole thing from a wrong perspective so please do set me on the right track.

How do I bridge the gap and interface with the Adruino from my C program.

The same way that the IDE and Serial Monitor application talk to the Arduino - using a serial port.

Please use [ code ] tags when posting code. The forum software has mis-interpreted some of your code as italics. You can edit your original post.

OK, so you know C and you know there must be a main() function. Arduino has a hidden main() function, which looks like this:

void main() {
  setup();
  for(;;) loop();
}

There's actually a little more but, that's the real core of it.

So I think one way to get started is, for the code which you eventually plan to put on the Arduino, copy that main() function and then write setup() and loop(). If you can get that working on your PC then you can transfer it to the Arduino easily.

To translate the C into something that will run on the Arduino, the main thing to change is the input and output. Output will use Serial.print() instead of printf(). This is a simpler function because it doesn't have that awkward format string. Use separate statements to print the text as strings, then print the values, then print the newline (or whatever.)

Input is a little more complex. You usually end up writing custom input routines every time, instead of using standard functions like inputString(). There's some available in the Arduino framework, like Serial.readFloat() but they have limited functionality.