Hi
I'm not a frequent poster as I find this forum helps resolve most queries, but I'm seeking a little help if possible please.
I may be approaching my solution from the wrong angle entirely but this is where I'm at.
I am trying to read a digital signal on a data pin and record the state of the pin at set intervals. There will be 16 samples.
I would like to end up with two seperate bytes of data which I can then use in other functions.
My code functions but the result is not as expected.
I know it's my conversion of the array to the uint8_t which is wrong. First issue is the result is backwards, it also misses 0's and in some cases I get what I will call an overflow (correct term?)
Here are a few examples of array vs converted which will hopefully explain better:-
*Example 1 - Missing 0's and backwards
11111110,00101011 // Data printed to the serial from the array
1111111,11010100 // Data printed to serial following conversion to uint8_t
*Example 2 - missing 0's, backwards and overflow
11111111,00000000 // Data printed to serial from array
11111111,0 // Data printed to serial following conversion
11111111,11111111 // Overflow?
11111111,11111111 // Overflow?
*Example 3 - Ok but both bytes are backwards
00000001,11110001 // Data printed to serial from array
10000000,10001111 // Data printed to serial following conversion
I have tried quite a few things to resolve - Changing array data type, using atoi, changing array length.
I know the conversion is the main issue but if someone could offer some help I'd appreciate it.
I obviously have an issue with the main capture into the array as the 'overflow' is present here as well, I have tried using delimiter '\0' but this doesnt seem to help.
Originally I captured all of the 16 bits into an array with a view to splitting it afterward, but I had the same issue with conversion.
Thanks
int pinstate;
int pin = 4; // Input on pin 4
uint8_t arrLength = 8; // 8no samples
uint8_t LStoredData[9]; // Lo byte storage set to 9 to include delimeter?
uint8_t HStoredData[9]; // Hi byte storage set to 9 to include delimeter?
void setup(void) {
pinMode(pin, INPUT); // Set pin to input
Serial.begin(9600);
delay(15); // Settle time for serial init
Serial.println("Ready to Sample"); // Send text just to check it's alive
}
void loop(void) {
pinstate = digitalRead(pin); // Read and monitor input for change. Input is normally High
// Read and store High byte
if (pinstate ==LOW) { // If input goes low, begin capture
for (int i = 0; i < arrLength ; i++) { // Loop 8 times to capture High Byte
delayMicroseconds(830); // Sample every x Microseconds
LStoredData[i] = digitalRead(pin); // Read current pin level and move to array
Serial.print (LStoredData[i]); // Print to serial
}
Serial.print (","); // Make it easier to read the two bytes on serial
// Read and store Low Byte
for (int i = 0; i < arrLength ; i++) { // Loop 8 times to capture High Byte
delayMicroseconds(830); // Sample every x Microseconds
HStoredData[i] = digitalRead(pin); // Read current pin level and move to array
Serial.print (HStoredData[i]); // Print to serial
}
Serial.println (); // New line on serial monitor
// Convert data in arrays to a useable real number
//Convert Low Byte
uint8_t LB = 0; // create variable for Low Byte
for (int i = 0; i <8; i++) // setup loop to shift 8 bits of data from array
{
LB |= LStoredData[i] << i; // Shift the data from the Low byte array into a uint8_t variable
}
Serial.print(LB, BIN); // Print result to serial
Serial.print (","); // Make it easier to read the two bytes on serial
//Convert High Byte
uint8_t HB = 0; // create variable for High Byte
for (int i = 0; i <8; i++) // setup loop to shift 8 bits of data from array
{
HB |= HStoredData[i] << i; // Shift the data from the Low byte array into a uint8_t variable
}
Serial.println(HB, BIN); // Print result to serial
} // end
} // end loop