I am trying to control a resistor ladder DAC using an Arduino.
Basically, I am trying to control a group of 8 digital out pins based on a decimal
number that I input via the serial interface.
The issue that I am currently having is with
char command[4] = inData[];
How do I access this array?
I get an error of "expected primary-expression before ']' token"
char inData[20]; // Allocate some space for the string
char inChar; // Where to store the character read
byte index = 0; // Index into array; where to store the character
void setup()
{
Serial.begin(115200);
for(byte i = 0; i < 8; i++){
pinMode(-i+11,OUTPUT);
}
}
void loop()
{
if (Serial.available() ) {
while(Serial.available() > 0)
{
char aChar = Serial.read();
if(aChar == '\n')
{
// End of record detected. Time to parse
index = 0;
inData[index] = NULL;
}
else
{
inData[index] = aChar;
index++;
inData[index] = '\0'; // Keep the string NULL terminated
}
}
int someValue = 100; //For this example, lets convert the number 20
char binary[9] = {0}; //This is where the binary representation will be stored
Serial.println(someValue); //print out our string.
char command[4] = inData[]; //How do I access this array?
someValue= atoi(&command[1]);
Serial.println(someValue); //Print value recieved from PC
someValue += 208; //Adding 208 so that there will always be 8 digits in the string
itoa(someValue,binary,2); //Convert someValue to a string using base 2 and save it in the array named binary
char* string = binary + 1; //get rid of the most significant digit as you only want 8 bits
Serial.println(); //Space
Serial.println(string); //print out the string that will be applied to pins
for(byte i = 0; i < 8; i++){
digitalWrite(-i+11,string[i] - '0'); //write to the pin (the - '0' converts the bit of the string to HIGH or LOW)
}
}
}