/*********
Aula 10 micro 2
*********/
TaskHandle_t Task1;
TaskHandle_t Task2;
// LED pins
const int led1 = 2;
const int led2 = 4;
// Constantes
#define sensorPino0 34
#define sensorPino1 14
#define leituras 10
#define canalPwm0 0
#define resolucaoAdPwm 12
#define freqPwm 1000
#define pinoPwm0 16
#define canalPwm1 1
#define pinoPwm1 17
int perct0, pernt1;
void setup() {
Serial.begin(115200);
// configurações do A/D
analogReadResolution ( resolucaoAdPwm ); // resolução de 12 bits
// Atenuação: ADC_0db, ADC_2_5db, ADC_6db, ADC_11db
// 0dB: 100 - 950mV # 2.5dB: 100 - 1250mV # 6dB: 150 - 1750mV # 11dB: 150 - 2450mV
analogSetPinAttenuation (sensorPino0, ADC_11db); // Atenuação de 6db no pino sensorPino
analogSetPinAttenuation (sensorPino1, ADC_11db); // Atenuação de 6db no pino sensorPino
// pwm
ledcSetup(canalPwm0, freqPwm, resolucaoAdPwm);
ledcAttachPin(pinoPwm0, canalPwm0);
ledcSetup(canalPwm1, freqPwm, resolucaoAdPwm);
ledcAttachPin(pinoPwm1, canalPwm1);
//GPIO
pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT);
//create a task that will be executed in the Task1code() function, with priority 1 and executed on core 0
xTaskCreatePinnedToCore(
Task1code, /* Task function. */
"Task1", /* name of task. */
10000, /* Stack size of task */
NULL, /* parameter of the task */
1, /* priority of the task */
&Task1, /* Task handle to keep track of created task */
0); /* pin task to core 0 */
delay(500);
//create a task that will be executed in the Task2code() function, with priority 1 and executed on core 1
xTaskCreatePinnedToCore(
Task2code, /* Task function. */
"Task2", /* name of task. */
10000, /* Stack size of task */
NULL, /* parameter of the task */
1, /* priority of the task */
&Task2, /* Task handle to keep track of created task */
1); /* pin task to core 1 */
delay(500);
}
//Task1code: blinks an LED every 1000 ms
void Task1code( void * pvParameters ){
while(1){
// Leitura do sensor
long sensorValor0 = 0; // zera as leituras
for (int i = 0; i < leituras; i++) {
sensorValor0 = sensorValor0 + analogRead(sensorPino0); // acumula as leituras
delay(1); // tempo para o conversor
}
sensorValor0 = sensorValor0 / leituras; // calcula a média
Serial.println(sensorValor0); // imprime a media da conversão
ledcWrite(canalPwm0,sensorValor0);
Serial.print("Task1 running on core ");
Serial.println(xPortGetCoreID());
delay(500);
}
//for(;;){
// digitalWrite(led1, HIGH);
// delay(1000);
// digitalWrite(led1, LOW);
// delay(1000);
//}
}
//Task2code: blinks an LED every 700 ms
void Task2code( void * pvParameters ){
while (1){
long sensorValor1 = 0; // zera as leituras
for (int i = 0; i < leituras; i++) {
sensorValor1 = sensorValor1 + analogRead(sensorPino1); // acumula as leituras
delay(1); // tempo para o conversor
}
sensorValor1 = sensorValor1 / leituras; // calcula a média
Serial.println(sensorValor1); // imprime a media da conversão
ledcWrite(canalPwm1,sensorValor1);
Serial.print("Task1 running on core ");
Serial.println(xPortGetCoreID());
delay(500);
//Serial.print("Task2 running on core ");
//Serial.println(xPortGetCoreID());
}
//for(;;){
// digitalWrite(led2, HIGH);
// delay(700);
// digitalWrite(led2, LOW);
// delay(700);
//}
}
void loop() {
}