Task Handle
Task Handle
TaskHandle_t Task2;
// LED pins
// const int led1 = 2;
// const int led2 = 4;
#include <PID_v1.h>
#include "soc/timer_group_struct.h"
#include "soc/timer_group_reg.h"
int MSB3;
int LSB3;
int flagSensor1 = 0;
int flagSensor2 = 0;
int flagSensor3 = 0;
pinMode(MotEnable1, OUTPUT);
pinMode(MotFwd1, OUTPUT);
pinMode(MotRev1, OUTPUT);
pinMode(MotEnable2, OUTPUT);
pinMode(MotFwd2, OUTPUT);
pinMode(MotRev2, OUTPUT);
pinMode(MotEnable3, OUTPUT);
pinMode(MotFwd3, OUTPUT);
pinMode(MotRev3, OUTPUT);
pinMode(sensor1Pin, INPUT);
pinMode(sensor2Pin, INPUT);
pinMode(sensor3Pin, INPUT);
pinMode(encoderPin1A, INPUT_PULLUP);
pinMode(encoderPin1B, INPUT_PULLUP);
pinMode(encoderPin2A, INPUT_PULLUP);
pinMode(encoderPin2B, INPUT_PULLUP);
pinMode(encoderPin3A, INPUT_PULLUP);
pinMode(encoderPin3B, INPUT_PULLUP);
//Interrupt endsensor
// attachInterrupt(sensor1Pin, turnOffMotor1, FALLING);
attachInterrupt(sensor2Pin, turnOffMotor2, FALLING);
attachInterrupt(sensor3Pin, turnOffMotor3, FALLING);
timeMillis = millis();
// Firebase
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
Serial.print("Dang ket noi");
while (WiFi.status() != WL_CONNECTED) {
Serial.print(".");
delay(500);
}
Serial.println("");
Serial.println("Da ket noi WiFi!");
Serial.println(WiFi.localIP());
/* Sign up */
if (Firebase.signUp(&config, &auth, "", "")) {
Serial.println("ok");
signupOK = true;
} else {
Serial.printf("%s\n", config.signer.signupError.message.c_str());
}
/* Assign the callback function for the long running token generation
task */
// config.token_status_callback = tokenStatusCallback; //see
addons/TokenHelper.h
Firebase.begin(&config, &auth);
Firebase.reconnectWiFi(true);
delay(1000);
Serial.println("Setup Done");
//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);
}
void loop() {
delay(1);
}
void pwmOut(int out1, int out2, int out3) {
if (out1 > 0) { // if REV_Theta1 > encoderValue1 motor
move in forward direction.
analogWrite(MotEnable1, out1); // Enabling motor enable pin to
reach the desire angle
forward1(); // calling motor to move forward
} else {
analogWrite(MotEnable1, abs(out1)); // if REV_Theta1 <
encoderValue1 motor move in forward direction.
reverse1(); // calling motor to move reverse
}
if (out2 > 0) { // if REV_Theta1 > encoderValue1 motor
move in forward direction.
analogWrite(MotEnable2, out2); // Enabling motor enable pin to
reach the desire angle
forward2(); // calling motor to move forward
} else {
analogWrite(MotEnable2, abs(out2)); // if REV_Theta1 <
encoderValue1 motor move in forward direction.
reverse2(); // calling motor to move reverse
}
void updateEncoder2() {
int MSB2 = digitalRead(encoderPin2A); // MSB2 = most significant
bit
int LSB2 = digitalRead(encoderPin2B); // LSB2 = least significant
bit
int encoded2 = (MSB2 << 1) | LSB2; // converting the 2 pin
value to single number
int sum2 = (lastEncoded2 << 2) | encoded2; // adding it to the
previous encoded value
void updateEncoder3() {
MSB3 = digitalRead(encoderPin3A); // MSB3 = most significant bit
LSB3 = digitalRead(encoderPin3B); // LSB3 = least significant bit
// readString = "";
}
void forward1() {
digitalWrite(MotFwd1, HIGH);
digitalWrite(MotRev1, LOW);
}
void reverse1() {
digitalWrite(MotFwd1, LOW);
digitalWrite(MotRev1, HIGH);
}
void finish1() {
digitalWrite(MotFwd1, LOW);
digitalWrite(MotRev1, LOW);
}
void forward2() {
digitalWrite(MotFwd2, HIGH);
digitalWrite(MotRev2, LOW);
}
void reverse2() {
digitalWrite(MotFwd2, LOW);
digitalWrite(MotRev2, HIGH);
}
void finish2() {
digitalWrite(MotFwd2, LOW);
digitalWrite(MotRev2, LOW);
}
void forward3() {
digitalWrite(MotFwd3, HIGH);
digitalWrite(MotRev3, LOW);
}
void reverse3() {
digitalWrite(MotFwd3, LOW);
digitalWrite(MotRev3, HIGH);
}
void finish3() {
digitalWrite(MotFwd3, LOW);
digitalWrite(MotRev3, LOW);
}