Go Down

Topic: control the arduino with more level command (Read 3559 times) previous topic - next topic

jodge

hello
i'm newbie here, i will try to controll my arduino and look around and find this:
http://sheepdogguides.com/arduino/art4SeSimpFrmIDE.htm

simple and easy but i have a little problem with this, if i have more level control command this solution not work for me. Ex:

first command should be: 1 or 2

if the command 1:  the second command should be: a,b,c,d
if the command 2:  the second command should be: e,f,g,h

if the second command done the arduino wait the next command and this command again should be 1 or 2 but never a,b,c,d.....


this code-part not work for me, bcose after the second Serial.read() command the program won't exit into the first Serial.read(),  can somebody help me?
Thank you
j.

Code: [Select]
if (Serial.available() > 0) {
// read the incoming byte:
incByte1 = Serial.read();
if (incByte1==114){settings = 1;} 
if (incByte1==119){settings = 2;} 

}
switch (settings) {
  case 1:
 
incByte2 = Serial.read();
//do something

break;



Old_guy

maybe do a Serial.println(incByte1) after the Serial.read() to see that the value is as expected...

Also, post all the code, what is the type of incByte1?

graynomad

Code: [Select]
f (incByte1==114){settings = 1;} 
if (incByte1==119){settings = 2;} 

}
switch (settings) {
  case 1:


If that's all you're doing you may as well just switch on incByte, no need to assign a value to it then switch on that value.

Code: [Select]

switch (incByte1) {
  case 114:
    break;
  case 119:
    break;


______
Rob

Rob Gray aka the GRAYnomad www.robgray.com

jodge

uhh thanks for the replies, i make a simple sketch maybe is better like my first post
Pls read the  marked comment
thank you
j.

Code: [Select]
int settings=0;

void setup()
{
Serial.begin(9600);
pinMode(13,OUTPUT);
pinMode(12,OUTPUT);
Serial.print("Enter s or f on serial terminal ");
Serial.println("to choose red or green led");
Serial.print("enter x or o on serial terminal ");
Serial.println("to switch off or on the selected LED");
};

void loop()
{
int incomingByte=0;

if (Serial.available() > 0) {
incomingByte = Serial.read();
Serial.print("I received: ");
Serial.print(incomingByte, DEC);
        if (incomingByte==115) {settings=1;
                Serial.println(", code for s. red led selected");};
        if (incomingByte==102) {settings=2;
                Serial.println(", code for f. green led selected.");};       
       
     }

switch (settings) {
  case 1:
  digitalWrite(13,HIGH);
  break;
//swich on the red led
// i want do here second serial.read() to recive a second level command. Example: blinking 5 time a red led and switch off. Then back to the beginning of the loop and
//read again a master level command to select a led again (red or green) but never (!) any second level command like blinking
  case 2:
  digitalWrite(12,HIGH);
  break;
  }
};

PaulS

You need to think about how serial data is sent to the Arduino (ssslllooowwwlllyyy).

If you enter "s", hit Send, enter "x", and hit Send, as fast as possible, there will still have been at least 1/4 second elapsed, and the Arduino will have executed 4 million instructions.

What you need to do is separate the reading of the serial data from the acting on the serial data. You need to remember what the previous value entered was.

So, if you read an s or f, set settings. If you read and x or o, AND the previous letter was s or f, you know what to do to which LED. If the previous letter was not s or f, ignore the current letter.

jodge

thx for your answer maybe clear now for me

i have idea
send data to arduino and the program cut the recived data into an array.

like:
sending: sx   recived and cutted data: settings
  • = s ; settings [1] = x; ( s turn the red led on, x blinking the led)
    sending: s0   recived and cutted data: settings
  • = s ; settings [1] = 0; (turn the red led on, and do nothing)
    sending: f1   recived and cutted data: settings
  • = f ; settings [1] = 1; ( s turn the green led on, 1 switch off  the led)

    it is working? Or impossible to reciving any array just simple byte?




                                 

jodge

uhh really funny this auto format
Code: [Select]

sending: sx   recived and cutted data: settings[0] = s ; settings [1] = x; ( s turn the red led on, x blinking the led)

sending: s0   recived and cutted data: settings[0] = s ; settings [1] = 0; (turn the red led on, and do nothing)

sending: f1   recived and cutted data: settings[0] = f ; settings [1] = 1; ( s turn the green led on, 1 switch off  the led)

PaulS

Quote
Or impossible to reciving any array just simple byte?

You can only read single bytes, but that does not preclude you storing those bytes in an array. Though there is no real reason to use an array to store two values.

You could simply use 4 different values:
a = red on
b = red off
c = green on
d = green off

However, learning how to read more than one byte is a good thing to do.

jodge

sorry but this LED thing just exaple. Sure i can make the whole thing just 4 command :)

I have may better example :))  (i know but i want to understand all possibilities )

i want to storge data into the internal eeprom and blinking the #13 LED while the read/write order processing

Sending:   wbdata   (w: write, b: blinking, data: 500 byte)
Sending:   rb          (r: read, b: blinking, sending the whole eep to serial.write)

can i use the array in this case too?

Really thx for any answer



PaulS

Quote
can i use the array in this case too?

Yes, and in this case, you should.

jodge

@PaulS really thx for your help


i look into refernces/array and i can't find info about the array maximum size.
Is there anybody here who has any information regarding this?

PaulS

There is no theoretical limit on the size of an array. There are practical limits, obviously, depending on the amount of physical memory you have. A UNO, for instance, has 2K of SRAM, where arrays and other variables, and literals, are stored.

The type of the array (byte, int, long, float, etc.) also affects how many elements the array can hold.

jodge

oki thx,
finally
if i want to send complicate orders to arduino the best way to using array, if i want to send mixed command (orders and data) the array solution not really work only if the data lenght not really long.

king :)

Go Up