-->
Page 1 of 2

Trouble getting a simple sketch working in Arudino IDE

PostPosted: Thu Mar 07, 2019 5:12 pm
by SupDoc
I have a C-program that runs in Dev-C++:
// C program to demonstrate snprintf()
#include <stdio.h>

int main()
{
char buffer[50];
char* s = "geeksforgeeks";

// Counting the character and storing
// in buffer using snprintf
int j = snprintf(buffer, 14, "%s\n", s);

// Print the string stored in buffer and
// character count
printf("string:\n%s\ncharacter count = %d\n", buffer, j);

return 0;
}

Output is:
string
geeksforgeeks

Character count = 14

I was told by a friend that a C-program requires a setup() and a loop() before it can run as a Sketch in arduino. I added those to the program and moved things around, added the Serial.begin & connected it to a working board & ran it :

#include <stdio.h>

char buffer[50];
char* s = "geeksforgeeks";

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

int main()
{
// Counting the character and storing
// in buffer using snprintf
int j = snprintf(buffer, 14, "%s\n", s);

// Print the string stored in buffer and
// character count
printf("string:\n%s\ncharacter count = %d\n", buffer, j);

return 0;
}

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

}
It compiled and uploaded successfully, but I got no Serial monitor response. Any ideas why?

Re: Trouble getting a simple sketch working in Arudino IDE

PostPosted: Thu Mar 07, 2019 7:39 pm
by rex.vk3pk@gmail.com
Hi,
I am new to this Forum, although I have 45 years of micro controller experience.
I find un-indented code almost impossible to read/understand. I am sure there is some sort of code tags which will indent the code.

On the surface the code looks OK, especially if it compiles OK.

You do not mention whether you have downloaded the sketch or not and to which controller. You do that with the RIGHT ARROW in the tool bar.
Then of course you have to open the "terminal" in the Tools menu and make sure that you set it at the correct baudrate.

Re: Trouble getting a simple sketch working in Arudino IDE

PostPosted: Fri Mar 08, 2019 4:16 am
by btidey
In the Arduino environment it is all about setup and loop. Your main is just a function and won't get called.

The setup function is called once as the first piece of application that runs. When that completes then loop is called effectively inside a do forever. So it gets repeatedly executed.

So the intention is that initialisation of hardware and software environment is done within setup, and then the main code runs inside the loop checking for things to do and responding accordingly.

As your main was just doing its thing once, then it could get called from within setup and the loop would be doing nothing.

Re: Trouble getting a simple sketch working in Arudino IDE

PostPosted: Fri Mar 08, 2019 11:35 am
by SupDoc
I am a real bonehead - So how do I modify this code in Arduno to make it work?