/* Firefly Code Generator by Andy Payne Copyright 2011 All Rights Reserved Code Generated on 11/08/2013 12:01:46 Special thanks to Panagiotis Michalatos. For more information visit: www.fireflyexperiments.com */ #include "FFCasts.h" #include //******************* 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; } //Gate_Nand Function: Perform boolean alternative denial (NAND gate). This is an inverse AND gate. boolean Gate_Nand(boolean _v1, boolean _v2){ boolean result; if (_v1 == true && _v2 == true){ result = false; }else{ result = true; }return result; } //Gate_Nor Function: Perform boolean joint denial (NOR gate). This is an inverse OR gate. boolean Gate_Nor(boolean _v1, boolean _v2){ boolean result; if (_v1 == true || _v2 == true){ result = false; }else{ result = true; }return result; } double smoothlist_0[8]; int smoothindex_0 = 0; double smoothtotal_0 = 0.0; double smoothlist_1[8]; int smoothindex_1 = 0; double smoothtotal_1 = 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; } double Counter_0 = 0.0; //Counter Function: Count upwards and downwards. double Counter(boolean _dir, double _dt, boolean _s, boolean _r, Interval _in, double *_cnt){ if(_s){ if(_dir) *_cnt = *_cnt + abs(_dt); else if(!_dir) *_cnt = *_cnt - abs(_dt); if(*_cnt >= _in.t1) *_cnt = _in.t1; else if (*_cnt <= _in.t0) *_cnt = _in.t0; if(_r) *_cnt = 0.0; } return *_cnt; } //Larger_Than: Tests if A is larger than B. boolean Larger_Than(double _v1, double _v2) { boolean result; if(_v1 > _v2) result = true; else result = false; return result; } //Smaller_Than: Tests if A is smaller than B. boolean Smaller_Than(double _v1, double _v2) { boolean result; if(_v1 < _v2) result = true; else result = false; return result; } //******************** End Function Definitions ******************** Servo servo11; void setup() { int smi; for(smi = 0; smi < 8; ++smi) { smoothlist_0[smi] = 0.0; } for(smi = 0; smi < 8; ++smi) { smoothlist_1[smi] = 0.0; } Counter_0 = 0.0; servo11.attach(11); pinMode(2, INPUT); } void loop() { int DPin2 = digitalRead(2); servo11.write(Remap_Numbers(Gate_Nor((boolean)(Smoothing_Moving_Average((int)(Gate_Nand(Smaller_Than((double)(DPin2),3000),Larger_Than((double)(DPin2),150))),8, smoothlist_0, &smoothindex_0, &smoothtotal_0)),Smaller_Than(Counter((boolean)(Smoothing_Moving_Average((int)(Gate_Nand(Smaller_Than((double)(DPin2),3000),Larger_Than((double)(DPin2),150))),8, smoothlist_1, &smoothindex_1, &smoothtotal_1)), &Counter_0),200.0)),Interval(0,1),Interval(180,1))); }