Why are my serial readings not accurate? Using 2 xbees - arduino

I have one XBee-Arduino transmitting a simple int counter to a receiver XBee-Arduino. The receiver prints fine until it gets to 16 and beyond. This is an example of my outputs:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
48
17
50
19
52
21
54
23
56
25
26
59
28
61
62
63
64
65
98
67
68
69
I have tried new XBees and the XBees do not seem to be the problem.
Transmitter Code:
#include "SoftwareSerial.h"
int count = 1;
// RX: Arduino pin 2, XBee pin DOUT. TX: Arduino pin 3, XBee pin DIN
SoftwareSerial XBee(2, 3);
void setup() {
XBee.begin(115200);
Serial.println ("Initializing...");
}
void loop() {
XBee.write(count);
delay(1000);
count++;
}
Receiver Code:
#include "SoftwareSerial.h"
// RX: Arduino pin 2, XBee pin DOUT. TX: Arduino pin 3, XBee pin DIN
SoftwareSerial XBee(2, 3);
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
Serial.println("Started Ground station");
XBee.begin(115200);
}
void loop() {
// put your main code here, to run repeatedly:
if (XBee.available())
{
int c = XBee.read();
Serial.println(c);
delay(1000);
}
else
{
Serial.println("XBee not available.");
delay(1000);
}
}
I just want the receiver to print the counter as it is. Not sure why I am getting those random numbers after 15. Any help is appreciated.

The incorrect numbers are different by one bit.
This could be caused by different timing when reading the serial line, especially at high baud rates (Software serial isn't particularly reliable at the higher baud rates). You can see where a bit has drifted into the timing for an adjacent bit e.g.
16 = 0001 0000 was interpreted as 0011 0000 = 48
18 = 0001 0010 was interpreted as 0011 0010 = 50
20 = 0001 0100 was interpreted as 0011 0100 = 52
27 = 0001 1011 was interpreted as 0011 1011 = 59
34 = 0010 0010 was interpreted as 0110 0010 = 98
Try using a lower baud rate so that the timing isn't as critical and the bits are less likely to drift into the timing for adjacent bits.

Related

Using CD74HC4067 16-Channel Analog Multiplexer Demultiplexer is not working as expected

I am working on a project that needs multiple digital signals and I am considering the CD74HC4067 (on a break-board) to work as a multiplexer. More specifically i am trying to build a keypad with multiple ttp123 break-boards. Each ttp123 signal is connected to one of the 0-15 pins of the CD74HC4067. The S0-S3 pins are connected to digital Pins 7-4 on a pro mini. Signal is on A0 and EN is on A1. A1 and EN have a 4K7 resistor to ground. I power the setup with a separate 5v and the arduino is connected on a pc usb port.
I expect when a ttp123 is pressed to trigger a HIGH and this event to be passed to SIG pin.
Here is my code:
//Using CD74HC4067 16-Channel Analog Multiplexer
//Mux control pins
int s0 = 6;
int s1 = 7;
int s2 = 8;
int s3 = 9;
//Mux in "SIG" pin
int SIG_pin = A0;
int EN_pin = A1;
void setup(){
pinMode(s0, OUTPUT);
pinMode(s1, OUTPUT);
pinMode(s2, OUTPUT);
pinMode(s3, OUTPUT);
pinMode(EN_pin, OUTPUT);
digitalWrite(s0, LOW);
digitalWrite(s1, LOW);
digitalWrite(s2, LOW);
digitalWrite(s3, LOW);
digitalWrite(EN_pin, LOW);
Serial.begin(9600);
Serial.println("keypad interfacing_CD74HC4067 starting...");
}
void loop(){
String a;
//digitalWrite(EN_pin, HIGH);
Serial.println(" press any key to continue ...");
//digitalWrite(EN_pin, LOW);
while (!Serial.available());
while(Serial.available()) {
//Loop through and read all 16 values
for(int i = 0; i < 16; i ++){
//Serial.print("Value at channel ");
//Serial.print(i);
//Serial.print("is : ");
//Serial.println(readMux(i));
float volts = readMux(i);
Serial.print("Value at channel ");
Serial.print(i);
Serial.print(" is : ");
Serial.print(volts);
Serial.println();
//delay(1000);
}
a= Serial.readString();// read the incoming data as string
}
}
float readMux(int channel){
int controlPin[] = {s0, s1, s2, s3};
int muxChannel[16][4]={
{0,0,0,0}, //channel 0
{1,0,0,0}, //channel 1
{0,1,0,0}, //channel 2
{1,1,0,0}, //channel 3
{0,0,1,0}, //channel 4
{1,0,1,0}, //channel 5
{0,1,1,0}, //channel 6
{1,1,1,0}, //channel 7
{0,0,0,1}, //channel 8
{1,0,0,1}, //channel 9
{0,1,0,1}, //channel 10
{1,1,0,1}, //channel 11
{0,0,1,1}, //channel 12
{1,0,1,1}, //channel 13
{0,1,1,1}, //channel 14
{1,1,1,1} //channel 15
};
//loop through the 4 sig
for(int j = 0; j < 4; j ++){
digitalWrite(controlPin[j], muxChannel[channel][j]);
}
//read the value at the SIG pin
int val = analogRead(SIG_pin);
//return the value
float voltage = (val * 5) / 1024.0;
return voltage;
}
Just for simplicity, I have connected only one ttp123 on the system, ttp123 signal to channel12 on 74HC4067. With ttp123 touched, I would expect to get a high on SIG when I write
{0,0,1,1}, to S0-S3. All other channels should be LOW. However here is what I get:
On my first run after boot:
press any key to continue ...
Value at channel 0 is : 2.53
Value at channel 1 is : 2.58
Value at channel 2 is : 2.53
Value at channel 3 is : 2.60
Value at channel 4 is : 2.51
Value at channel 5 is : 2.55
Value at channel 6 is : 2.50
Value at channel 7 is : 2.52
Value at channel 8 is : 2.42
Value at channel 9 is : 2.47
Value at channel 10 is : 2.43
Value at channel 11 is : 2.50
Value at channel 12 is : 2.40
Value at channel 13 is : 2.43
Value at channel 14 is : 2.38
Value at channel 15 is : 2.44
and then all channels seem to lower...
press any key to continue ...
Value at channel 0 is : 1.70
Value at channel 1 is : 1.76
Value at channel 2 is : 1.71
Value at channel 3 is : 1.78
Value at channel 4 is : 1.70
Value at channel 5 is : 1.76
Value at channel 6 is : 1.72
Value at channel 7 is : 1.77
Value at channel 8 is : 1.67
Value at channel 9 is : 1.72
Value at channel 10 is : 1.69
Value at channel 11 is : 1.76
Value at channel 12 is : 1.69
Value at channel 13 is : 1.73
Value at channel 14 is : 1.67
Value at channel 15 is : 1.74
and I get no HIGH on channel 12 as I should.
Is there a need for a pullup or pulldown resistors in this setup? Should I connect a resistor to each S0-S3 with ground? Why is this not working?
Since there was an elementary failure in the code as well I would like to post the solution in here:
I had failed to initialize the A0 pin as input:
pinMode(SIG_pin, INPUT);
Here is the working set of code:
// interfacing mux with tp223
//Mux control pins
const int s0 = 6;
const int s1 = 7;
const int s2 = 8;
const int s3 = 9;
//Mux in "SIG" pin
int SIG_pin = A0;
int EN_pin = 10;
void setup(){
pinMode(s0, OUTPUT);
pinMode(s1, OUTPUT);
pinMode(s2, OUTPUT);
pinMode(s3, OUTPUT);
pinMode(SIG_pin, INPUT);
pinMode(EN_pin, OUTPUT);
digitalWrite(s0, LOW);
digitalWrite(s1, LOW);
digitalWrite(s2, LOW);
digitalWrite(s3, LOW);
digitalWrite(EN_pin, LOW);
Serial.begin(115200);
Serial.println("keypad interfacing_CD74HC4067 starting...");
}
void loop(){
//Loop through and read all 16 values
for(int i = 0; i < 16; i ++){
int val = readMux(i);
if (val > 0) {
Serial.print(i);
Serial.print(" pressed ");
Serial.print(val);
Serial.println();
}
}
}
float readMux(int channel){
int controlPin[] = {s0, s1, s2, s3};
int muxChannel[16][4]={
{0,0,0,0}, //channel 0
{1,0,0,0}, //channel 1
{0,1,0,0}, //channel 2
{1,1,0,0}, //channel 3
{0,0,1,0}, //channel 4
{1,0,1,0}, //channel 5
{0,1,1,0}, //channel 6
{1,1,1,0}, //channel 7
{0,0,0,1}, //channel 8
{1,0,0,1}, //channel 9
{0,1,0,1}, //channel 10
{1,1,0,1}, //channel 11
{0,0,1,1}, //channel 12
{1,0,1,1}, //channel 13
{0,1,1,1}, //channel 14
{1,1,1,1} //channel 15
};
//loop through the 4 sig
for(int j = 0; j < 4; j ++){
digitalWrite(controlPin[j], muxChannel[channel][j]);
}
//read the value at the SIG pin
int val = digitalRead(SIG_pin);
//return the value
return val;
}
I would like to mention that there was a cabling issue as well. I pulled down the signal pin with a 10k resistor.

Read an incoming HEX packet from Serial

I'm coding a on an arduino and I am communicating with an other device in HEX. I would like to know how to read the data he sends me.
I am sending a HEX packet (everything good here, no problem)
//Ask for Data
Serial.write(askData, sizeof(askData));
After this I will receive data (in HEX). I need to store it all to use it later. The only thing I know is that it will end with a "16". I dont know the length of the packet in advance.
Here is an example or packet that I can reveive :
68 4E 4E 68 08 09 72 90 90 85 45 68 50 49 06 19 00 00 00
0C 14 02 00 00 00 8C 10 12 35 02 00 00 0B 3B 00 00 00 8C
20 14 02 00 00 00 8C 30 14 00 00 00 00 04 6D 2F 09 61 24
4C 14 02 00 00 00 42 6C 5F 2C 42 EC 7E 7F 2C 0A 92 2A 00
10 0A 92 2B 00 10 39 16
Can someone help me please ?
This arduino example slightly modified:
/* reserve 200 bytes for the inputString:
* assuming a maximum of 200 bytes
*/
uint8_t inputString[200]; // a String to hold incoming data
int countInput = 0;
bool stringComplete = false; // whether the string is complete
void setup() {
// initialize serial:
Serial.begin(115200);
}
void loop() {
// print the string when 0x16 arrives:
if (stringComplete) {
for (int i=0; i<countInput; i++) {
Serial.print(inputString[i], HEX);
Serial.print(" ");
}
// clear the string:
countInput = 0;
stringComplete = false;
}
}
/*
SerialEvent occurs whenever a new data comes in the hardware serial RX. This
routine is run between each time loop() runs, so using delay inside loop can
delay response. Multiple bytes of data may be available.
*/
void serialEvent() {
while (Serial.available()) {
// get the new byte:
inputString[countInput] = (uint8_t)Serial.read();
// if the incoming character is '0x16', set a flag so the main loop can
// do something about it:
if (inputString[countInput] == 0x16) {
stringComplete = true;
}
countInput++;
}
}

TCPDF - Problem with Alphanumerical characters (wrong size)

I have a size problem when using TCPDF to generate QR code with only ALPHANUMERICAL characters. My objective: generate the longest URL (with a random part), but keeping the QR code at its lowest size, i.e. 21x21 modules (version1).
Documentation (QRcode.com) reports that using only alphanumerical characters set (thonky.com), URL can be 25 characters long with ERC set to L.
Using write2DBarCode with this 25 Alphanumerical URL leads to version1 (21x21mod) QR as expected
$pdf->write2DBarcode('HTTP://SITE-COM/123456789', 'QRCODE,L', 20, 20, 40, 40, $style, 'N');
but changing to this other URL, with also 25 Alphanumerical, I get a version 2 (25x25mod) QR code, whereas a version 1 could be done (Tested on Nayuki)
$pdf->write2DBarcode('HTTP://TXT-CH/AYAWEQYAF4A', 'QRCODE,L', 20, 70, 40, 40, $style, 'N');
I join the TCPDF Outputs of the 2 QR codes given as examples TCPDF Outputs
Thank you in advance for your help on this wonderful TCPDF library.
Short answer: The TCPDF software that you are using is suboptimal. It is generating a full 4-bit terminator even when a shorter one suffices. Please contact the software's authors to fix the problem. You can link them to this thread.
So I cropped your image into two QR Code images, and submitted them to ZXing Decoder Online and KaarPoSoft QR Decode with debug output.
ZXing, first barcode:
Decode Succeeded
Raw text HTTP://SITE-COM/123456789
Raw bytes 20 83 1a a6 5f 9f d5 b4 75 3e 8d 20 48 81 23 db 91 8a 80
Barcode format QR_CODE
Parsed Result Type URI
Parsed Result HTTP://SITE-COM/123456789
ZXing, second barcode:
Decode Succeeded
Raw text HTTP://TXT-CH/AYAWEQYAF4A
Raw bytes 20 cb 1a a6 5f 9f d6 5e ae 82 ca 0f 21 e2 52 18 11 53 94 00 ec 11 ec 11 ec 11 ec 11 ec 11 ec 11 ec 11
Barcode format QR_CODE
Parsed Result Type URI
Parsed Result HTTP://TXT-CH/AYAWEQYAF4A
KaarPoSoft, first barcode:
Debug output
skew_limit=7.21875
skew=0
left=31 right=427 top=27 bottom=423
size=397
matchVersion version=1 finder0=64 finder1=64 finder2=64
matchVersion version=1 timing0=1 timing1=1 alignment=1
matchVersion version=1 format_NW =14 0 format_NESW =14 1 format = 14 ecl = 1 mask = 6
matchVersion version=1 grades(F(V)TAF): 4444->4
findModuleSize matchVersion version=1 grade=4
findModuleSize version=1 grade=4 error_correction_level=1 mask=6
getCodewords mask=6 length=26
getCodewords = 32,131,26,166,95,159,213,180,117,62,141,32,72,129,35,219,145,138,128,62,191,105,157,147,176,164
setBlocks n_blocks_first=1 n_blocks_second=0 n_blocks=1 n_block_words_first=19 n_block_words_second=0 n_block_ec_words=7 total=26
setBlocks block 0 (26): 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25
RS calculateSyndroms: No errors
correctErrors in = 32,131,26,166,95,159,213,180,117,62,141,32,72,129,35,219,145,138,128,62,191,105,157,147,176,164
correctErrors out = 32,131,26,166,95,159,213,180,117,62,141,32,72,129,35,219,145,138,128
error_grade=4
extractData bytes in (19) = 32,131,26,166,95,159,213,180,117,62,141,32,72,129,35,219,145,138,128
extractData mode = 2
extractAlphanum charcount = 16
extractData mode = 1
extractNumeric charcount = 9
extractData mode = 0
extractData data(25) = 72,84,84,80,58,47,47,83,73,84,69,45,67,79,77,47,49,50,51,52,53,54,55,56,57
KaarPoSoft, second barcode:
Debug output
skew_limit=7.015625
skew=1
left=21 right=417 top=30 bottom=425
size=396.5
findModuleSize matchVersion version=1 grade=0
matchVersion version=2 finder0=64 finder1=64 finder2=64
matchVersion version=2 timing0=1 timing1=1 alignment=1
matchVersion version=2 format_NW =14 0 format_NESW =14 1 format = 14 ecl = 1 mask = 6
matchVersion version=2 grades(F(V)TAF): 4444->4
findModuleSize matchVersion version=2 grade=4
findModuleSize version=2 grade=4 error_correction_level=1 mask=6
getCodewords mask=6 length=44
getCodewords = 32,203,26,166,95,159,214,94,174,130,202,15,33,226,82,24,17,83,148,0,236,17,236,17,236,17,236,17,236,17,236,17,236,17,87,194,99,197,7,184,131,204,163,52
setBlocks n_blocks_first=1 n_blocks_second=0 n_blocks=1 n_block_words_first=34 n_block_words_second=0 n_block_ec_words=10 total=44
setBlocks block 0 (44): 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43
RS calculateSyndroms: No errors
correctErrors in = 32,203,26,166,95,159,214,94,174,130,202,15,33,226,82,24,17,83,148,0,236,17,236,17,236,17,236,17,236,17,236,17,236,17,87,194,99,197,7,184,131,204,163,52
correctErrors out = 32,203,26,166,95,159,214,94,174,130,202,15,33,226,82,24,17,83,148,0,236,17,236,17,236,17,236,17,236,17,236,17,236,17
error_grade=4
extractData bytes in (34) = 32,203,26,166,95,159,214,94,174,130,202,15,33,226,82,24,17,83,148,0,236,17,236,17,236,17,236,17,236,17,236,17,236,17
extractData mode = 2
extractAlphanum charcount = 25
extractData mode = 0
extractData data(25) = 72,84,84,80,58,47,47,84,88,84,45,67,72,47,65,89,65,87,69,81,89,65,70,52,65
Both QR Codes appear to have no problems regarding error correction or format violations.
From the KaarPoSoft output, we can see the segments in the QR Codes.
The first barcode has two segments:
Alphanumeric mode, count = 16, text = "HTTP://SITE-COM/". Segment bit length = 4 (mode) + 9 (count) + 88 (data) = 101 bits.
Numeric mode, count = 9, text = "123456789". Segment bit length = 4 (mode) + 10 (count) + 30 (data) = 44 bits.
The second barcode has one segment:
Alphanumeric mode, count = 25, text = "HTTP://TXT-CH/AYAWEQYAF4A". Segment bit length = 4 (mode) + 9 (count) + 138 (data) = 151 bits.
Now, a QR Code at version 1 with low error correction level has capacity for 19 data codeword bytes, or 152 bits. The first barcode uses 101 + 44 = 145 bits = 19 bytes (rounded up), so it fits. The second barcode uses 151 bits = 19 bytes (rounded up), so it fits. So in theory, both lists of segments of text data should fit in version 1 low ECC.
According to the QR spec, after the list of segments ends, these bits are appended:
(TERM) Up to four "0" bits (but less if the data capacity is reached) for the terminator pseudo-mode.
(BITPAD) Zero to seven "0" bits to fill up the last partial byte.
(BYTEPAD) Alternating bytes of 0xEC and 0x11 until the data capacity is reached.
Let's dissect what actually happened. Convert the ZXing hexadecimal byte output to binary, and annotate the fields.
First barcode:
20 83 1a a6 5f 9f d5 b4 75 3e 8d 20 48 81 23 db 91 8a 80
0010 000010000 [88 bits] 0001 0000001001 [30 bits] 0000 000 (Total length = 152 bits)
^Mode ^Count ^Data ^Mode ^Count ^Data ^TERM ^BITPAD
Second barcode:
20 cb 1a a6 5f 9f d6 5e ae 82 ca 0f 21 e2 52 18 11 53 94 00 ec 11 ec 11 ec 11 ec 11 ec 11 ec 11 ec 11
0010 000011001 [138 bits] | 0000 00000 11101100 00010001 [...] (Total length = 272 bits)
^Mode ^Count ^Data | ^TERM ^BITPAD ^BYTEPAD
Note that in the second barcode, at the position | just before the TERMinator, there are 151 bits on the left side. The terminator is normally four "0" bits, but is allowed to be shortened if the capacity (of 152 bits) is reached. So the optimal terminator is a single bit of "0", and then there must be no bit padding and no byte padding.

Sizeof(const char **) on 32 bit system

What's the value of the expression above? Is there any difference in running it in 32 or 64 bit (I'm on 32 bit)
Thank you
Value of expression Sizeof(const char **) is 8 on 64 bit machine and 4 and 32 bit machine.
Edit :
1 byte = 8 bits
64/8 = 8 bytes and 32/8 = 4 bytes.

Does Gray code exist for other bases than two?

Just a matter of curiosity, is the Gray code defined for bases other than base two?
I tried to count in base 3, writing consecutive values paying attention to change only one trit at a time. I've been able to enumerate all the values up to 26 (3**3-1) and it seems to work.
000 122 200
001 121 201
002 120 202
012 110 212
011 111 211
010 112 210
020 102 220
021 101 221
022 100 222
The only issue I can see, is that all three trits change when looping back to zero. But this is only true for odd bases. When using even bases looping back to zero would only change a single digit, as in binary.
I even guess it can be extended to other bases, even decimal. This could lead to another ordering when counting in base ten ... :-)
0 1 2 3 4 5 6 7 8 9 19 18 17 16 15 14 13 12 11 10
20 21 22 23 24 25 26 27 28 29 39 38 37 36 35 34 33 32 31 30
Now the question, has anyone ever heard of it? Is there an application for it? Or it is just mathematical frenzy?
Yes. Have a look at the Gray code article at wikipedia. It has a section on n-ary Gray Code.
There are many specialized types of Gray codes other than the binary-reflected Gray code. One such type of Gray code is the n-ary Gray code, also known as a non-Boolean Gray code. As the name implies, this type of Gray code uses non-Boolean values in its encodings.
Just for completeness (as aioobe already gave the right answer), here's a C++ program that lists all the 168 2-digit gray codes for base 3 that start with 00 and marks the 96 cyclic ones. Using the algorithm from Wikipedia, you can construct longer Gray codes easily for even bases. For uneven bases, you can change the program to generate according Gray codes.
The first cyclic 2-digit gray code found with this program is this one:
00 01 02 12 10 11 21 22 20
After changing the program, the first cyclic 3-digit gray found is this:
000 001 002 012 010 011 021 020 022 122 102 100 101 111
110 112 212 202 222 220 120 121 221 201 211 210 200
Code:
#include <stdio.h>
#include <stdlib.h>
// Highest number using two trits
#define MAXN 9
int gray_code_count, cyclic_count;
bool changes_one_trit(int code1, int code2) {
int trits_changed = 0;
if ((code1 / 3) != (code2 / 3)) trits_changed++;
if ((code1 % 3) != (code2 % 3)) trits_changed++;
return (trits_changed == 1);
}
int generate_gray_code(int* code, int depth) {
bool already_used;
if (depth == MAXN) {
for (int i = 0; i < MAXN; i++) {
printf("%i%i ", code[i]/3, code[i]%3);
}
// check if cyclic
if (changes_one_trit(code[MAXN-1], 0)) {
printf("cyclic");
cyclic_count++;
}
printf("\n");
gray_code_count++;
}
// Iterate through the codes that only change one trit
for (int i = 0; i < MAXN; i++) {
// Check if it was used already
already_used = false;
for (int j = 0; j < depth; j++) {
if (code[j] == i) already_used = true;
}
if (already_used) continue;
if (changes_one_trit(code[depth-1], i)) {
code[depth] = i;
generate_gray_code(code, depth + 1);
}
}
}
int main() {
int* code = (int*)malloc(MAXN * sizeof(int));
code[0] = 0;
gray_code_count = 0;
generate_gray_code(code, 1);
printf("%i gray codes found, %i of them are cyclic\n", gray_code_count, cyclic_count);
free(code);
}

Resources