this weeks assignment was to add an output device to a micro-controller board and program it to do something. I am using the arduino uno board with firefly.
I want to control the speed of the dc motor with a microphone.I connected the mic. to analog pin A0 and the DC motor to digital pin no. 3.
In Firefly i opened the right port ( no. 7) and added an uno read component.
now i read data from the mic sensor attached to the arduino board.
I connected a “buffer” component so i can get the minimum and maximum values of the reading.I take these values from the last 100 readings.
I approximately constrained the values that i get from the mic with the help from the data i got from the “buffer” component. The DC motor gets values from 0-255 (pwn) so i added a “remap” component to set the values from 21-88 to a 0-255 values.To make the speed change smoother i added the “smooth” component and set it’s value to 5.
Now i add a “uno write” component and plag the data coming from the “smooth” component to the digital pin no. 3 (dpin3). After i add a boolean toggle and set it to “true” at the “start” pin the motor starts to react to the mic sensor.
To the out pin of the “uno write” component i added a “code generator” component so the code can be generated to a specific location that i set in the path pin.
I closed Rhino and uploaded the file that was generated by Firefly in Arduino to the board and it warked just fine!!! here is the code from arduino:
/*
Firefly Code Generator by Andy Payne
Copyright 2011 All Rights Reserved
Code Generated on 04/24/2013 23:57:10
Special thanks to Panagiotis Michalatos.
For more information visit: www.fireflyexperiments.com
*/
#include “FFCasts.h”
//******************* Begin Function Definitions *******************
//Remap Number Function: Remap a value into a new numeric domain.
double Remap_Numbers(double x, Interval _in, Interval _out) {
return (x – _in.t0) * (_out.t1 – _out.t0) / (_in.t1 – _in.t0) + _out.t0;
}
//Constrain Function: Constrains a number to a specific numeric range.
double Constrain(double _v1, Interval _in){
double _min, _max, result;
if (_in.t0 < _in.t1){
_min = _in.t0;
_max = _in.t1;
}else{
_min = _in.t1;
_max = _in.t0;
}
if (_v1 < _min){
result = _min;
}else if (_v1 > _max){
result = _max;
}else{
result = _v1;
}return result;
}
double smoothlist_0[2];
int smoothindex_0 = 0;
double smoothtotal_0 = 0.0;
//Smoothing Function: Smooth (or average) an incoming value based on (N) number of samples.
double Smoothing_Moving_Average(double _v1, int _n, double *_list, int *_index, double *_total){
*_total -= _list[*_index];
*_total += _v1;
_list[*_index] = _v1;
(*_index)++;
if (*_index >= _n) *_index = 0;
return *_total/(double)_n;
}
//******************** End Function Definitions ********************
void setup() {
int smi;
for(smi = 0; smi < 2; ++smi) {
smoothlist_0[smi] = 0.0;
}
pinMode(3, OUTPUT);
}
void loop() {
int APin0 = analogRead(0);
analogWrite(3, Smoothing_Moving_Average(Remap_Numbers(Constrain(APin0,Interval(24,67)),Interval(24,67),Interval(40,255)),2, smoothlist_0, &smoothindex_0, &smoothtotal_0));
}