Hi guys, I am pretty new in the programming , but I will try my best to explain what I want to achieve with my code and scheme.
I am using the code from a few tutorials + adding some of my own. My program should count steps when accelerometer is moving and LED should come up for each step taken (up to 10 LED’s). The step counting program running OK, and the LED bar working fine on their on, but the combined code is wrong and I cannot figure out where. I am positive I did some mistakes there - I am learning Arduino for a month or so.
I see that you are declaring 3 100 element float arrays as global, then in loop() 5 more 100 element float arrays that are local to loop().
How much SRAM do you have? Uno has 2.0K SRAM, your arrays take 3.2K SRAM.
Hi! Oh, I thought it uses 1GB from 2GB. My Arduino compiler says “Global variables use 1,032 bytes (50%) of dynamic memory, leaving 1,016 bytes for local variables. Maximum is 2,048 bytes.”
I hope it is true. Because the step counting program uses the same and works fine.
And I think most likely something wrong with this part (or with setup) :
int sensorReading = digitalRead(steps);
int ledLevel = map(sensorReading, 0, 1023, 0, ledCount);
if (steps < ledLevel) {
digitalWrite(ledPins[steps], HIGH);
}
else {
digitalWrite(ledPins[steps], LOW);
}
Yes, your are right, it is KB , not GB. Somehow I skipped MB and KB. Sorry. But rams is not the case here as I understand, it's quiet simple program - basically made by combining two tutorials.
Memory on the Arduino is not measured in GB.
Memory on the Arduino is not measured in MB.
Memory on the Arduino is not measured in KB.
Memory on the Arduino IS measured in bytes.
But rams is not the case here
You have yet to prove that. 300 floats is 1200 bytes. That's 60% of the memory on a UNO.
//first calibrate on powering up the module
//a poscal function to calculate avg values of x , y , z and print them on serial
//turn on the LED on pin13 and turn it off after the calibration is completed
//in loop continually read the data and calculate total accerelation vector
//call stepdetect function
//print total number of steps on monitor
//if step occurs flash led 3 times
const int xpin=A2;
int ypin=A3;
int zpin=A4;
int powerpin=A0;
int gnd=A1;
float threshhold=80.0;
float xval[100]={0};
float yval[100]={0};
float zval[100]={0};
float xavg;
float yavg;
float zavg;
int steps,flag=0;
void setup()
{
Serial.begin(9600);
pinMode(powerpin,OUTPUT);
pinMode(gnd,OUTPUT);
digitalWrite(powerpin,HIGH);
digitalWrite(gnd,LOW);
pinMode(13,OUTPUT);
calibrate();
}
void loop()
{
int acc=0;
float totvect[100]={0};
float totave[100]={0};
//float sum1,sum2,sum3=0;
float xaccl[100]={0};
float yaccl[100]={0};
float zaccl[100]={0};
// float x,y,z;
for (int i=0;i<100;i++)
{
xaccl[i]=float(analogRead(xpin));
delay(1);
//delay(100);
//x=sum1/100.0;
//Serial.println(xavg);
yaccl[i]=float(analogRead(ypin));
delay(1);
//sum2=yaccl[i]+sum2;
//y=sum2/100.0;
//Serial.println(yavg);
//delay(100);
zaccl[i]=float(analogRead(zpin));
delay(1);
//sum3=zaccl[i]+sum3;
//z=sum3/100;
totvect[i] = sqrt(((xaccl[i]-xavg)* (xaccl[i]-xavg))+ ((yaccl[i] - yavg)*(yaccl[i] - yavg)) + ((zval[i] - zavg)*(zval[i] - zavg)));
totave[i] = (totvect[i] + totvect[i-1]) / 2 ;
//acc=acc+totave[i];
Serial.println(totave[i]);
delay(200);
//cal steps
if (totave[i]>threshhold && flag==0)
{
steps=steps+1;
flag=1;
}
else if (totave[i] > threshhold && flag==1)
{
//do nothing
}
if (totave[i] <threshhold && flag==1)
{flag=0;}
Serial.println('\n');
Serial.print("steps=");
Serial.println(steps);
}
//float tim=acc/100;
//Serial.println(tim);
delay(1000);
// stepcal(totave);
}
/*void stepcal(float arr[100])
{
// int threshhold=80;
float jack=0;
//delay(100)
//Serial.println(xavg);
for (int i=0;i<100;i++)
{
jack=jack+arr[i];
}
float m=jack/100;
//Serial.println(yavg);
//delay(100);
//detect pushups or count for number of dumbles
//crossing the threshold and
//cross 20 in down and 80 in up
/*add one to counter and set a flag high to indicate it is above a threshhold value
if a flag is high and threshhold is crossed :do nothing }
if signal falls below threshhold set flag to -1 indicating it is blow threshhold
if (m>threshhold && flag==0)
{
steps=steps+1;
flag=1;
Serial.println('\n');
Serial.print("steps=");
Serial.println(steps);
}
else if (m > threshhold && flag==1)
{
//do nothing
}
if (m <threshhold && flag==1)
{flag=0;}
Serial.println('\n');
Serial.print("steps=");
Serial.println(steps);
} */
//calculate total accerelation vector
void calibrate()
{
digitalWrite(13,HIGH);
float sum=0;
float sum1=0;
float sum2=0;
for (int i=0;i<100;i++)
{
xval[i]=float(analogRead(xpin));
sum=xval[i]+sum;
}
delay(100);
xavg=sum/100.0;
Serial.println(xavg);
for (int j=0;j<100;j++)
{
xval[j]=float(analogRead(xpin));
sum1=xval[j]+sum1;
}
yavg=sum1/100.0;
Serial.println(yavg);
delay(100);
for (int i=0;i<100;i++)
{
zval[i]=float(analogRead(zpin));
sum2=zval[i]+sum2;
}
zavg=sum2/100.0;
delay(100);
Serial.println(zavg);
digitalWrite(13,LOW);
}
And I just not sure if I connected the wires correctly.
The ADXL 345 accelerometer is interfaced with either SPI or I2C communication. So, no your wiring is not right. Vcc goes to 5V or 3.3V (depending on the exact module that you have). GND goes to one of the ground pins. The other pins depend on which interface you choose. If I2C, SDA goes to A4 (analog input 4) and SCL goes to A5 with pullups on SCL and SDA. If SPI, SCL to Uno pin 13, SDA to pin 11, SDO to pin 12, and CS to (usually) pin 10. Your code is for an analog output Accelerometer so will not work with ADXL 345.
I suppose that if you want to power the accelerometer on and off during the program you could power it from those pins (and the sensor draws less than 20mA). If you don't need to control its power, it seems needlessly complicated.
if you want to follow the Instructable you need the ADXL 335 Triple Axis Accelerometer (analog) and wire per the instructions.
Hi guys. I just got new analog MMA7361 accelerometer (it was impossible to get ADXL335). But it is not showing right values. How can I test it to know if it is working well?
Ahoy! Accelerometer is working! But my code - not completely. I have a hard time understanding how mapping LED’s works without the potentiometer. I have 10 LEDs (ledCount), and for the test I am mapping the steps - one LED should light up each time as step happens.
int ledLevel = map(steps, 0, 10, 0, ledCount);
// loop over the LED array:
for (int thisLed = 0; thisLed < ledCount; thisLed++) {
// if the array element's index is less than ledLevel,
// turn the pin for this element on:
if (thisLed < ledLevel) {
digitalWrite(ledPins[thisLed], HIGH);
}
// turn off all pins higher than the ledLevel:
else {
digitalWrite(ledPins[thisLed], LOW);
}
}
Now LEDs is not lighting up at all when running Serial Monitor, but since I close it - LEDs getting on! But in a strange way: e.g. it doesn’t light up 5,6th. Or 9th LED.
Thanks.
Hello
I am sorry I am not going to answer your question but I need to know if the instructables code works for you? because it doesn't want to work for me.
I have a 335 accelerometers exactly like the tutorial but I don't know why it increments steps without moving the accelermeter from the table
Can anyone please help me with understanding the following lines used in the code in the first question?
totvect = sqrt(((xaccl-xavg)* (xaccl-xavg))+ ((yaccl - yavg)(yaccl - yavg)) + ((zval _- zavg)(zval *- zavg))); * //…why zval is being used here…why not zaccl totave = (totvect + totvect[i-1]) / 2 ; //…what is the need for this formula… Thanks in advance_
Can someone help me understand these lines in the code in the first post here?
totvect = sqrt(((xaccl-xavg)* (xaccl-xavg))+ ((yaccl - yavg)(yaccl - yavg)) + ((zval _- zavg)(zval - zavg))); //…why is it written zval here instead of zaccl…whereas xaccl and yaccl are being used…and what does this formula do ? totave = (totvect + totvect[i-1]) / 2 ; //…what is the need for this ? Thanks in advance._
Can someone help me understand these lines in the code in the first post here?
What part don't you understand? Two x values are multiplied. Two y values are multiplied. Two z values are multiplied. The results are added, and the square root of the total is determined.