Need help with this code!

guys help, I have developed this code but don`t have a working arduino right now. Please anyone run this code and provide me the output it is getting.

//program to assign values to 18 digital pins using string from a
//serial port
 byte pin[] = {2,3,4,5,6,7,8,9,10,11,12,13,A0,A1,A2,A3,A4,A5};

int var,st; // variablees  for  loop
String value; // string from serial port
char compare,binary_value[18]; 
boolean feedback[18] = {0};
void setup()
{
  Serial.begin(9600);
  for(st=0;st<18;st++){
  pinMode(pin[st],OUTPUT); 
  compare ='1';
  } 
}

void loop()
{
  Serial.available();
  value = Serial.readString();
  // reading string from serial 
  value.toCharArray(binary_value, 18);
  //converting to char and storing in buffer
  
  
  for(st=0;st<18;st++){
    if(binary_value[st]==compare){
    digitalWrite(pin[st],HIGH);
    }
    else{
    digitalWrite(pin[st],LOW);
    }
  }
  
  
 
delay(200);

reading();
sending();
}

void reading(){
  // checking pin values to send to serial
  for(var=0;var<18;var++){
    
  feedback[var] = digitalRead(pin[var]);
  }
}

void sending(){
  // sending data to serial
  Serial.print('#');
  
  for(int k=0; k<18; k++)
  {
    Serial.print(feedback[k]);
    Serial.print('+');
  }
  Serial.print('~');
  Serial.println();
  delay(10);


}

please pass any 18bit binary no. as string like- ‘111111111111111111’

it should return the same no. and assign these to pins.

  Serial.available();I can see what this line is supposed to do but it is not actually checking whether any serial data is available.

if possible please tell what am I doing wrong.

Well, let’s work our way down from the top. pin has 18 members:

int pin[] = {2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, A0, A1, A2, A3, A4, A5};
int var, st, st1;

Your other arrays are 17 variables long. Why the mismatch? “var, st, and st1” are obfuscated variable names.
It has absolutely no comments. That sets the stage for a program that is tedious to decipher. I will break to see if anybody else wants to chime in.

You use st and st1 for the same purpose, so st1 is not needed.

  for (st1 = 0; st1 < 18; st1++) {
    pin[st1] = binary_value[st1];
  }

This simply copies one array into the other. You never use binary_value again, so you could have just used:

  value.toCharArray(pin, 18);

and eliminated binary_value.

Hold on. you just scrambled your pin definitions.

There’s more… I give up.

Aishwary:
if possible please tell what am I doing wrong.

Well, to start with

 Serial.available();

should presumably be

 if (Serial.available())

to test whether there is any serial data to read, but even if there is it does not mean that there are 18 bytes ready to read.

I suggest that you look at Serial input basics to see how to read serial input.

There is no character to binary translation anywhere in your program. You need to somehow get from the ASCII 0's and 1's that you send it, to HIGH and LOW to send to the pins. Character '0' is not the same as logic 0.

You should declare your pin as const int, so you won't forget that you should leave it alone.

i have made few changes as per your suggestions

Aishwary:
i have made few changes as per your suggestions

That was too fast. You need to spend some time with it.

You should declare your pin as const int,

Or even better const byte to save RAM.

UKHeliBob:
Or even better const byte to save RAM.

Until an Arduino comes out with more than 256 pins.

aarg:
Until an Arduino comes out with more than 256 pins.

Please PM me when that happens :slight_smile:

if anyone running this code, please tell what output it gives

Aishwary:
if anyone running this code, please tell what output it gives

You changed almost nothing. Did you actually read what I wrote?
Sorry, what we wrote.

yes, I need to develop some different logic i guess

your program does this:

#1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+~
#1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+~
#1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+~
#1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+~
#1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+~
#1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+~
#1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+~
#1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+~

Now you know what to do. :slight_smile:

guys I have changed the code, but im getting 0 at first and last position, can anyone explain me why??

I have changed the code,

It would be helpful to see it, don't you think ?

I have made the changes in the first post itself.

anyway here it is.

//program to assign values to 18 digital pins using string from a
//serial port
byte pin = {2,3,4,5,6,7,8,9,10,11,12,13,A0,A1,A2,A3,A4,A5};

int var,st; // variablees for loop
String value; // string from serial port
char compare,binary_value[18];
boolean feedback[18] = {0};
void setup()
{
Serial.begin(9600);
for(st=0;st<18;st++){
pinMode(pin[st],OUTPUT);
compare =‘0’;
}
}

void loop()
{
Serial.available();
value = Serial.readString();
// reading string from serial
value.toCharArray(binary_value, 18);
//converting to char and storing in buffer

for(st=0;st<18;st++){
if(binary_value[st]>compare){
digitalWrite(pin[st],HIGH);
}
else{
digitalWrite(pin[st],LOW);
}
}

delay(200);

reading();
sending();
}

void reading(){
// checking pin values to send to serial
for(var=0;var<18;var++){

feedback[var] = digitalRead(pin[var]);
}
}

void sending(){
// sending data to serial
Serial.print(’#’);

for(int k=0; k<18; k++)
{
Serial.print(feedback[k]);
Serial.print(’+’);
}
Serial.print(’~’);
Serial.println();
delay(10);

}

I have made the changes in the first post itself.

{sigh}

I have made the changes in the first post itself.

That is not a very friendly thing to do because it can make a nonsense of any later posts that refer to it. It would also be more helpful if you used code tags (seventh icon from the right above the smileys) when posting code.

Here is your program in code tags and formatted more consistently. Do you see how much nicer it looks and how much easier it is to copy and scroll through ?

//program to assign values to 18 digital pins using string from a
//serial port
byte pin[] = {2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, A0, A1, A2, A3, A4, A5};
int var, st; // variablees  for  loop
String value; // string from serial port
char compare, binary_value[18];
boolean feedback[18] = {0};
void setup()
{
  Serial.begin(9600);
  for (st = 0; st < 18; st++) {
    pinMode(pin[st], OUTPUT);
    compare = '0';
  }
}

void loop()
{
  Serial.available();
  value = Serial.readString();
  // reading string from serial
  value.toCharArray(binary_value, 18);
  //converting to char and storing in buffer
  for (st = 0; st < 18; st++) {
    if (binary_value[st] > compare) {
      digitalWrite(pin[st], HIGH);
    }
    else {
      digitalWrite(pin[st], LOW);
    }
  }
  delay(200);
  reading();
  sending();
}

void reading() 
{
  // checking pin values to send to serial
  for (var = 0; var < 18; var++) 
  {
    feedback[var] = digitalRead(pin[var]);
  }
}

void sending() 
{
  // sending data to serial
  Serial.print('#');
  for (int k = 0; k < 18; k++)
  {
    Serial.print(feedback[k]);
    Serial.print('+');
  }
  Serial.print('~');
  Serial.println();
  delay(10);
}

I will have a look at it.