Sorry, I did not try to compile… I figured you grabbed that from the wiring page and it should have worked. I updated my example above after testing on a Core.
Yes the onBoard blue is blinking thanks. I guess that it can sense, I just need to know how to notify other cores about it any ideas are welcome.
Thanks one more time
Meth
Hi,
this worked for me (a long, long time back ) :
adxl335.h:
#ifndef __ADXL335_H__
#include <spark_wiring.h>
#define __ADXL335_H__
#define ADC_ref 5.0
#define analog_resolution 1024.0
#define zero_x 1.60156
#define zero_y 1.60156
#define zero_z 1.64551
#define sensitivity_x 0.33
#define sensitivity_y 0.33
#define sensitivity_z 0.31
#define PI 3.14159265
#define HALF_PI 1.57079
#define TWO_PI 6.283185
#define DEG_TO_RAD 0.01745329
#define RAD_TO_DEG 57.2957786
class ADXL335 {
public:
ADXL335(unsigned xPin=A0, unsigned yPin=A1, unsigned zPin=A2, float adcRef=ADC_ref);
/**
* call this before getting sensor values.
*/
void update();
float getX();
float getY();
float getZ();
float getXDeg();
float getYDeg();
float getZDeg();
private:
unsigned int xPin;
unsigned int yPin;
unsigned int zPin;
float adcRef;
unsigned int value_x;
unsigned int value_y;
unsigned int value_z;
float xv;
float yv;
float zv;
float angle_x;
float angle_y;
float angle_z;
};
#endif
adxl335.cpp:
#include "adxl335.h"
#include <math.h>
ADXL335::ADXL335(unsigned xP, unsigned yP, unsigned zP, float ref){
xPin=xP;
yPin=yP;
zPin=zP;
adcRef=ref;
adcRef=4096;
}
void ADXL335::update() {
value_x = analogRead(xPin);
value_y = analogRead(yPin);
value_z = analogRead(zPin);
xv = (value_x/analog_resolution*adcRef-zero_x)/sensitivity_x;
yv = (value_y/analog_resolution*adcRef-zero_y)/sensitivity_y;
zv = (value_z/analog_resolution*adcRef-zero_z)/sensitivity_z;
angle_x =atan2(-yv,-zv)*RAD_TO_DEG;
angle_y =atan2(-xv,-zv)*RAD_TO_DEG;
angle_z =atan2(-yv,-xv)*RAD_TO_DEG;
}
float ADXL335::getXDeg(){
return angle_x;
}
float ADXL335::getYDeg(){
return angle_y;
}
float ADXL335::getZDeg(){
return angle_z;
}
float ADXL335::getX(){
return xv;
}
float ADXL335::getY(){
return yv;
}
float ADXL335::getZ(){
return zv;
}
test:
// This #include statement was automatically added by the Spark IDE.
#include "adxl335.h"
#include <math.h>
// This #include statement was automatically added by the Spark IDE.
int minVal = 570;
int maxVal = 661;
int xPin=A0;
int yPin=A1;
int zPin=A2;
double xd1;
double yd1;
double zd1;
void setup() {
pinMode(A0, INPUT);
pinMode(A1, INPUT);
pinMode(A2, INPUT);
/*
pinMode(A0,INPUT);
pinMode(A1,INPUT);
pinMode(A2,INPUT);
*/
Spark.variable("xd1",&xd1,DOUBLE);
Spark.variable("yd1",&yd1,DOUBLE);
Spark.variable("zd1",&zd1,DOUBLE);
Serial.begin(9600);
}
void loop() {
//read the analog values from the accelerometer
double xRead = analogRead(xPin);
double yRead = analogRead(yPin);
double zRead = analogRead(zPin);
xRead/=4;
//xRead/=4095;
yRead/=4;
//yRead/=4095;
zRead/=4;
//zRead/=4095;
//convert read values to degrees -90 to 90 - Needed for atan2
double xAng = map(xRead, minVal, maxVal, -90, 90);
double yAng = map(yRead, minVal, maxVal, -90, 90);
double zAng = map(zRead, minVal, maxVal, -90, 90);
//Caculate 360deg values like so: atan2(-yAng, -zAng)
//atan2 outputs the value of -π to π (radians)
//We are then converting the radians to degrees
xd1 = RAD_TO_DEG * (atan2(-yAng, -zAng) + PI);
yd1 = RAD_TO_DEG * (atan2(-xAng, -zAng) + PI);
zd1 = RAD_TO_DEG * (atan2(-yAng, -xAng) + PI);
Serial.print("x(");
Serial.print(xRead);
Serial.print("): ");
Serial.print(xd1);
Serial.print(" y(");
Serial.print(yRead);
Serial.print("): ");
Serial.print(yd1);
Serial.print(" z(");
Serial.print(zRead);
Serial.print("): ");
Serial.println(zd1);
delay(3000);
}
Hope this helps.
Thanks,
/N
Hi NaAl,
Thanks for the input. I think the code I can use is the one in the test.Because in my case I just need some readings from the accelerometer which is used to trigger/notify other cores, Just some clearance for the Spark.variable("xd1",&xd1,DOUBLE);
can you give more explanation about it.
Thanks Meth
I thought that I could read or see the data in the WEB IDE but found out that serial communication only communicates via USB, so there isn’t any way I can see the data on the cloud?
Thanks Meth
Hi @Meth
Please have a look at the tutorial section of the forum. You can see your values on a remote web page anywhere in the world via Spark.variable() or Spark.publish very simply and easily. You can control your Spark via Spark.function() too.
Something wrong with analog input of my spark core.
Core connected only to laptop by USB cable.
Write simple sketch:
void setup() {
Serial.begin(9600);
pinMode(A7, INPUT_PULLUP); }
void loop() {
Serial.println(analogRead(A7));
delay(2000);
}
And serial monitor show
I expect, that A7 pin connected to +3.3V by the pull up 40kOm resistor show values around 4095?
Something wrong with logic in my head or my spark core?
Hi @Godz
The input impedance of the A to D on the Spark core is much lower than on say an Arduino.
Try a 4k resistor instead of a 40k.
I could be terribly mistaken, but aren’t you missing some pin declarations?
void setup()
{
Serial.begin(9600); // sets the serial port to 9600
}
Something along the lines of this: http://docs.spark.io/firmware/#setup-pinmode ?
Try a 4k resistor instead of a 40k.
I use integrated pull_up resistor. It have 40kOm resistance by Spark Core docs
void setup() {
Serial.begin(9600);
pinMode(A7, INPUT_PULLUP); }
No, I got it with input pull up resistor actavation. pinMode(A7, INPUT_PULLUP)
I would guess, haven’t looked, but after you set the input to INPUT_PULLUP and then call analogRead() it will change the input to AN_INPUT and there won’t be a pull up anymore. When I first saw your numbers I thought, hey… his input is floating I typically hangs out in the middle when it floats (high impedance).
This code should give you the same results:
void setup() {
Serial.begin(9600);
}
void loop() {
Serial.println(analogRead(A7));
delay(2000);
}
Cool. I think you write. Can you see in source code - for 100% confidence. I will correct firmware description in "documents" on github and make pull request.
Yep it’s here:
https://github.com/spark/firmware/blob/master/src/spark_wiring.cpp#L415-L420
Also if you do this the opposite way around and do an analogRead() and then configure a digitalRead() it will not perform the digitalRead()
https://github.com/spark/firmware/blob/master/src/spark_wiring.cpp#L221-L228
Thanks a lot. https://github.com/spark/docs/pull/244
@BDub Why does doing an analogRead() on an input set for PULL_UP cause it to not have the pull-up anymore?
Also does this mean that analogRead() will cause floating values? A capacitor would fix that, correct?
When you perform an analogRead(), the firmware will change the mode of the pin to connect it to the ADC. That means it can no longer be connected internally to the pull up resistor.
Code here:
https://github.com/spark/firmware/blob/master/src/spark_wiring.cpp#L415-L420
The ADC is a effectively a high impedance input that expects you to hook a low impedance circuit up to it. High impedance means it will not add any kind of a significant load to your analog sensor output or circuitry. However, the way the ADC (Analog to Digital Convertor) works requires you to provide a voltage at it’s input that is also Low Impedance, or it will not convert the value properly. How to calculate impedance almost needs a class on electronics to explain properly, so I’ll let you google that if you really want to dig in. One way to force a sensor that is a higher impedance to “appear” to be a low impedance is to simple add a capacitor in parallel with the ADC input and ground. In my testing on the Core, a 0.01uF cap works well for most circuits. Normally the standard cap you would “sprinkle” around your ADC inputs is a 0.1uF cap, but specifically that is too much capacitance for the TMP36 temperature sensor that we include in the Maker Kit… so it’s all about the 0.01uF caps now
Sorry for reviving this thread, but why is the firmware doing this? I have a Core which uses the internal pulldown in combination with analogRead()
, and that setup works perfectly fine (i.e. more than accurate enough for my needs). This does not work with the Photon on the latest firmware. Is that a limitation of the Photon's processor or a limitation in the firmware?