Is CharAt() the best method for me

I am wondering if using a "CharAt()" method is a good idea for the following scenario:

I would define a string that has 26 values: "00110111000100010001110001" as an example.

I would step though the string reading each position using CharAt(x) one at a time.
I would then run subroutine "0" or subroutine "1" depending on the current CharAt(x) value.
Repeat 26 times by incrementing x.

Does this seem like a reasonable (and efficient) way to look at this desired behavior?? I have been staring at the Arduino reference site for quite a while.

(In case anyone is curious, I am trying to simulate an access control prox reader.)

Thanks for any advice.

Maybe? Are you using String class or c-string?

This?

If you have a String constant, yes, just juse CharAt(). If you are receiving the string of0's 1's from serial port, you should use c-string instead or at least reserve space ahead of time:

Most people don't use Strings on Arduino systems with only 2KB of ram.

Does this seem like a reasonable (and efficient) way to look at this desired behavior?? I have been staring at the Arduino reference site for quite a while.

Only if the data really is a string or String; A more efficient and compact method would be to use the bits of an integer like this.

long testData = 0b10100011100011100011100011100011;

void setup() 
{
  Serial.begin(115200);

  for (int bitCount = 31; bitCount >= 0; bitCount--)
  {
    byte thisBit = bitRead(testData, bitCount); 
    if (thisBit == 0)
    {
      zero();
    }
    else
    {
      one(); 
    }
  }
  Serial.println();
}

void zero()
{
  Serial.print(0);
}

void one()
{
  Serial.print(1);
}

void loop() 
{
}

Obviously the program is very artificial as it stands but it demonstrates the principles involved and the operation of bitRead() is very obvious.

Thank you both for your comments. This is why I like forums and support them whenever I can.

UKHeliBob's comment is spot-on with what I kinda knew must be out there, but didn't know how to ask. Thanks.

I hate being this green, but I shouldn't be for much longer.

James

Had to tell someone... AND THANK YOU for the help. I am on Cloud9 after taking this:

/*

 This code is a proof of concept for having an Arduino device mimic a 26bit wiegand card.

 Data is wiggled at 5mS interbit time.
 Pulse width is .3mS

  Pin14 is dZero
  Pin17 is dOne

 26bit wiegand
 bit1 = even parity, but is fixed at zero in this example
      parity is set to be ignored in Quintron sw
 bit2-9 = facility code, set to 774 decimal, 0000 0110 binary MSB
 bit10-25 = 16 bit value for UserID, set to 1 decimal, 0000 0001 binary MSB
 bit26 = odd parity, always zero
      parity is set to be ignored in Quintron sw

 Sequence is to send the "card read", wait 10 seconds, send the card read again.  
       

*/


// the setup routine runs once when you press reset;
// Give the pins each a name.
 int dZero = 4;
 int dOne = 7;
 // pulse is 100 microS;
 int pulse=100;
 // interBitDelay is 5mS;
 int interbitDelay=1;
 long randNumber;

void setup() {                

// initialize the serial communication:
  Serial.begin(9600);
  
// initialize the digital pin as an output.;
 pinMode(dZero, OUTPUT);     
 pinMode(dOne, OUTPUT);     
 digitalWrite(dZero, HIGH);   // Set voltage level high to start)
 digitalWrite(dOne, HIGH);    // Set voltage level high to start)
 

}


/*
  Basic loop, send card read, wait 7-10 seconds, repeat.

  Creating a "bit" is accomplished by driving the 
  appropriate data pin LOW for 100microS, returning to HIGH, 
  then DELAY 1mS to complete the bit transmission.
  
  
*/


void loop() {
//text send just before card read;
 Serial.println("Card read.");
 Serial.println("     ");
  
//  Bit 1: Parity bit, transmit a zero;
  digitalWrite(dZero, LOW);
  delayMicroseconds(pulse);             
  digitalWrite(dZero, HIGH);  
  delay(interbitDelay);  

//  Bit 2: transmit a zero;
  digitalWrite(dZero, LOW);
  delayMicroseconds(pulse);             
  digitalWrite(dZero, HIGH);  
  delay(interbitDelay);  

//  Bit 3: transmit a one;
  digitalWrite(dOne, LOW);
  delayMicroseconds(pulse);             
  digitalWrite(dOne, HIGH);  
  delay(interbitDelay);  

//  Bit 4: transmit a one;
  digitalWrite(dOne, LOW);
  delayMicroseconds(pulse);             
  digitalWrite(dOne, HIGH);  
  delay(interbitDelay);  

//  Bit 5: transmit a zero;
  digitalWrite(dZero, LOW);
  delayMicroseconds(pulse);             
  digitalWrite(dZero, HIGH);  
  delay(interbitDelay);  

//  Bit 6: transmit a zero;
  digitalWrite(dZero, LOW);
  delayMicroseconds(pulse);             
  digitalWrite(dZero, HIGH);  
  delay(interbitDelay);  

//  Bit 7: transmit a zero;
  digitalWrite(dZero, LOW);
  delayMicroseconds(pulse);             
  digitalWrite(dZero, HIGH);  
  delay(interbitDelay);  

//  Bit 8: transmit a zero;
  digitalWrite(dZero, LOW);
  delayMicroseconds(pulse);             
  digitalWrite(dZero, HIGH);  
  delay(interbitDelay);  

//  Bit 9: transmit a zero;
  digitalWrite(dZero, LOW);
  delayMicroseconds(pulse);             
  digitalWrite(dZero, HIGH);  
  delay(interbitDelay);  


// NOW THE CARD NUMBER;

//  Bit 10: transmit a zero;
  digitalWrite(dZero, LOW);
  delayMicroseconds(pulse);             
  digitalWrite(dZero, HIGH);  
  delay(interbitDelay);  

//  Bit 11: transmit a zero;
  digitalWrite(dZero, LOW);
  delayMicroseconds(pulse);             
  digitalWrite(dZero, HIGH);  
  delay(interbitDelay);  

//  Bit 12: transmit a zero;
  digitalWrite(dZero, LOW);
  delayMicroseconds(pulse);             
  digitalWrite(dZero, HIGH);  
  delay(interbitDelay);  

//  Bit 13: transmit a zero;
  digitalWrite(dZero, LOW);
  delayMicroseconds(pulse);             
  digitalWrite(dZero, HIGH);  
  delay(interbitDelay);  

//  Bit 14: transmit a zero;
  digitalWrite(dZero, LOW);
  delayMicroseconds(pulse);             
  digitalWrite(dZero, HIGH);  
  delay(interbitDelay);  

//  Bit 15: transmit a zero;
  digitalWrite(dZero, LOW);
  delayMicroseconds(pulse);             
  digitalWrite(dZero, HIGH);  
  delay(interbitDelay);  

//  Bit 16: transmit a zero;
  digitalWrite(dZero, LOW);
  delayMicroseconds(pulse);             
  digitalWrite(dZero, HIGH);  
  delay(interbitDelay);  

//  Bit 17: transmit a zero;
  digitalWrite(dZero, LOW);
  delayMicroseconds(pulse);             
  digitalWrite(dZero, HIGH);  
  delay(interbitDelay);  

//  Bit 18: transmit a zero;
  digitalWrite(dZero, LOW);
  delayMicroseconds(pulse);             
  digitalWrite(dZero, HIGH);  
  delay(interbitDelay);  

//  Bit 19: transmit a zero;
  digitalWrite(dZero, LOW);
  delayMicroseconds(pulse);             
  digitalWrite(dZero, HIGH);  
  delay(interbitDelay);  

//  Bit 20: transmit a zero;
  digitalWrite(dZero, LOW);
  delayMicroseconds(pulse);             
  digitalWrite(dZero, HIGH);  
  delay(interbitDelay);  

//  Bit 21: transmit a zero;
  digitalWrite(dZero, LOW);
  delayMicroseconds(pulse);             
  digitalWrite(dZero, HIGH);  
  delay(interbitDelay);  

//  Bit 22: transmit a zero;
  digitalWrite(dZero, LOW);
  delayMicroseconds(pulse);             
  digitalWrite(dZero, HIGH);  
  delay(interbitDelay);  

//  Bit 23: transmit a zero;
  digitalWrite(dZero, LOW);
  delayMicroseconds(pulse);             
  digitalWrite(dZero, HIGH);  
  delay(interbitDelay);  

//  Bit 24: transmit a zero;
  digitalWrite(dZero, LOW);
  delayMicroseconds(pulse);             
  digitalWrite(dZero, HIGH);  
  delay(interbitDelay);  

//  Bit 25: transmit a one;
  digitalWrite(dOne, LOW);
  delayMicroseconds(pulse);             
  digitalWrite(dOne, HIGH);  
  delay(interbitDelay);  

//  Bit 26: ending parity, transmit a zero;
  digitalWrite(dZero, LOW);
  delayMicroseconds(pulse);             
  digitalWrite(dZero, HIGH);  
  delay(interbitDelay);  

//  Pause for between 7 and 10 seconds;
  randNumber = random(5000, 8000);
 //Send data to the serial port each time a read occurs";
 Serial.println("Card data transmitted.");
 Serial.print("Random delay of ");
 Serial.print(randNumber);
 Serial.println("mS.");
 Serial.println("     ");
 delay(randNumber);
 
}

And just a few minutes later creating this using your help:

/*

Version 2,  this code is a proof of concept for having an Arduino 
device mimic a 37bit default card format wiegand card.

 Data is wiggled at 1mS interbit time.
 Pulse width is 100uS

  Pin14 is dZero
  Pin17 is dOne

 37bit wiegand
 bit1 = even parity, but is fixed at zero in this example
      parity is set to be ignored in Quintron sw
 bit2-17 = facility code, set to 774 decimal, 0110 0000 binary MSB
 bit18-36 = 19 bit value for UserID, set to 1 decimal, 0000 0001 binary MSB
 bit37 = odd parity, always zero
      parity is set to be ignored in Quintron sw

 Sequence is to send the "card read", wait 5-8 seconds, send the card read again.  
*/


// the setup routine runs once when you press reset;
// Give the pins each a name.
 int dZero = 4;
 int dOne = 7;
 int led = 13;
 // pulse is 100 microS;
 int pulse=100;
 // interBitDelay is 5mS;
 int interbitDelay=1;
 long randNumber;
// Card data is 37 bits;
// Card f/c code is 0000 0000 0100 0000;
// long cardData = 0b0000 0000 0110 0000 0000000000000000001;
 long cardData = 0b0000000000110000000000000000000000010;


void setup() {                

// initialize the serial communication:
  Serial.begin(9600);
  
// initialize the digital pin as an output.;
 pinMode(dZero, OUTPUT);     
 pinMode(dOne, OUTPUT);     
 digitalWrite(dZero, HIGH);   // Set voltage level high to start);
 digitalWrite(dOne, HIGH);    // Set voltage level high to start);
 digitalWrite(led, LOW);   // Set LED off to start);
}


/*
  Basic loop, send card read, wait 7-10 seconds, repeat.

  Creating a "bit" is accomplished by driving the 
  appropriate data pin LOW for 100microS, returning to HIGH, 
  then DELAY 1mS to complete the bit transmission.  
*/


void loop() {
//text send just before card read;
// Serial.println("Card read.");
// Serial.println("     ");

// Blink LED 13 to show card data sent;
 digitalWrite(led, HIGH);

  for (int bitCount = 36; bitCount >= 0; bitCount--)
  {
    byte currentBit = bitRead(cardData, bitCount); 
    if (currentBit == 0)
    {
      pulseD0();
    }
    else
    {
      pulseD1(); 
    }
  }
  Serial.println();

//  Pause for between 7 and 10 seconds;
  randNumber = random(5000, 8000);
 //Send data to the serial port each time a read occurs";
 Serial.println("Card data transmitted.");
 Serial.print("Random delay of ");
 Serial.print(randNumber);
 Serial.println("mS.");
 Serial.println("     ");
 delay(randNumber);
 

}
  
  void pulseD0() {
  
  //  Transmit a zero bit;
    digitalWrite(dZero, LOW);
    delayMicroseconds(pulse);             
    digitalWrite(dZero, HIGH);  
    delay(interbitDelay);  
    Serial.print("0");
  }

void pulseD1()
  {
  //  Transmit a one bit;
    digitalWrite(dOne, LOW);
    delayMicroseconds(pulse);             
    digitalWrite(dOne, HIGH);  
    delay(interbitDelay);
    Serial.print("1");  
  }

Freakin' amazing.