hi everyone,
I am trying to program a simple Arduino console that can get commands using the serial monitor. I based my code on Robin2 ‘serial input basic’ (post B),
I used the Robin2 code to get the command from the user as a char array. then I create some keywords stored as constant with values 0-5 for using them as a variable in my switch loop. I hope it’s clear
the main problem is that: my commands are not getting the values that I set on my keywords constant.
it’s not surprising because I try to convert char array to single int. I used that codes as reference for doing that:
and that’s my code, thanks for the help
///run the serial monitor in a ‘new line’ mode
#define TIME 0
#define RFCK 1
#define TEST 2
#define KEYP 3
#define INFO 4
#define MUTE 5
int data;
const byte numChars = 32;
char receivedChars[numChars]; // an array to store the received data
boolean newData = false;
void setup() {
Serial.begin(9600);
Serial.println("");
}
void loop() {
recvWithEndMarker();
runCommand();
}
void runCommand() {
if (newData == true) {
Serial.println(receivedChars);
int command = atoi(receivedChars);
Serial.println(command);
switch (command)
{
case 0:
{
Serial.print("CMD1 ON ");
break;
}
case 1:
{
Serial.print(“CMD2 ON”);
break;
}
case 2:
{
Serial.print(“CMD3 ON”);
break;
}
case 3:
{
Serial.print(“CMD4 ON”);
break;
}
case 4:
{
Serial.print(“CMD5 ON”);
break;
}
case 5:
{
Serial.print(“CMD6 ON”);
break;
}
}
newData = false;
}
}
void recvWithEndMarker() {
static byte ndx = 0;
char endMarker = ‘\n’;
char rc;
// if (Serial.available() > 0) {
while (Serial.available() > 0 && newData == false) {
rc = Serial.read();
if (rc != endMarker) {
receivedChars[ndx] = rc;
ndx++;
if (ndx >= numChars) {
ndx = numChars - 1;
}
}
else {
receivedChars[ndx] = ‘\0’; // terminate the string
ndx = 0;
newData = true;
}
}
}
void showNewData() {
if (newData == true) {
Serial.print("This just in … ");
Serial.println((char*)receivedChars);
newData = false;
}
}