I finally got the code to work properly. This is the final version of the code. Actually, I could say my real first code 8-). PaulS should make a book of advises/codes and sell it...
/*
Code by: Reynal74
special thanks to PaulS for all his help
*/
//Variable Declaration
int ledPin = 9;//Setting Pin 9PWM for the LED
boolean started = false;
boolean ended = false;
char buffer[300];//creating buffer large enough
int serialIn = 0;//counter for the incoming data
int atoiHolder = 0;//holds the value of atoi
void setup(){
pinMode(ledPin, OUTPUT);//ledPin as an output
Serial.begin(9600);
}
void loop(){
while(Serial.available() > 0){
char incomingData = Serial.read(); //reading from the serial ( data from C# )
if(incomingData == '<'){//check for started packet
started = true;//the string started
ended = false;
}
else if(incomingData == '>'){//check for ended packet
ended = true;//indicated end of the reading
break; //break out of the loop
}
else{
buffer[serialIn] = incomingData;//begining to store data in the buffer
serialIn++;
buffer[serialIn] = '\0';// NULL terminate the array
}
}//END OF WHILE LOOP
//--------------CHECKING & TESTING -------------------
if(started && ended){
//GETTING THE ON AND OFF BUTTON FROM C# WORKING
if(buffer[0] == 'D' && buffer[1] == '0'){//turn off the light
digitalWrite(ledPin,LOW);
}
else if(buffer[0] == 'D' && buffer[1] == '1'){//turn on the light
digitalWrite(ledPin,HIGH);
}
//GETTING THE TRACK BAR FROM C# WORKING (TO VARY THE LED LIGHT INTENSITY)
else if(buffer[0] == 'A'){
buffer[0] = '0';//replaces A by '0' and now buffer holds '0','X','X','X',NULL
atoiHolder = atoi(buffer);//atoiHolder (LED Intensity) now holds XXX
/*ALTERNATIVE METHOD SUGGESTED BY: robtillaart
start conversion at index 1, just after the A. This keeps the original string intact,
optionally for later use. Note: added a var cmd to hold the type of command.
char cmd = inData[0];
int val = atoi( &inData[1] );
*/
analogWrite(ledPin,atoiHolder);
}
else {
//If no data matches the descriptions above, give a blinking light to indicate an error
digitalWrite(ledPin,HIGH);
delay(100);
digitalWrite(ledPin,LOW);
delay(100);
}
serialIn = 0;//resetting serialIn
buffer[serialIn] = '\0';//Null the buffer array
started = false; //reset to false
ended = false; //reset to false
}//END OF CHECKING & TESTING
}
Can Arduino tell C# or any other program what to do ( like control the other program )? If so, can those commands be made into graphic? what is the maximum amount of FLOPS that Arduino can put out? Can Arduino board program any other chips?