Ciao a tutti! Sto sviluppando un sensore con all'interno una soluzione salina, e visualizzando i cambiamenti di resistenza voglio leggere i cambiamenti della sua lunghezza. Essendo una soluzione salina, ho bisogno di alimentare il sensore con una tensione variabile e per questo motivo sto usando tramite Arduino Due un segnale PWM a 3kHz.
In uscita dal sensore ho sempre un'onda quadra alla stessa frequenza, che campiono con l'ADC e mando a Matlab per poi analizzarla.. Mi stavo chiedendo se fosse possibile fare un campionamento a 3kHz, dato che i due segnali sono in fase, per poter prendere solo i "picchi" dell'onda quadra ma non i segnali a zero..
Qualcuno ha in mente un'idea su come potrei fare??
Questo è il codice, in cui uso una libreria per cambiare la frequenza del PWM e uno sketch pronto per l'interfaccia con Matlab..
Grazie a tutti ![]()
#include "C:\Users\bbbb\Desktop\TESI\arduino\pwm01.h"
#if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__)
#define INTERNAL INTERNAL1V1
#define ADC_STARTUP_FAST 12
#endif
void setup() {
/* initialize serial */
Serial.begin(115200);
adc_init(ADC, SystemCoreClock, ADC_FREQ_MAX, ADC_STARTUP_FAST);
ADC->ADC_MR = 0b1111111111000000; // prescaler = 255 ADCClock = 164kHz
analogReadResolution(12);
uint32_t pwm_duty = 32767;
uint32_t pwm_freq1 = 3000;
// Set PWM Resolution
pwm_set_resolution(16);
// Setup PWM Once (Up to two unique frequencies allowed
//-----------------------------------------------------
pwm_setup( 6, pwm_freq1, 1); // Pin 6 freq set to "pwm_freq1" on clock A
// Write PWM Duty Cycle Anytime After PWM Setup
//-----------------------------------------------------
pwm_write_duty( 6, pwm_duty ); // 50% duty cycle on Pin 6
}
void loop() {
/* variables declaration and initialization */
static int s = -1; /* state */
static int pin = 13; /* generic pin number */
int val = 0; /* generic value read from serial */
int agv = 0; /* generic analog value */
int dgv = 0; /* generic digital value */
if (Serial.available() >0) {
/* whatever is available from the serial is read here */
val = Serial.read();
switch (s) {
/* s=-1 means NOTHING RECEIVED YET ******************* */
case -1:
/* calculate next state */
if (val>47 && val<90) {
/* the first received value indicates the mode
49 is ascii for 1, ... 90 is ascii for Z
s=0 is change-pin mode;
s=10 is DI; s=20 is DO; s=30 is AI; s=40 is AO;
s=90 is query script type (1 basic, 2 motor);
s=340 is change analog reference;
s=400 example echo returning the input argument;
*/
s=10*(val-48);
}
if ((s>40 && s<90) || (s>90 && s!=340 && s!=400)) {
s=-1;
}
/* the break statements gets out of the switch-case, so
/* we go back and wait for new serial data */
break; /* s=-1 (initial state) taken care of */
/* s=0 or 1 means CHANGE PIN MODE */
case 0:
/* the second received value indicates the pin
from abs('c')=99, pin 2, to abs('¦')=166, pin 69 */
if (val>98 && val<167) {
pin=val-97; /* calculate pin */
s=1; /* next we will need to get 0 or 1 from serial */
}
else {
s=-1; /* if value is not a pin then return to -1 */
}
break; /* s=0 taken care of */
case 1:
/* the third received value indicates the value 0 or 1 */
if (val>47 && val<50) {
/* set pin mode */
if (val==48) {
pinMode(pin,INPUT);
}
else {
pinMode(pin,OUTPUT);
}
}
s=-1; /* we are done with CHANGE PIN so go to -1 */
break; /* s=1 taken care of */
/* s=10 means DIGITAL INPUT ************************** */
case 10:
/* the second received value indicates the pin
from abs('c')=99, pin 2, to abs('¦')=166, pin 69 */
if (val>98 && val<167) {
pin=val-97; /* calculate pin */
dgv=digitalRead(pin); /* perform Digital Input */
Serial.println(dgv); /* send value via serial */
}
s=-1; /* we are done with DI so next state is -1 */
break; /* s=10 taken care of */
/* s=20 or 21 means DIGITAL OUTPUT ******************* */
case 20:
/* the second received value indicates the pin
from abs('c')=99, pin 2, to abs('¦')=166, pin 69 */
if (val>98 && val<167) {
pin=val-97; /* calculate pin */
s=21; /* next we will need to get 0 or 1 from serial */
}
else {
s=-1; /* if value is not a pin then return to -1 */
}
break; /* s=20 taken care of */
case 21:
/* the third received value indicates the value 0 or 1 */
if (val>47 && val<50) {
dgv=val-48; /* calculate value */
digitalWrite(pin,dgv); /* perform Digital Output */
}
s=-1; /* we are done with DO so next state is -1 */
break; /* s=21 taken care of */
/* s=30 means ANALOG INPUT *************************** */
case 30:
/* the second received value indicates the pin
from abs('a')=97, pin 0, to abs('p')=112, pin 15 */
if (val>96 && val<113) {
pin=val-97; /* calculate pin */
agv=analogRead(pin); /* perform Analog Input */
Serial.println(agv); /* send value via serial */
}
s=-1; /* we are done with AI so next state is -1 */
break; /* s=30 taken care of */
/* s=40 or 41 means ANALOG OUTPUT ******************** */
case 40:
/* the second received value indicates the pin
from abs('c')=99, pin 2, to abs('¦')=166, pin 69 */
if (val>98 && val<167) {
pin=val-97; /* calculate pin */
s=41; /* next we will need to get value from serial */
}
else {
s=-1; /* if value is not a pin then return to -1 */
}
break; /* s=40 taken care of */
case 41:
/* the third received value indicates the analog value */
analogWrite(pin,val); /* perform Analog Output */
s=-1; /* we are done with AO so next state is -1 */
break; /* s=41 taken care of */
/* s=90 means Query Script Type:
(0 adio, 1 adioenc, 2 adiosrv, 3 motor) */
case 90:
if (val==57) {
/* if string sent is 99 send script type via serial */
Serial.println(0);
}
s=-1; /* we are done with this so next state is -1 */
break; /* s=90 taken care of */
/* s=340 or 341 means ANALOG REFERENCE *************** */
case 340:
#if defined(__AVR__) || defined(__PIC32MX__)
switch (val) {
case 48:
analogReference(DEFAULT);
break;
case 49:
analogReference(INTERNAL);
break;
case 50:
analogReference(EXTERNAL);
break;
default: /* unrecognized, no action */
break;
}
#endif
s=-1; /* we are done with this so next state is -1 */
break; /* s=341 taken care of */
/* s=400 roundtrip example function (returns the input)*/
case 400:
/* your own code goes here instead of the serial print */
Serial.println(val);
s=-1; /* we are done with the aux function so -1 */
break; /* s=400 taken care of */
/* ******* UNRECOGNIZED STATE, go back to s=-1 ******* */
default:
s=-1; /* go back to the initial state, break unneeded */
} /* end switch on state s */
} /* end if serial available */
} /* end loop statement */
pwm01.h (3.96 KB)