I also tried hooking up the acclerometer and getting values with the adxl3XX sketch, for x-axis it reads 414 to 630. I tried mapping that and still doesn't work.
there is a number in the datasheet showing ratiometric 270 to 330
You said that the range of values you got was from 414 to 630, and that you got 519 when the accelerometer was laying flat on a table.So, why are you mapping from 414 to only 519? Your map from range should be from 414 to 630.You can use Serial.begin() in setup, and Serial.print() and/or Serial.println() in loop() to see what value was read from the accelerometer and what value is returned by the map function.I notice though, that the code that prints the values has a lot more pins defined and has more pinMode statements.Why would you switch pins between scripts? What happened to powering and ground the accelerometer?
The circuit:analog 0: accelerometer self test
I checked the datasheet and it didn't show any calculations or any range that the x, y, z axis's output
I'm puzzled as to why you're getting a reading of 519 for a motionless, horizontal accelerometer.Is your Arduino running off 3.3V too, or have you set the analogue reference to 3.3V also?
You realise that the self-test is an input to the accelerometer?
However, the map command does not constrain the values to be in the from range. So, if you are reading (and not printing, so you don't know) a value like 312 from the potentiometer, and mapping values in the range 414 to 519 to 0 to 179, then the 312 value will cause map to return a negative number.Since the servo can't move to a negative value, it simply sits still.
#include <Servo.h> Servo myservo; // create servo object to control a servo int potpin = 3; // analog pin used to connect the potentiometerint val; // variable to read the value from the analog pin int val2;int val3;void setup() { Serial.begin(9600); myservo.attach(9); // attaches the servo on pin 9 to the servo object } void loop() { val = analogRead(potpin); // reads the value of the potentiometer (value between 0 and 1023) Serial.println(val);Serial.print("\t");val = map(val, 269, 409, 0, 179); // scale it to use it with the servo (value between 0 and 180)Serial.println(val);myservo.write(val);delay(100);}
I also donno why I'm getting 519 when it's horizontal, maybe cus the analog reading is 0-1023 and 519 is near the middle
I got it working with this code:
val = map(val, 269, 409, 0, 179);
Now i just have to add a few more lines of code to get the avg value so it's more stable
I must say that I'm surprised, or that you have a different definition of working than I do.This code is reading the value of a potentiometer, not an accelerometer. If the potentiometer is wired correctly, this code should result in analogRead returning values in the range 0 to 1023.
For values from 0 to 269, this will result in val containing a negative value. For values from 410 to 1023, val will contain values of 180 or more.So, for a broad range of potentiometer movement, the map function will output a value that the servo can not move to. Unless it's not a normal 180 degree servo.
I don't think you completely get what I'm doing still.
I just didn't bother to rename the code
At steady state ( leaving the accelerometer flat on the table) I get 336
Why would the pot or aka (accelerometer) output values higher than 409?
Which is the values that I tested and will never go past.