relay control help

Guys,
'1284 , IDE 1.0, maniacbug environment for pin definitions, optiboot hexfile bootloader.
What am I doing wrong here?

I power, LED on D0 flashes 4 times.

What I am I doing wrong here? IDE 1.0

I enter 00, I get back

"0207207

Invalid Relay Number 207"

according to asciitable.com, character ‘0’ is 0x30, so why is the math not working?

arrg, software …

/*
Relay Control Sketch
 Robert Patterson, Cross Roads Fencing Center
 for Brad Rojas, brojas@accllcpos.com
 */
byte relayArray[21] = { 1, 14, 16, 18,20,22,28,1,1,1,1,1,15,17,19,21,23,24,25,26,27};
/*
byte relayArray[0] = 1;  // not used
 byte relayArray[1] = 14; // T3-C10
 byte relayArray[2] = 16; // T2-C2
 byte relayArray[3] = 18; // T4-C2
 byte relayArray[4] = 20; // T2-C3
 byte relayArray[5] = 22; // T1-C5
 byte relayArray[6] = 28; // T2-C5
 byte relayArray[7] = 1;  // not used
 byte relayArray[8] = 1;  // not used
 byte relayArray[9] = 1;  // not used
 byte relayArray[10] = 1;  // not used
 byte relayArray[11] = 1;  // not used
 byte relayArray[12] = 15; // T1-C2
 byte relayArray[13] = 17; // T3-C2
 byte relayArray[14] = 19; // T0-C3
 byte relayArray[15] = 21; // T1-C3
 byte relayArray[16] = 23; // T3-C3
 byte relayArray[17] = 24; // T3-C5
 byte relayArray[18] = 25; // T4-C5
 byte relayArray[19] = 26; // NO CONNECTIONS
 byte relayArray[20] = 27; // NO CONNECTIONS
 */
byte activityLED = 0; //

// VARIABLES  
byte x = 0;
byte digitTens = 0;
byte digitOnes = 0;
byte relayNumber = 0;
byte relayValid = 0;

void setup(){
  // set up relay control pins and make sure turned off
  // output pins float at turn on
  for (x = 1; x<21; x=x+1){
    pinMode (relayArray[x], OUTPUT);
    digitalWrite(relayArray[x], HIGH); // HIGH = off, LOW = energized
  }

  // Enable serial ports & clear incoming buffer
  Serial.begin(9600); // USB port
  Serial.flush();
  //Serial1.begin(9600); // RS232 port
  //Serial1.flush();

  // set up activity LED, flash to show set up is done
  pinMode (activityLED, OUTPUT);  // HIGH = On, LOW = Off
  for (int x = 0; x<4; x=x+1){  // flash a couple times on startup
  digitalWrite (activityLED, HIGH);
  delay (200);
  digitalWrite (activityLED, LOW);
  delay (200);
  }

}
void loop(){

  // get 2 characters from the UBS Serial port
  if (Serial.available()){
    digitTens = Serial.read(); // read a byte & convert from ASCII
    Serial.print(digitTens, HEX);
  }
  if (Serial.available() ){
    digitOnes = Serial.read();
    Serial.print(digitOnes, HEX);
  }
    relayNumber = digitTens * 10 + digitOnes;
    Serial.println (relayNumber);

    // check of a good number
    if (( relayNumber >=1 & relayNumber <=6) | (relayNumber >=12 & relayNumber <=20)){
      relayValid = 1;
      Serial.print("Relay Number ");
      Serial.println(relayNumber);
    } // end valid relay received
    else {
      relayValid = 0;
      Serial.print ("Invalid Relay Number ");
      Serial.println (relayNumber);
      // turn any that were on, off.
      for (x = 1; x<21; x=x+1){
        digitalWrite(relayArray[x], HIGH); // HIGH = off, LOW = energized
      } // end turn off relays with invalid relayNumber received
     // end invalid relay received
  } // end if Serial available

  // clear for next command
  digitTens = 0;
  digitOnes = 0;

  // Change state of the Relay
  if (relayValid == 1){
    if (activityLED ==1){ 
      digitalWrite (relayArray[relayNumber], LOW);
      digitalWrite (activityLED, HIGH);
      activityLED = 0;
      // do something with the other serial port
      Serial.print(relayNumber);
      Serial.println ("relay on");
    }
    else {
      digitalWrite (relayArray[relayNumber], LOW);
      digitalWrite (activityLED, LOW);
      activityLED = 1;
      Serial.print(relayNumber);
      Serial.println ("relay off");
    }
    delay (500);
  } // end change relay state
} // end void loop
  Serial.begin(9600); // USB port
  Serial.flush();

Open the serial port, then block until all pending serial data has been sent. There haven't been ANY Serial.print() or Serial.write() statements, so what are you doing here?

  if (Serial.available()){
    digitTens = Serial.read(); // read a byte & convert from ASCII
    Serial.print(digitTens, HEX);
  }
  if (Serial.available() ){
    digitOnes = Serial.read();
    Serial.print(digitOnes, HEX);
  }
    relayNumber = digitTens * 10 + digitOnes;
    Serial.println (relayNumber);

If you send 27 using the Serial Monitor, the value in digitTens will be '2', not 2. The value in digitOnes will be '7', not 7. Multiplying '2' by 10 makes no sense. Nor does adding '7' to the result.

Each of those read() statements needs " ' '0'" added to the end (before the ;).

That’s what I’m saying. I expect to receive characters over the serial port.
For number 0, that would be 0x30,
number 1, 0x31. etc.

Dropped it for now, just wrote a simple sketch with hard D# to turn off & on, confirmed I can control all the relays, and also showed me I had my D#s swapped end for end on my schematic for port A - D24 being A0 and not A7. Guess that makes sense as maniacbug seems to have gone 0-7, 8-15, 16-23 for the other ports from 0-7 also. Didn’t look at pins_arduino.c (h?) to confirm.

Almost done checking out the hardware.

For number 0, that would be 0x30,

Shouldn't that be > For character 0, that would be 0x30? I think I've seen a conversion/trick by adding or substracting 0 to the character to get the character to convert the byte value.

Yes,
http://www.asciitable.com/
‘0’ = 0x30
‘1’ = 0x31
‘2’ = 0x32
etc.
so I had planned to subtract 0x30 to convert to a number, only I get those odd results instead.

Had done the same for an I2C RTC test where F1xx, F2xx, etc came in

  if (Serial.available() >3){
    incomingCommand = Serial.read();
    incomingRegister = Serial.read();
    incomingData1 = Serial.read();
    incomingData1 = incomingData1 - 0x30; // convert ASCII to HEX
    incomingData2 = Serial.read();
    incomingData2 = incomingData2 - 0x30;  // convert ASCII to HEX
    new_data = (incomingData1 << 4) + incomingData2;  // put the Upper/Lower nibbles together
    Serial.print ("command ");
    Serial.println (incomingCommand);
    Serial.print ("register ");
    Serial.println(incomingRegister);
    Serial.print ("data1 ");
    Serial.println (incomingData1, HEX);
    Serial.print ("data2 ");
    Serial.println (incomingData2, HEX);
    Serial.print ("combined data ");    
    Serial.println (new_data, HEX);
    
  }

In that case I wanted the data in certain nibbles.
In this case I wanted a number from 1 to 20 for pointers into an array, so the conversion of tensDigit * 10 + onesDigit was used instead.

ended up just changing the # by hand & redownloading to get thru hardware testing, would like to show the serial commands working tho.

/*
Relay Control Sketch, ATMega1284 with FTDI Module on Serial and MAX232 on Serial1
 */
// Simple test of Relay on pin Dxx, change relayNumber by hand
byte activityLED = 0; //
byte relayNumber = 30;

void setup(){
  // set up relay control pins and make sure turned off
  // output pins float at turn on
  pinMode (relayNumber, OUTPUT);
  digitalWrite(relayNumber, HIGH); // HIGH = off, LOW = energized

  // Enable serial ports & clear incoming buffer
  Serial.begin(9600); // USB port
  Serial.flush();
  Serial1.begin(9600); // RS232 port
  Serial1.flush();

  // set up activity LED, flash to show set up is done
  pinMode (activityLED, OUTPUT);  // HIGH = On, LOW = Off
  for (int x = 0; x<=3; x=x+1){
    digitalWrite (activityLED, HIGH);
    delay (200);
    digitalWrite (activityLED, LOW);
    delay(200);
  }

}
void loop(){
  digitalWrite (relayNumber, LOW);
  digitalWrite (activityLED, LOW);
  Serial.print ("RelayNumber "); // print out on Serial to show it works
  Serial.print (relayNumber);
  Serial.println (" On");
  Serial1.print ("RelayNumber ");  /print out on 2nd Serial to show it works
  Serial1.print (relayNumber);
  Serial1.println (" On");
  delay (500);
  digitalWrite (relayNumber, HIGH);
  digitalWrite (activityLED, HIGH);
  Serial.print ("RelayNumber ");
  Serial.print (relayNumber);
  Serial.println (" Off");
  Serial1.print ("RelayNumber ");
  Serial1.print (relayNumber);
  Serial1.println (" Off");
  delay (500); 
} // end void loop

so I had planned to subtract 0x30 to convert to a number

As opposed to subtracting the more meaningful ‘0’ that I first suggested?

    new_data = (incomingData1 << 4) + incomingData2;  // put the Upper/Lower nibbles together

So, you type “27” and hit send. The value in incomingData1 is now 2, and the value in incomingData2 is now 7. newData is assigned the value of (2 * 16) + 7. That is not 27, unless there is something wrong with my calculator.

The equation you had before was correct, except that the incomingDataN values were wrong.