Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: mbed
Diff: LightSensor.cpp
- Revision:
- 2:2b7dc4f24d04
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/LightSensor.cpp Fri May 18 13:34:28 2018 +0000 @@ -0,0 +1,182 @@ +// Tested: NUCLEO-F207ZG +#include "mbed.h" + +// numero di acqusizioni su cui effettuare la media della luminosità +#define NUMSAMPLE 300 + +AnalogIn InWave(PF_3); +Serial pc(USBTX, USBRX); + + +// Output LED di diagnostica +DigitalOut led1(LED1); // verde +DigitalOut led2(LED2); // blu +DigitalOut led3(LED3); // rosso + + +// ticker per l'acquisizione dell'onda con ADC +Ticker SamplingTicker; + +// carattere in arrivo dal PC ed equivalente numerico +volatile char cReadChar; +volatile int nReadChar; + +// flag che diventa true quando si vuole fermare l'acquisizione +volatile bool bStop; + +// valore letto dall'ADC e corrispondente in tensione +volatile unsigned short usReadADC; +volatile float fReadVoltage; + +// valore di luminosità letto dall'ADC +volatile float fLight; + +//*************************** +// Acquisizione da ADC +//*************************** +void Sampling() +{ + // indice per i cicli + int nIndex; + // valore medio della Luminosità su NUMACQUISISIONI acquisizioni + float fAvgLight; + + // se è stato inviato il comando Stop, non fare niente fino a nuovo comando + if(bStop) + { + } + else // se non è stato inviato il comando di bStop continua + { + // inizializza il valore medio della Luminosità + fAvgLight=0.0; + for(nIndex=0; nIndex < NUMSAMPLE; nIndex++) + { + // acquisisce dato da ADC + usReadADC = InWave.read_u16(); + fReadVoltage=(usReadADC*3.3)/65535.0; // converte in Volt il valore numerico letto dall'ADC + //fReadVoltage=InWave.read(); // acquisisce il valore dall'ADC come valore di tensione in volt + fLight= fReadVoltage; //ATTENZIONE Visualizza il valore grezzo letto dall'ADC + fAvgLight+=fLight; + } + // calcola valore medio su NUMSAMPLE acquisizioni + fAvgLight/= NUMSAMPLE; + + // accendi LED in base a superamento soglie + if (fAvgLight <= 21845.0) + { + // accendi LED Blu sotto 0.3*valore massimo (65535) + led1=0; + led2=1; + led3=0; + } + else + { + if(fAvgLight >= .0) + { + // accendi LED Rosso 0.6*valore massimo + led1=0; + led2=0; + led3=1; + } + else + { + // accendi LED verde (0.3 e 0.6)*valore massimo + led1=1; + led2=0; + led3=0; + } + } + + + + + // invia il dato al PC + //pc.printf("\n\r--- Voltage= %.1f [Volt]; Brightness= %.1f [Celsius] ---\n\r", fReadVoltage, fLight); + pc.printf("\n\r--- Digital= %d [Volt]; Brightness= %.2f [Celsius] ---\n\r", usReadADC, fLight); + + + + /* + // prepara il pacchetto di dati acquisiti da restituire al PC + caTxPacket[nSampleInCount]= (char)(usReadADC&0xFF); + //+++caTxPacket[nSampleInCount]= 'a'; + nSampleInCount++; + caTxPacket[nSampleInCount] = (char)((usReadADC>>8)&0xFF); + //++++caTxPacket[nSampleInCount]= 'b'; + */ + + } +} + + + + + //******************* + // Loop Principale + //******************* +int main() +{ + // periodo di campionamento + int nDeltaT; + + // inizializza variabili + bStop=true; + + // configura velocità della comunicazione seriale su USB-VirtualCom e invia messaggio di benvenuto + pc.baud(921600); //921600 bps + + // messaggio di benvenuto + pc.printf("\r\nHallo Amaldi Students - Exercise 8 \r\n"); + pc.printf("\r\n*** Light Acquisition ***\r\n"); + + // test dei LED + led1=1; //Verde + wait_ms(1000); + led1=0; + led2=1; // Blu + wait_ms(1000); + led2=0; + led3=1; //Rosso + wait_ms(1000); + led3=0; + + + + while(true) + { + // verifica se è arrivato un carattere dalla seriale del PC + if(pc.readable()) + { + cReadChar = pc.getc(); // Read hyperterminal + + if((cReadChar == 'S') || (cReadChar == 's')) // blocca acquisizione se riceve 'S' oppure 's' + { + bStop= true; + pc.printf("\n\r--- Acquisition Stopped ---\n\r"); + } + if((cReadChar >= '0') && (cReadChar <='9')) + { + bStop = false; + nReadChar = cReadChar -'0'; //converte il carattere acquisito dal PC nel corrispondente valore numerico + nDeltaT = nReadChar; // sampling Period in sec + pc.printf("\n\r--- Acquisition Started DeltaT = %c ---\n\r", cReadChar); + SamplingTicker.attach(&Sampling,nDeltaT); + } + + } //lettura da pc + } // while(true) +} + + + + + + + + + + + + + +