Important changes to forums and questions
All forums and questions are now archived. To start a new conversation or read the latest updates go to forums.mbed.com.
11 years, 1 month ago.
How generate a random signal using mbed NXP LPC1768
Hello please could anyone give me an hint on how to start. i need to generate a random signal using mbed which should work like this:
it should be between 0-3.3volts. with offset of 0.1volts.
that is, the highest voltage should be 3.2volt and lowest 0.1volts, anything above 3.2v or below 0.1v should be read as noise.
thanks in advance
1 Answer
11 years, 1 month ago.
Try this:
#include "mbed.h" AnalogOut signal(p18); //Only for p18 Serial pc(USBTX, USBRX); //Debug int main() { float min = 0.1 / 3.3; //Define 0.1 V as ~03% from 3.3 V float max = 3.2 / 3.3; //Define 3.2 V as ~97% from 3.3 V while(1) { signal = min + ((float)rand()/RAND_MAX) * (max - min); // Generate random voltage output from 0.1 to 3.2 printf("Voltage: %1.2f [V]\n", signal.read()* 3.3); // Debug wait(1); // Time to keep random voltage before next change } }
EDIT: Repair rand() and added debug messages
Did you try if this code is really working?
I thought AnalogOut has a range of 0.0 to 1.0 where 0.0 = 0.0V and 1.0 = 3.3V. Am I wrong?
Also I am missing an initialization of random. Normally you use srand(time(NULL)); for this purpose.
posted by 12 Jul 2013Yes it really works, but I programmed this on paper without testing. The only thing I changed is small mistake at rand() (added devide by RAND_MAX), because I thought that rand() returns 0.0-1.0. And added debug messages for comparison with measured data.
posted by 12 Jul 2013I tested it also. It's really working. I am a little confused, because the class reference tells me that the range is 0.0 to 1.0.
Anyway your code looks better now.
posted by 15 Jul 2013