Using variables inside digital write

I have a board that controls 48 small electric motors, the board uses what I think is called multiplex (according to that 4x4x4 led cube tutorial) I've attached a text schematic below. my pins are all wired to the input side of an optocoupler before using transistors to switch the main voltage.
Simply, I'm trying to send a string through serial (eg. 66V) to make motor 36 rotate.
I can change the code

digitalWrite(column[Message[0]], HIGH);
digitalWrite(layer[Message[1]], LOW);

to

    digitalWrite(7, HIGH);
    digitalWrite(10, LOW);

which will allow the motor to rotate, but trying to use message[0] doesn't write the pin high.
I've tried adding extra parenthesis but can't find any other information on the exact topic. I did find another post but it only deals with 1 serial input.
I'm also using the V as a check to ensure reading one of 3 specific codes I'm planning to send.

	5v	5v	5v	5v	5v	5v
0v	1	2	3	4	5	6
0v	7	8	9	10	11	12
0v	13	14	15	16	17	18
0v	19	20	21	22	23	24
0v	25	26	27	28	29	30
0v	31	32	33	34	35	36
0v	37	38	39	40	41	42
0v	43	44	45	46	47	48
IF left is 5V and top is 0V motor runs
/*
  
  [columns-Pin]
  1-2
  2-3
  3-4
  4-5
  5-6
  6-7
  7-8
  8-9
  [layer-Pin]
  A-A1
  B-A0
  C-13
  D-12
  E-11
  F-10
*/
//initializing and declaring motor columns
  int column[8]={2,3,4,5,6,7,8,9};
//initializing and declaring shelf layers
  int layer[6]={10,11,12,13,A1,A0};
const unsigned int MAX_MESSAGE_LENGTH = 3;
char Message[3] ={'-', '-', '-'};
 
void setup()
{
  // opens serial port, sets data rate to 9600 bps
  Serial.begin(9600); 
  //setting rows to ouput
  for(int i = 0; i<8; i++)
  {
    pinMode(column[i], OUTPUT);
  }
  //setting layers to output
  for(int i = 0; i<6; i++)
  {
    pinMode(layer[i], OUTPUT);
  }
  //setting layers to high
  for(int i = 0; i<6; i++)
  {
    digitalWrite(layer[i], 1);
  }
}

void loop()
{
   //Check to see if anything is available in the serial receive buffer
 while (Serial.available() > 0)
 {
   //Allow the Buffer to fill
   delay(200);
   //Create a place to hold the incoming message
   static unsigned int message_pos = 0;

   //Read the next available byte in the serial receive buffer
   char inByte = Serial.read();

   //Message coming in (check not terminating character) and guard for over message size
   if ( inByte != '\n' && (message_pos - MAX_MESSAGE_LENGTH - 1) )
   {
     //Add the incoming byte to our message
     Message[message_pos] = inByte;
     message_pos++;
   }
   //Full message received...
   else
   {
     //Add null character to string
     Message[message_pos] = '\0';
     //Reset for the next message
     message_pos = 0;
   }
 }
if (Message[2] == 'V')
  {
    Serial.println(Message);
    digitalWrite(column[Message[0]], HIGH);
    digitalWrite(layer[Message[1]], LOW);
    delay(20000); // #####
    delay(200); // #####
    digitalWrite(column[Message[0]], LOW);
    digitalWrite(layer[Message[1]], HIGH);
    Serial.println("Signal done");
    Message[0] = '-';
    Message[1] = '-';
    Message[2] = '-';

 }
 else
 {

 }
}




Hello

This is because Message[0] is a character, let's say '6', but you want a number 6. To convert, you can do this : Message[0] - '0' ( because in ASCII, '6' = 54 and '0' = 48, so '6' - '0' = 6 )

Thank you sir,
I also had the array for the layer backwards. Simple solution but I would never have worked out the ASCII, The other answer I had found one comment said basically message[0] - '0' but didn't have any explanation.
Again Thank you! Now I just have to work out how the old controller knew to stop rotating after 1 rotation -.-

NP :slight_smile:

And there is another mistake in your code : the first element in an array is in array[0], not array[1]

So for example in this array layer[6]={10,11,12,13,A1,A0};, the last element A0 is in layer[5], not layer[6]

And on the column too, Thank you again, although would this throw any realistic faults?

I think you mean:
(message_pos < MAX_MESSAGE_LENGTH - 1)

works as - but not as <?

MAX_MESSAGE_LENGTH is 3
Valid indexes into the Message array are 0, 1, and 2.

The original expression
(message_pos - MAX_MESSAGE_LENGTH - 1)
produces the following results:
0 -> -4 -> true
1 -> -3 -> true
2 -> -2 -> true (looking good so far...)
3 -> -1 -> true (Not good. This goes off the end of the array.)
4 -> 0 -> false (Good! At least it overflows only one byte.)

You probably wanted:
(message_pos < MAX_MESSAGE_LENGTH)

Thank-you!

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.