variables at the beginning of my programs for setting information

I am trying to use variables at the beginning of my programs for setting information that can be viewed from the Serial Monitor. i would like to be able to enter information and have it show up when the sketch first runs. i am having problems doing this. the program name does not show up. Also is float the correct way to enter a program version number. i would like to be able to enter a version number like 1.0.1 but i am getting a to many decimals points in number fault. Does anyone have any suggestions.

SKETCH:

char ProgramName = "Blinker"; float versionNumber = 1.0;

int TimeOn(500); // milliseconds int TimeOff(1500); // milliseconds

void setup() { Serial.begin(9600); // turn on Serial Port

Serial.print("Program: "); Serial.println(ProgramName); Serial.print("Version: "); Serial.println(versionNumber);

pinMode(13, OUTPUT);

} void loop() {

// Blink code digitalWrite(13, HIGH); delay(TimeOn); digitalWrite(13, LOW); delay(TimeOff); }

A char uses up 1 byte. I don't think you can put "Blinker" in a char. Maybe you could put it in a character array and print it by iterating over the index?

 char ProgramName [] = "Blinker";

Jimmy

i would like to be able to enter a version number like 1.0.1

Think about it. Is 1.0.1 a floating point number ? Try

char * ProgramName = "Blinker";
char * versionNumber = "1.0.1";

mixographer: A char uses up 1 byte. I don't think you can put "Blinker" in a char. Maybe you could put it in a character array and print it by iterating over the index?

char ProgramName [] = "Blinker";

Jimmy

No need to use an index and iterate over the array. This is fine:-

char ProgramName[] = "Blinker";
Serial.println(ProgramName);

In the posted code, the only thing missing is [] when declaring 'ProgramName'. Edit: Oh, and the lack of [code]code tags[/code]. :(

I don't think this does what you intended:

int TimeOn(500);    // milliseconds
int TimeOff(1500);  // milliseconds

Use

int TimeOn = 500;    // milliseconds
int TimeOff = 1500;  // milliseconds

Better yet

const int TimeOn = 500;    // milliseconds
const int TimeOff = 1500;  // milliseconds

olf2012:
I don’t think this does what you intended:

Ha, I missed that. I did run his code earlier, however, and it does do what he intended - 500ms ‘on’ and 1500ms ‘off’.
Did you test it?

This is why it works:-

N.B. It’s known as “constructor initialisation”, and is perfectly valid.

See here:- Variables and types
(About 3/4 of the way down the page.)

Never too old to learn. :smiley:

cplusplus.com:
All three ways of initializing variables are valid and equivalent in C++.

#include <iostream>

using namespace std;

int main ()
{
 int a=5;               // initial value: 5
 int b(3);              // initial value: 3
 int c{2};              // initial value: 2
 int result;            // initial value undetermined

a = a + b;
 result = a - c;
 cout << result;

return 0;
}

am having problems doing this. the program name does not show up

char ProgramName = "Blinker";

A sketch which does not compile cannot run, so it isn’t surprising that the program name doesn’t show.

Remember to use code tags when posting code - here, I’ve got a spare pair you can have [code][/code]

Thank you everyone. i am still new to coding and i am not sure how to use the code tags. is this correct?

char ProgramName[] = "Blinker";   
char versionNumber[] = 1.0;

Thank You David

Your use of tags is correct, but the compiler has already told you that part of the code you posted is incorrect.

dsmith9583: i am still new to coding and i am not sure how to use the code tags. is this correct?

char versionNumber[] = 1.0;

That one will probably not do what you want. Use this:

char versionNumber[] = "1.0";

For 'variables' you don't intend to change it helps the compiler if you tell it not to let you accidentally change the value:

const char ProgramName[] = "Blinker";   
const char versionNumber[] = "1.0";

That one will probably not do what you want.

It won’t do anything at all, because it doesn’t compile.

I worked. You guys are great. i appreciate all the help

David

dsmith9583: I worked. You guys are great. i appreciate all the help

David

Learn from getting it to work. Do you understand why it did not work originally but does now ?

dsmith9583:
is this correct?

char versionNumber[] = 1.0;

AWOL:
Your use of tags is correct, but the compiler has already told you that part of the code you posted is incorrect.

Unfortunately what the compiler says is:

sketch_aug10a:2: error: initializer fails to determine size of 'versionNumber'
 char versionNumber[] = 1.0;
                        ^
sketch_aug10a:2: error: array must be initialized with a brace-enclosed initializer

That implies that the solution is:

 char versionNumber[] = {1.0};

which compiles with a warning (if warnings are turned up):

/Users/john/Documents/Arduino/sketch_aug10a/sketch_aug10a.ino:2:28: warning: narrowing conversion of '1.0e+0' from 'double' to 'char' inside { } [-Wnarrowing]
 char versionNumber[] = {1.0};
                            ^

Still won’t produce the desired results. We can’t always rely on the compiler giving correct instructions for fixing a programming error that causes bad syntax.

Regarding the version "number"

If you aren't doing arithmetic on it, it's text, not a number. It doesn't matter if the individual characters are all numerals.

You're going to have to convert it to a string anyway to display it, so just store it as one to begin with and cut out the useless heavyweight float stuff.

UKHeliBob

UKHeliBob: Learn from getting it to work. Do you understand why it did not work originally but does now ?

Thank you for asking. i do understand most of it but i am still having problems understanding what the [] function is.

const char ProgramName[] = "Blinker";   
const char versionNumber[] = "1.0";

Thank You David

dsmith9583: i am still having problems understanding what the [] function is.

It's not a function, it tells the compiler, that you want to create an array. See https://www.arduino.cc/en/Reference/Array

Bearonic: It's not a function, it tells the compiler, that you want to create an array. See https://www.arduino.cc/en/Reference/Array

Thank You