Im in need of a little help. I am triying to convert the digital input from the TinyDuino Accelerometer to an analog output. I have changing to every available PWM pin and I just cant seem to get it to work. In the serial monitor I am getting successful conversion and constrained values but under my pin name "Analog" it just prints the pin number and not the value of "Converted".
Any help would be awesome Thanks!
/*
TinyDuino Accelerometer Demo
May 25 2014, by Ben Rose
This example code is in the public domain.
http://www.tiny-circuits.com
*/
#include <Wire.h>
#include "BMA250.h"
int Analog = 8;
int Convert = 0;
int Converted = 0;
int AxisX = 0;
BMA250 accel;
void setup()
{
Serial.begin(38400);
Wire.begin();
accel.begin(BMA250_range_2g, BMA250_update_time_05ms);//This sets up the BMA250 accelerometer
}
void loop()
{
accel.read();//This function gets new data from the accelerometer
AxisX = accel.X;
Convert = map(AxisX, -260, 265, 0, 1023);
Converted = constrain(Convert, 0, 1023);
analogWrite(Analog, Converted);
Serial.print("X = ");
Serial.print(accel.X);
Serial.print(" ");
Serial.print("Y = ");
Serial.print(accel.Y);
Serial.print(" ");
Serial.print("Z = ");
Serial.print(accel.Z);
Serial.print(" Temperature(C) = ");
Serial.println((accel.rawTemp*0.5)+24.0,1);
Serial.print(" Convert = ");
Serial.print(Convert);
Serial.print(" Converted = ");
Serial.print(Converted);
Serial.print(" AnalogOut = ");
Serial.println(Analog);
delay(120);//We'll make sure we're over the 64ms update time set on the BMA250
}
but under my pin name "Analog" it just prints the pin number and not the value of "Converted".
Yes... That's exactly what your print statements do... After printing the text "AnalogOut = ", it prints the value of the variable named Analog, which is always 8.
And as Robert says, your PWM value is going to be fouled-up if if you feed it a value greater than 255. I would guess you're going the get the bottom 8-bits which will pretty-much look like random values.
Okay I understand the 255 part I missed that. I lost then how can you print that actual output of that pin? I know it should be Converted but doesn't tell me I for sure have that value at that pin..
I am not sure the last question, but I think: read that pin with a volt meter. As you vary the value, the volts out should change. Between 0V, up to ~5V. Does it change? how much?
What will be printed is the value of Analog, which is 8.
Because of;
int Analog = 8;
You need to serialprint the variable Converted.
However it will not be an output voltage level, it will be the duty cycle value of the outputted PWM signal.
0 is no pulse, 228 will be 50% duty cycle, 255 will be a constant DC output.
nissan20det:
I am triying to convert the digital input from the TinyDuino Accelerometer to an analog output. I have changing to every available PWM pin and I just cant seem to get it to work.
There are no analogue outputs on a TinyDuino.
With a resistor and a capacitor you can turn a PWM signal into a voltage.
0-PWM is 0volt, and 255-PWM is the voltage the processor runs on.
It might need additional parts, depending on what you want to do with that voltage.
Leo..