Boa tarde, 

Fiz um sketch que roda um PWM emulado, utilizei millis ao invés de delay , e consigo ajustar o duty-cycle com o potenciômetro.Vou utilizar uma frequencia de 1hz(no código coloquei 60hz para poder visualizar no ociloscópio)

Em outro sketch fiz a leitura de temperatura com um sensor de tempertarua Circuitar ,termopar Nanoshield 2.0. Estou utilizando o arduino mega, 2560,anexei o esquema de ligamento do sensor.

Os dois estão funcionando perfeitamente, mas ao juntar os código a função serial.begin do sensor e o delay(1000) comprometem o PWM . Gostaria de saber uma forma de resolver isso.Pensei em interrupção ,mas antes de aprender sobre ,queria saber se é possivel fazer a leitura no momento em que o PWM não está funcionando(1s ).

Resumindo quero manter o pwm e a leitura de dados do sensor,sem acabar com meu pwm.

Se foi interrupção por código ,sem precisar utilizar bibliotecas, ou interrupção externa melhor ainda.


#include <SPI.h>
#include "Nanoshield_Termopar.h"

//// Termopar Nanoshield on CS pin D8, type K thermocouple, no averaging
Nanoshield_Termopar tc(8, TC_TYPE_K, TC_AVG_OFF);

//----------Start PWM---------------
// constants won't change. Used here to
// set pin numbers:
const int Pin = 53; // the number of the pin

// Variables will change:

long previousMillis = 0; // will store last time was updated
// the follow variables is a long because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long interval = 16.6 ; // interval(milliseconds),frequency=1/interval


////----------End PWM---------------
void setup()
{ Serial.begin(9600);

//PWM
// set the digital pin as output for PWM:
pinMode(Pin, OUTPUT);

//Termopar
// tc.begin();
}

void printErrors() {
if (tc.isOpen()) {
Serial.print("Open circuit");
} else if (tc.isOverUnderVoltage()) {
Serial.print("Overvoltage/Undervoltage");
} else if (tc.isInternalOutOfRange()) {
Serial.print("Internal temperature (cold junction) out of range)");
} else if (tc.isExternalOutOfRange()) {
Serial.print("External temperature (hot junction) out of range");
}
}

void loop()
{ // here is where you'd put code that needs to be running all the time.


//Potentiometer
float adc = analogRead(A0);
adc = map(adc, 0, 1023, 1, 100);
float duty = adc / 100;
//--------Start Counter----------
unsigned long currentMillis = millis();

//--------------Start PWM---------------------
if (currentMillis - previousMillis <= duty * interval)
{
digitalWrite(Pin, HIGH);
}
else
{ digitalWrite(Pin, LOW);
}

//--------------End of PWM--------------

//--------------Start Interval-----------------
if (currentMillis - previousMillis > interval) {
// save the last time
previousMillis = currentMillis;

}
//---------------End Interval----------------
// Read thermocouple data
tc.read();

// Print temperature in the serial port, checking for errors
if (tc.hasError()) {
printErrors();
} else {
Serial.print("Internal: ");
Serial.print(tc.getInternal());
Serial.print(" | External: ");
Serial.print(tc.getExternal());
}
Serial.println();
// // Wait for next reading
delay(1000);

}

Exibições: 219

Anexos

Responder esta

© 2024   Criado por Marcelo Rodrigues.   Ativado por

Badges  |  Relatar um incidente  |  Termos de serviço