serial input, control, split up commands and their values?

Hello all!
Im trying to learn but im too impatient to bother with hello world and progress and too ignorant to understand where and how to find what im looking for..
Instead i start with what interest me and try to figure out how to make it work.

But programming has never been my strong suit, i simply fail to grasp the finer things such as data handling and why there´s a specific symbol between two identical words in the code.
But thats life, you just cant be great at everything.. :slight_smile:

However, im trying to make my arduino doing two things, turn on/off lights and display time.
that means i need to input serial commands to do different things, not merely turn something on/off or setting a predefined variable.

How can i via arduino serial monitor (and later wifi) send a specific command and a value?

I want to set the time thats displayed on a 7segment 4digit display, i would want to send a "setTime1435" through arduino serial monitor to change the current time to 14:35.
nevermind that rest of the code thats required to make 14:35 turn to 14:36 and so forth, i just really want to understand how i can define a command that i can activate remotely, pour a value into it and then use that value, or a specific part of the value later.

Another example would be my remote wallsocket program running and working, that if one inputs two numbers in serial monitor, for example 11 for socket 1, on or 10 for turn off socket one.

Its cleverly configured to allow binary 24bit to directly output binary to transmitter or 2 char input to use an array for socket on/off, but i simply cannot understand why and how its working.

code snippet:
if (mySwitch.available()) {
Serial.print(mySwitch.getReceivedBitlength());
Serial.print(" bits ");
Serial.println(mySwitch.getReceivedValue(), BIN);
mySwitch.resetAvailable();
}
if (Serial.available() > 0) {
char input = Serial.read();
if ( input == 'B' ) {
Serial.readBytesUntil('\n', binary_data, sizeof(binary_data));
Serial.print("send B");
Serial.println( binary_data );
mySwitch.send( binary_data );
} else
if ( input >= 0x30 && input <= 0x39 ) {
input = input - 0x30; // ASCII to number
serial_data[serial_pos++] = input;
} else {
Serial.print("ignore: ");
Serial.println(input, HEX);
}

Just to clarify, the above "quote" is a snippet from a working code, i need to understand what it does, why its working and how i can do it myself, for example to set correct time on the arduino clock! :slight_smile:

And sorry for the messy and ramblish text, its half eleven in this part of the world, on a friday night no less!
So have a good night and thank you for your time and effort!

xarvox:
Hello all!
Im trying to learn but im too impatient to bother with hello world and progress and too ignorant to understand where and how to find what im looking for..
Instead i start with what interest me and try to figure out how to make it work.

Funny, I wanted to learn to swim but I'm too impatient to start in the shallow end and too stupid to take lessons. Instead I just jumped off a cruise ship in the middle of the ocean.

Thank God for the Coast Guard!

This link may help: Serial Input Basics

xarvox:
Hello all!
Im trying to learn

But programming has never been my strong suit, i simply fail to grasp the finer things such as data handling and why there´s a specific symbol between two identical words in the code.
But thats life, you just cant be great at everything.. :slight_smile:

These statements ar incompatible.

You say "too ignorant to understand where and how" but (even taking that at face value) you are a genius compared to the stupidity of a computer.

That is why successful computer programming demands attention to those "finer things"

If you really want to learn your first task is to develop the interest and the patience to get familiar with the finer things. Otherwise, just pay someone else to write the program.

Maybe have a look at Planning and Implementing a Program - though it was not written for a complete beginner.

...R

Below is serial servo code that controls servos and also prints to the serial port. May be similar to what you are trying to do.

//zoomkat 11-22-12 simple delimited ',' string parse 
//from serial port input (via serial monitor)
//and print result out serial port
//multi servos added 
// Powering a servo from the arduino usually *DOES NOT WORK*.

String readString;
#include <Servo.h> 
Servo myservoa, myservob, myservoc, myservod;  // create servo object to control a servo 

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

  //myservoa.writeMicroseconds(1500); //set initial servo position if desired

  myservoa.attach(6);  //the pin for the servoa control
  myservob.attach(7);  //the pin for the servob control
  myservoc.attach(8);  //the pin for the servoc control
  myservod.attach(9);  //the pin for the servod control 
  Serial.println("multi-servo-delimit-test-dual-input-11-22-12"); // so I can keep track of what is loaded
}

void loop() {

  //expect single strings like 700a, or 1500c, or 2000d,
  //or like 30c, or 90a, or 180d,
  //or combined like 30c,180b,70a,120d,

  if (Serial.available())  {
    char c = Serial.read();  //gets one byte from serial buffer
    if (c == ',') {
      if (readString.length() >1) {
        Serial.println(readString); //prints string to serial port out

        int n = readString.toInt();  //convert readString into a number

        // auto select appropriate value, copied from someone elses code.
        if(n >= 500)
        {
          Serial.print("writing Microseconds: ");
          Serial.println(n);
          if(readString.indexOf('a') >0) myservoa.writeMicroseconds(n);
          if(readString.indexOf('b') >0) myservob.writeMicroseconds(n);
          if(readString.indexOf('c') >0) myservoc.writeMicroseconds(n);
          if(readString.indexOf('d') >0) myservod.writeMicroseconds(n);
        }
        else
        {   
          Serial.print("writing Angle: ");
          Serial.println(n);
          if(readString.indexOf('a') >0) myservoa.write(n);
          if(readString.indexOf('b') >0) myservob.write(n);
          if(readString.indexOf('c') >0) myservoc.write(n);
          if(readString.indexOf('d') >0) myservod.write(n);
        }
         readString=""; //clears variable for new input
      }
    }  
    else {     
      readString += c; //makes the string readString
    }
  }
}