3#include <rom/ets_sys.h>
4const uint16_t samples = 256; //This value MUST ALWAYS be a power of 2
5const int centeredThreshold = 50 ;
6//const float signalFrequency = 1000;
7const float samplingFrequency = 4000;
8float vReal[4][samples];
9float vImag[4][samples];
12#define SCL_FREQUENCY 0x02
15ArduinoFFT<float> FFT = ArduinoFFT<float>(vReal[0], vImag[0], samples, samplingFrequency); /* Create FFT object */
17Dezibot dezibot = Dezibot();
21 //dezibot.infraredLight.front.turnOn();
22 //dezibot.infraredLight.bottom.turnOn();
26 portDISABLE_INTERRUPTS();
27 for(int i = 0; i < samples; i++){
28 vReal[0][i] = dezibot.lightDetection.getValue(IR_FRONT);
30 vReal[1][i] = dezibot.lightDetection.getValue(IR_LEFT);
32 vReal[2][i] = dezibot.lightDetection.getValue(IR_RIGHT);
34 vReal[3][i] = dezibot.lightDetection.getValue(IR_BACK);
39 portENABLE_INTERRUPTS();
40 //PrintVector(vReal, (samples>>1), 0);
42 //PrintVector(vReal, (samples>>1), 0);
45 for(int index = 0; index <4; index++){
46 FFT.setArrays(vReal[index], vImag[index]);
47 FFT.windowing(FFTWindow::Rectangle, FFTDirection::Forward); /* Weigh data */
48 FFT.compute(FFTDirection::Forward); /* Compute FFT */
49 FFT.complexToMagnitude(); /* Compute magnitudes */
50 FFT.majorPeak(&frequency[index],&magnitude[index]);
51 if(abs(frequency[index]-1147)>10){
56 Serial.print(frequency[index]);
58 Serial.print(index+4);
60 Serial.print(magnitude[index]);
67//================================================
68float leftValue = magnitude[1];
69float rightValue = magnitude[2];
70switch(brightest(magnitude)){
72 //correct Stearing to be centered
73 if( abs(leftValue-rightValue)
75 dezibot.motion.move();
77 if (leftValue > rightValue){
78 dezibot.motion.rotateAntiClockwise();
80 dezibot.motion.rotateClockwise();
83 dezibot.multiColorLight.setTopLeds(BLUE);
86 dezibot.motion.rotateAntiClockwise();
87 dezibot.multiColorLight.setTopLeds(RED);
90 dezibot.motion.rotateClockwise();
91 dezibot.multiColorLight.setTopLeds(GREEN);
94 if(leftValue > rightValue){
95 dezibot.motion.rotateAntiClockwise();
97 dezibot.motion.rotateClockwise();
99 dezibot.multiColorLight.setTopLeds(YELLOW);
110photoTransistors brightest(float *magnitudes){
112 float maxMagnitude = 0;
113 for(int index = 0; index <4; index++){
114 if (magnitudes[index] > maxMagnitude){
116 maxMagnitude = magnitudes[index];
131void PrintVector(float *vData, uint16_t bufferSize, uint8_t scaleType)
133 for (uint16_t i = 0; i < bufferSize; i++)
136 /* Print abscissa value */
140 abscissa = (i * 1.0);
143 abscissa = ((i * 1.0) / samplingFrequency);
146 abscissa = ((i * 1.0 * samplingFrequency) / samples);
149 Serial.print(abscissa, 6);
150 if(scaleType==SCL_FREQUENCY)
153 Serial.println(vData[i], 4);