Sum calculation error in Arduino Mega 2560 - arduino

I am using Arduino Mega 2560 to communicate with the server.
I create a byte array, uses the first digit as a indicator (to tell the server this message is from a arduino device) and the last digit for check sum.
// for creating msg
void createmsg(){
int index = 0;
memset(MSGpack,0,sizeof(MSGpack));
byte sum;
MSGpack[0] = 0x23; // for identifing it is arduino
// for current readings
index = 14;
for (int i = 0; i < 7; i++){
float voltage = readcurrent(i);
injectByte(voltage, index);
index = index + 4;
}
////////////////////////////////////////////////////////////DATE
myRTC.updateTime();
index = 162;
int timeVAR = myRTC.dayofmonth;//reporting day
injectByte(timeVAR, index);
timeVAR = myRTC.month;
injectByte(timeVAR, 166); //reporting month
timeVAR = myRTC.year;
injectByte(timeVAR, 170); //reporting year
timeVAR = myRTC.year + myRTC.month + myRTC.dayofmonth; //sum of date
injectByte(timeVAR, 158);
////////////////////////////////////////////////////////////DATE
////////////////////////////////////////////////////////////TIME
myRTC.updateTime();
timeVAR = myRTC.hours;
injectByte(timeVAR, 146); //reporting hour
timeVAR = myRTC.minutes;
injectByte(timeVAR, 150); //reporting second
timeVAR = myRTC.hours + myRTC.minutes;
injectByte(timeVAR, 154); //sum of time
////////////////////////////////////////////////////////////TIME
//to pass buffer verification
for (int i = 0; i < 186; i++) {
sum += MSGpack[i];
}
MSGpack[186] = sum;
}
void injectByte(float value, int index){
byte * b = (byte *) &value;
MSGpack[index] = b[3];
MSGpack[index + 1] = b[2];
MSGpack[index + 2] = b[1];
MSGpack[index + 3] = b[0];
}
At the server side, it checks if the last digit equals to the sum of all the previous digit, if yes, it identify the received package is valid.
The problems is, if I comment out the date and time data, the server could identify the package as valid. But if I add the data back into the package, the server says the package is not valid.
So I conclude it is check sum error at the Arduino side.
According to here, "some constant calculations may overflow" && "Know at what point your variable will "roll over" " etc at "Programming tips:"
A byte stores an 8-bit unsigned number, from 0 to 255. So what if the check sum calculated is larger than 255? What will be resulted?
And how should I solve this issue and let the server receive a valid package? Thanks!

I would suggest changing your byte array to being an unsigned byte array
then change sum to an unsigned int or unsigned short (16 bits will suffice)
Where you calculate the sum:
sum = (sum + MSGpack[i]) & 0xFF;
MSGpack[186] = (unsigned byte)sum;
I think the issue is that you are adding signed numbers together and also not limiting the output to 8 bits. As you are using the bytes as unsigned, it's best to tell the compiler explicitly so it doesn't make rash assumptions.

Related

Arduino is giving a weird output when using toInt()

I'm trying to convert a string to an integer (which is actually a binary number) so that I can output the DEC value, but where the answer SHOULD be 63 (00111111), it's giving me -19961 as an output? It would be great if someone can help me correctly convert the string to an int :)
// C++ code
//
const int button = 13;
int buttonPressed = 0;
int counter = 0;
int myInts[] = {0, 0, 1, 1, 1, 1, 1, 1};
void setup()
{
Serial.begin(9600);
pinMode(button, INPUT);
}
void loop()
{
buttonPressed = digitalRead(button);
if (buttonPressed == HIGH){
counter = count();
//rial.println(counter);
}
else{
Serial.println("Button not pressed");
}
delay(1000);
}
int count()
{
String myString = ""; //Empty string for constructing
int add = 0;
int i = 0;
//Should add all the values from myInts[] into a string
while(i < 8){
myString = String(myString + myInts[i]);
Serial.println(add);
delay(1000);
add++;
i++;
}
Serial.println(myString);
int myNumber = myString.toInt(); //Convert the string to int
Serial.println(myNumber, DEC); //Should print 63 in this case
return add;
}
Your code currently does the following:
Concatenates all your integers together to make a string "00111111"
Attempts to convert it (as a string holding a base 10 integer) to the integer 111,111 (one hundred eleven thousand, one hundred eleven)
Hits integer overflow. The range of an Arduino int is -32768 to 32767 (65536 possible values), so the number you really have is 111111 - 2*65536 = -19961.
I don't believe that there's an overload of Arduino's ToInt that converts a binary string to an integer. Depending on the C++ support in Arduino, you may be able to use std::stoi as described here.
Instead, you may choose to do the conversion yourself - you can keep track of a number sum, and at each loop iteration, double it and then add the next bit:
int sum = 0;
for(int i = 0; i < 8; i++) {
sum *= 2;
sum += myInts[i];
// print and delay if you want
}
Over the eight iterations, sum ought to have values 0, 0, 1, 3, 7, 15, 31, and finally 63

Arduino - How to convert double to HEX format

I have an arudino code where I get some temperature reading:
double c1 = device.readCelsius();
Serial.println(c1);
The output is for example: 26.23
What I need is to get this converted to 2623 and then to HEX value so I get: 0x0A3F
Any clue?
I guess your float values always get numbers up to two decimal. So, you can just multiply the value which you read from sensor with a 100.
decimalValue = 100 * c1
And then you can use this small code for converting the decimal value to HEX.
Thanks to GeeksforGeeks
You can find the full tutorial here
// C++ program to convert a decimal
// number to hexadecimal number
#include <iostream>
using namespace std;
// function to convert decimal to hexadecimal
void decToHexa(int n)
{
// char array to store hexadecimal number
char hexaDeciNum[100];
// counter for hexadecimal number array
int i = 0;
while (n != 0) {
// temporary variable to store remainder
int temp = 0;
// storing remainder in temp variable.
temp = n % 16;
// check if temp < 10
if (temp < 10) {
hexaDeciNum[i] = temp + 48;
i++;
}
else {
hexaDeciNum[i] = temp + 55;
i++;
}
n = n / 16;
}
// printing hexadecimal number array in reverse order
for (int j = i - 1; j >= 0; j--)
cout << hexaDeciNum[j];
}
// Driver program to test above function
int main()
{
int n = 2545;
decToHexa(n);
return 0;
}

Arduino read 20 bit number from 3 registers

I have an ADXL355 (EVAL-ADXL355-PMDZ) that I am trying to test against a very expensive industrial grade sensor. I am using I2C and I am able to read the device properties and settings as described in the datasheet.
The issue I'm having is how to read the 3 ZDATA (or XDATA, YDATA) registers as a single value. I have tried two approaches. Here is the first:
double values[3];
Wire.beginTransmission(addr);
Wire.write(0x08); // ACCEL_XAXIS
Wire.endTransmission();
Wire.requestFrom(addr, 9, true); // Read 9, 3 for each axis
byte x1, x2, x3;
for (int i = 0; i < 3; ++i){
x3 = Wire.read();
x2 = Wire.read();
x1 = Wire.read();
unsigned long tempV = 0;
unsigned long value = 0;
value = x3;
value <<= 12;
tempV = x2;
tempV <<= 4;
value |= tempV;
tempV = x1;
tempV >>= 4;
value |= tempV;
values[i] = SCALEFACTOR * value;
}
This will produce values that approach 1g for negative gravity and 3g for positive gravity. Also the unloaded axes will sometimes show offscale high instead of -0.0g. They bounce from 0.0 to 4.0 g's. This tells me I have a sign problem which I'm sure comes from using unsigned long. So I attempted to read it as a 16 bit value and retain the sign.
double values[3];
Wire.beginTransmission(addr);
Wire.write(0x08); // ACCEL_XAXIS
Wire.endTransmission();
Wire.requestFrom(addr, 9, true); // Read 9, 3 for each axis
byte x1, x2, x3;
for (int i = 0; i < 3; ++i){
x3 = Wire.read();
x2 = Wire.read();
x1 = Wire.read();
long tempV = 0;
long value = 0;
value = x3;
value <<= 8;
tempV = x2;
value |= tempV;
values[i] = SCALEFACTOR * value;
}
This produced values are good in terms of sign but they are (as expected) much lower in magnitude than they are supposed to be. I tried to create a 20 bit number like this long value:20; but I received
expected initializer before ':' token
same error for int.
How do I properly read from 3 registers to obtain a correct 20 bit value?
First of all, you really want to use unsigned types when using the left and right shift operators (see this question).
Taking a look to the avr-gcc type layout we learn that long are represented on 4 bytes (i.e. 32 bits) so they are long enough (no pun intended) to "hold" your 20 bits numbers (XDATA, YDATA, and ZDATA). On the other hand, int are represented on 2 bytes (i.e. 16 bits) and thus should not be used in your case.
According to the datasheet you linked page 33, the numbers are formatted as two's complement. Your first example correctly set the last 20 bits of your unsigned, 32 bits long value (in particular the left justification handling — right-shifting x1 by four — already looks correct) but the "new" 12 most significants bits are always set to 0.
To perform sign extension, you need to set the "new" 12 most significant bits to 0 if the number is a positive value, 1 if the number is a negative value (adaptation of your first example):
...
value |= tempV;
if (x3 & 0x80) /* msb is 1 so the number is a negative value */
value |= 0xFFF00000;
From there, what you should observe is about the same behaviour as previously: high positive values instead of small negative ones (but even higher than previously). This is caused by the fact that while your value is correct bitwise speaking, it is still intepreted as unsigned. This can be worked around by forcing the compiler to use value as signed:
values[i] = SCALEFACTOR * (long)value;
And now it should be working.
Note that this answer use the fact that your C/C++ implementation use two's complement to represent negative integers. While very rare in practice, the standard allow other representations (see this question for examples).
Here is one way to make it work. It does use bitshifting on a signed value. Various sources have said that this is a potential bug as it is implementation defined. It worked on my platform.
typedef union {
byte bytes[3];
long value:24;
} accelData;
double values[3];
Wire.beginTransmission(addr);
Wire.write(0x08); // ACCEL_XAXIS
Wire.endTransmission();
Wire.requestFrom(addr, 9, true); // Read 9, 3 for each axis
accelData raw;
for (int i = 0; i < 3; ++i){
raw.bytes[2] = Wire.read();
raw.bytes[1] = Wire.read();
raw.bytes[0] = Wire.read();
long temp = raw.value >> 4;
values[i] = SCALEFACTOR * (double)temp;
}
I prefer the solution presented by Alexandre Perrin.
union
{
struct
{
unsigned : 4;
unsigned long uvalue : 20;
};
struct
{
unsigned : 4;
signed long ivalue : 20;
};
unsigned char rawdata[3];
}raw;
for (int i = 0; i < 3; ++i){
raw.bytes[2] = Wire.read(); //if most significant part is transfered first
raw.bytes[1] = Wire.read();
raw.bytes[0] = Wire.read();
values[i] = SCALEFACTOR * (double)raw.ivalue;
}

Appropriate sample for PIC ADC after converting from analog voltage.

if I'm reading an analog signal from my pressure sensor at 500mSec. my instructor told me that you should make the ADC Timr0 interrupt double what you are reading from analog Oscilloscope (500mSec.).i.e. 2fc. My code is down below.
Should I configure my timer0 to be 20Hz or less or more?
enter code here
char temp[5];
unsigned int adc_value;
char uart_rd;
int i;
unsigned int d[10]={0};
int average = 0;
int counter =0;
void interrupt(){
if (INTCON.T0IF) {
INTCON.T0IF = 0 ;// clear T0IF (Timer interrupt flag).
}
TMR0 = 178;
}
void main() {
temp[0]='1';
temp[1]='2';
temp[2]='3';
temp[3]='4';
temp[4]=' ';
OSCCON= 0x77; //8MHz
ANSEL = 0b00000100; //ANS2
CMCON0 = 0X07; //
TRISA = 0b00001100;
UART1_Init(9600);
TMR0 = 178 ;
//CMCON0 = 0X04; // turn off compartor.
OPTION_REG = 0x87; //
INTCON =0xA0;
while(1){
average= ADC_Read(2);
temp[0] = average/1000+48;
temp[1] = (average/100)%10+48;
temp[2] = (average/10)%10+48;
temp[3] = average%10+48;
for (i=0;i<5; i++)
{
UART1_Write(temp[i]);
}
}
}
When preform sampling on a signal you are not capturing all of is information but only parts of it with a given sampling period.
The Nyquist–Shannon sampling theorem claims that if you can actual sample at above of some given frequency you can get all the information of a finite bandwidth of the signal. This frequency is twice the maximum frequency of that bandwidth.
If you don't do comply with that frequency you will suffer from an effect called aliasing.
You can learn more about here: https://en.wikipedia.org/wiki/Aliasing

why fundamental Frequency and magnitude are not null when microphone is off?

I would like to make real time audio processing with Qt and display the spectrum using FFTW3.
What I've done in steps:
I capture any sound from computer device and fill it into the buffer.
I assign sound samples to double array
I compute the fundamental frequency.
when I'm display the fundamental frequency and Magnetitude when the microphone is on but no signal(silence) , the fundamental frequency is not what I expected , the code don't always return zero , sometimes the code returns 1500Hz,2000hz as frequency
and when the microphone is off (mute) the code don't return zero as fundamamental frequency but returns a number between 0 and 9000Hz. Any help woulbd be appreciated
here is my code
QByteArray *buffer;
QAudioInput *audioInput;
audioInput = new QAudioInput(format, this);
//Check the number of samples in input buffer
qint64 len = audioInput->bytesReady();
//Limit sample size
if(len > 4096)
len = 4096;
//Read sound samples from input device to buffer
qint64 l = input->read(buffer.data(), len);
int input_size= BufferSize;
int output_size = input_size; //input_size/2+1;
fftw_plan p3;
double in[output_size];
fftw_complex out[output_size];
short *outdata = (short*)m_buffer.data();// assign sample into short array
int data_size = size_t(outdata);
int data_size1 = sizeof(outdata);
int count = 0;
double w = 0;
for(int i(chanelNumber); i < output_size/2; i= i + 2) //fill array in
{
w= 0.5 * (1 - cos(2*M_PI*i/output_size)); // Hann Windows
double x = 0;
if(i < data_size){
x = outdata[i];
}
if(count < output_size){
in[count] = x;// fill Array In with sample from buffer
count++;
}
}
for(int i=count; i<output_size; i++){
in[i] = 0;
}
p3 = fftw_plan_dft_r2c_1d(output_size, in, out, FFTW_ESTIMATE);// create Plan
fftw_execute(p3);// FFT
for (int i = 0; i < (output_size/2); i++) {
long peak=0;
double Amplitudemax=0;
double r1 = out[i][0] * out[i][0];
double im1 = out[i][3] * out[i][4];
double t1 = r1 + im1;
//double t = 20*log(sqrt(t1));
double t = sqrt(t1)/(double)(output_size/2);
double f = (double)i*8000 / ((double)output_size/2);
if(Magnitude > AmplitudeMax)
{
AmplitudeMax = Magnitude;
Peak =2* i;
}
}
fftw_destroy_plan(p3);
return Peak*(static_cast<double>(8000)/output_Size);
What you think is silence might contain some small amount of noise. The FFT of random noise will also appear random, and thus have a random magnitude peak. But it is possible that noise might come from equipment or electronics in the environment (fans, flyback transformers, etc.), or the power supply to your ADC or mic, thus showing some frequency biases.
If the noise level is low enough, normally one checks the level of the magnitude peak, compares it against a threshold, and cuts off frequency estimation reporting below this threshold.

Resources