Byte array

Hi, I am using C# to send data to the arduino such at 010, 015, 200... Then i want my arduino to store and compare those characters such as

if (buf[0] == 0 & buf [1] == 1 && buf [0] and pin6 == LOW && pin 7 high) { digitalwrite(13, HIGH) }

My aim is to create a number of and gates or or gates and configure the inputs/outputs of the OR/AND Gate via C#

Any Suggestions?

Any Suggestions?

Go for it.

Or, are you having some kind of problem?

I'm trying to do it but i'm having trouble, if you've got any suggestions pls reply.

Thanks Matthew

You've shown one fragment that isn't even legal C - what is it you want to do?

Well my aim is to creat a Programmable logic controller (PLC). I created a program in C# which lets me draw ladder logic diagrams and assign a value to each line

I1 I2 Q1 ---] [--------] [---------( )----- = 210

Now what i want is to send the 210 serially to the arduino. the arduino stores it an Array and when serial is not available it starts an infinite loop and calls each value of the array ez (Arrray[0] = 210, Array[1] = 150, etc) and according to what value there is, the arduino performs a task.

So, the C# application passes data to an Arduino. The Arduino then has to read that serial data, store it in an array (if there is room), determine whether the Arduino is able to read from the serial port (not a trivial task), read data from an array, and execute some code based on the values read from the array.

What part(s) are you having trouble with? None of it (except that checking for an inactive serial connection) looks difficult.

Well from the C# to arduino i will only send 16 numbers ex. 123,12,58,147,32156,589,458,478,5,697,47,485,167,475,132,689 and thats done. The arduino will have to store those numbers so that when all 16 numbers are stored an infinite for loop will call each number and compare it. Now that's the part i can do, I need some assistance on how to store the numbers and then and do the infinite loop.

You need to create an array to store the values in:

int serialValues[16]; // Or however many values you need to save
int serialIndex = 0; // Point to next place to store a value

Then, you need to read the data from the serial port (presumably as a string, since many of your values are too large to send as bytes). Do you know how to do that?

Once you have a string, you need to convert it to an int. The atoi() function would be useful.

Then, you need to store the value in the array:

serialValues[serialIndex] = inVal; // where inVal is the integer from the string
serialIndex++; // Point to the next position

Some error checking would be useful. For instance, make sure that serialIndex is less than the size of the array, before storing a value there.

Then, create an infinite loop to read and process the commands:

while(1)
{
   // loop through the serialValues, and do something
   for(int i=0; i<serialIndex; i++)
   {
      int nextVal = serialValues[i];
      // Do something based on nextVal
   }
}

Hi first of all thanks for your time.

"Then, you need to read the data from the serial port (presumably as a string, since many of your values are too large to send as bytes). Do you know how to do that?" - Well no, I can not read a string value.

Once you have a string, you need to convert it to an int. The atoi() function would be useful. - what is the atoi() cause i've searched a lot before but still i don't know what it means exactly and how to use it.

Thanks, Warne :)

http://www.cplusplus.com/reference/clibrary/cstdlib/atoi/

The atoi function takes one argument - the NULL terminated array of characters that represent an integer. It returns that integer.

Well no, I can not read a string value.

I'm tired of retyping this every time this comes up... http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1273230655/

Hi Paul,

I took a look at that site and managed to compile a program that accepts the serial data <12> <45> <89> and shows 12, 45, 89. Now I’m try to store the integer values (12, 45, 89) into an array such that myArray [0] = 12, myArray [1] = 45 and myArray [2] = 89. After which, when the serial is not longer available i want to start an infintite loop which if myArray [0] == 12 then digital.Write(13,HIGH)
if myArray [1] == 45 then digital.Write(12,HIGH)
if myArray [2] == 89 then digital.Write(11,HIGH)

Unfortunately this is not working :frowning:

Here’s my Code

char inData[10];
int i = 0;
int index;
int myArray[16];
boolean started = false;
boolean ended = false;

void setup()
{
//Serial1.begin(9600);
Serial.begin(9600);
}
void loop()
{
while(Serial.available() > 0)
{
char aChar = Serial.read();
if(aChar == ‘<’)
{
started = true;
index = 0;
inData[index] = ‘\0’;
}
else if(aChar == ‘>’)
{
ended = true;
}
else if(started)
{
inData[index] = aChar;
index++;
i++;
inData[index] = ‘\0’;
}
}

// Convert the string to an integer
int inInt = atoi(inData);

//Store each integer into an array
myArray = inInt;

  • if(started && ended)*

  • {*

  • // Use the value*

  • while(1)*

  • {*

  • // loop through the serialValues, and do something*

  • for( i=0; i< 17; i++)*

  • {*
    _ if (myArray == “1”)_
    * digital.Write(13, HIGH);*
    _ if (myArray == “10”)
    * digital.Write(12, HIGH);
    if (myArray == “11”)
    digital.Write(11, HIGH);
    if (myArray == “21”)
    digital.Write(10, HIGH);
    }
    }*_

* // Get ready for the next time*
* started = false;*
* ended = false;*
* index = 0;*
* inData[index] = ‘\0’;*

}
}
Regards
Matthew

Program flow is wrong. You do this:- // Convert the string to an integer int inInt = atoi(inData); even though you haven't got a number in. You should wait until all the digits are in the buffer before extracting them.

When posting code highlight it and hit the # button in the reply box.

Well then I'm able to store them with this prgram

BUt mt problem is that once I get all the numbers stored and the serial communication is stopped/closed i want the arduino to start an infinite loop (and stoppes the loop if serial is availbale). In this infinite loop I want to get each number from an array (where i have stored all the integers recieved), compare them and if the compariosion is true then i want the arduino to control some outputs. I tired to do that in the previous program but no success so far :(

Regards Matthew

the # button in the reply box.

not your keyboard!

i want the arduino to start an infinite loop (and stoppes the loop if serial is availbale).

So it's not actually an infinite loop is it?

Ehh sry my mistake, here’s the code.

char inData[10];
int index;
boolean started = false;
boolean ended = false;

void setup()
{
//Serial1.begin(9600);
Serial.begin(9600);
}
void loop()
{
while(Serial.available() > 0)
{
char aChar = Serial.read();
if(aChar == ‘<’)
{
started = true;
index = 0;
inData[index] = ‘\0’;
}
else if(aChar == ‘>’)
{
ended = true;
}
else if(started)
{
inData[index] = aChar;
index++;
inData[index] = ‘\0’;
}
}

if(started && ended)
{
// Convert the string to an integer
int inInt = atoi(inData);
Serial.println(inInt);
// Use the value

// Get ready for the next time
started = false;
ended = false;

index = 0;
inData[index] = ‘\0’;

}
}

Ehh sry my mistake, here's the code.

AND THE # BUTTON!!!!!!!!

You need to time how long it is since the last data was available and if this exceeds a certain value then go and do your loop thing using the values previous stored. as soon as new data is available you stop the loop thing and go back to your input routines. It will help if you split it up into functions you call from the main loop.

So, you now know how to get serial data, and how to interpret the serial data as an integer. Once of the earlier replies showed how to store that value in an array.

You know how to loop through that array, and do something based on the value in the array.

The next thing to work on is determining whether there is a serial connection, or not. Just because there is no serial data waiting to be read does not mean that there is nothing on the other end of the serial port.

Before you worry too much about how to tie all the pieces together, you need to work on knowing whether you are in receive mode or execute mode.

Hi, I’m using C# to send values to my arduino such as 12 and 24 to my arduino. I’ve got the coding were my arduino reads 12 if i send <12> from C# and when i send * an infinte for loop is activated. Now I got the infinite for loop to go from 0 to 15 and start again from 0 to 15 and this will keep on going until the arduino is switched on.

my infite for loop
for (int b = 0; b<17; b++)
{
look what is the value of serialValue
and do something
}
I did this infinite for loop because i’m storing the C# values on the ardunio, which are then stored in an array called serialValues__, and when i send * from C# the infinte loop starts.__
As explaind i want the arduino to go through all the values it received and if it receives 12 and pin 10 is high then pin 12 is high etc…
the problem i’m facing is that when the infite loop starts again from 0 and the value 12 reappeard from the array and pin 10 is low, pin 12 remains high and i want it to go LOW. And when b starts from 0 again and pin 10 is high i want pin12 u go high again
HERE is my coding I would appreciate any help cause i’m a college student and i need to solve this problme ASAP
char inData[10];
int index;
boolean started = false;
boolean ended = false;
boolean final = false;
int serialValues[16];
int serialIndex = 0;
const int Pin13 = 13;
const int Pin12 = 12;
const int Pin11 = 11;
const int Pin10 = 10;
int buttonState1 = 0;
int buttonState2 = 0;
** void setup()**
** {**
** //Serial1.begin(9600);**
** Serial.begin(9600);**
** //setting pinouts**
** pinMode(Pin13, OUTPUT);**
** pinMode(Pin12, OUTPUT);**
** pinMode(Pin11, INPUT);**
** pinMode(Pin10, INPUT);**
** }**
** void loop()**
** {**
** buttonState1 = digitalRead(Pin11);**
** buttonState2 = digitalRead(Pin10);**

** while(Serial.available() > 0)**
** {**
** char aChar = Serial.read();**
** if(aChar == ‘<’)**
** {**
** started = true;**
** index = 0;**
** inData[index] = ‘\0’;**
** }**
** else if(aChar == ‘>’)**
** {**
** ended = true;**
** }**

** else if(started)**
** {**
** inData[index] = aChar;**
** index++;**
** inData[index] = ‘\0’; **
** }**

__ else if (aChar ==’*’)__
** {**
** final = true;**
** }**
** }**

** if(started && ended)**
** {**
** // Convert the string to an integer**
** int inInt = atoi(inData);**
** Serial.println(inInt);**
** // Use the value**
** serialValues [serialIndex] = inInt;**
** serialIndex++;**

** // Get ready for the next time**
** started = false;**
** ended = false;**
** index = 0;**
** inData[index] = ‘\0’; **
** }**

** if(final)**
** while(1)**
{
** // loop through the serialValues, and do something**
** for(int b=0; b <16;b++ )**
** {**

__ int nextVal = serialValues**;
//if serial value is 24 and pin10 is connected to 5V switch led12 on**
** if (nextVal == 24 && buttonState2 == HIGH)
{
digitalWrite(Pin12, HIGH);
}
//if serial value is 12 and pin11 is connected to 5V switch led13 on**
** if (nextVal == 12 && buttonState2 == LOW)
{
digitalWrite(Pin12, LOW);
}
Serial.println(b);
delay(1000); **__

** }**
** }**
** }**

the problem i'm facing is that when the infite loop starts again from 0 and the value 12 reappeard from the array and pin 10 is low, pin 12 remains high and i want it to go LOW. And when b starts from 0 again and pin 10 is high i want pin12 u go high again

The for loop is not an infinite loop. It has finite limits on the values that b can hold.

After the if tests, add else clauses to define what to do when the if test is false.

hey paul thanks but i managed to do it. what i was intend to do was to start a loop when a command is inputted to the arduino. This loop goes from 0 to 12 and when b== 12, then b is set to 0 (b=0) so that the loop will start over again, and continue until the arduino is switched off. that is why i called it and infinite loop :)

thanks again Regards Warne