It’s happening again this time by
WidgetTerminal terminal();
When I enable and use it gets stuck in ping/ready. The serial monitor is the same as the first post.
Here’s the updated code with terminal enabled:
#define BLYNK_TEMPLATE_ID ""
#define BLYNK_DEVICE_NAME ""
#define BLYNK_AUTH_TOKEN ""
/*
//LCD
//#include <avr/interrupt.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
//Ultrasonic Easyuino API
#include <Easyuino.h>
using Easyuino::DistanceMeter;
*/
//Defines and Pins
//====================================================================================================
//Motor
#define motor1F 9
#define motor1B 6
#define motor2F 5
#define motor2B 3
//UltraSS
#define trigPinF 11
#define echoPinF 10
#define trigPinB 8
#define echoPinB 7
//DistanceMeter USSF(trigPinF, echoPinF);
//DistanceMeter USSB(trigPinB, echoPinB);
//LCD
//LiquidCrystal_I2C lcd(0x27, 16, 2);
// addr,en,rw,rs,d4,d5,d6,d7,bl,blpol
// addr can be 0x3F or 0x27
#define breaksecs 15 //break out
//====================================================================================================
//GValues
//====================================================================================================
boolean StartStop = false;
boolean BoxLoad = false;
int BHZz = 1000;
//====================================================================================================
//Start of Blynk Code
//====================================================================================================
// Template ID, Device Name and Auth Token are provided by the Blynk.Cloud
// See the Device Info tab, or Template settings
#define BLYNK_PRINT Serial
#include <ESP8266_Lib.h>
#include <BlynkSimpleShieldEsp8266.h>
// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "";
char pass[] = "";
/*
char ssid[] = "(2.4G)";
char pass[] = "";
*/
// or Software Serial on Uno, Nano...
#define NEOSWSERIAL_EXTERNAL_PCINT
#include <NeoSWSerial.h>
NeoSWSerial EspSerial(12, 13); // RX, TX
//#include <SoftwareSerial.h>
//SoftwareSerial EspSerial(12, 13); // RX, TX
// Your ESP8266 baud rate:
#define ESP8266_BAUD 9600
ESP8266 wifi(&EspSerial);
WidgetTerminal terminal(V15);
BlynkTimer timer;
int Rsteer = 0;
BLYNK_WRITE(V3) {
Rsteer = param.asInt(); // assigning incoming value from pin V0 to a variable
}
int Speed = 0;
BLYNK_WRITE(V2) {
Speed = param.asInt(); // assigning incoming value from pin V0 to a variable
}
int Trigger = 0;
BLYNK_WRITE(V0) {
Trigger = param.asInt(); // assigning incoming value from pin V0 to a variable
}
int X = 0;
BLYNK_WRITE(V8) {
X = param.asInt();
}
int Y = 0;
BLYNK_WRITE(V9) {
Y = param.asInt();
}
int RunitsF = 0;
BLYNK_WRITE(V10) {
RunitsF = param.asInt();
}
BLYNK_WRITE(V15) {
String TempStringTerm = param.asString();
if (String("Stop") == param.asStr()) {
StartStop = false;
terminal.println("Stopping");
} else if (String("Start") == param.asStr()) {
StartStop = true;
terminal.println("Starting");
} else if (String("Full") == param.asStr()) {
BoxLoad = true;
terminal.println("Set to Full");
} else if (String("Empty") == param.asStr()) {
BoxLoad = false;
terminal.println("Set to Empty");
} else if (TempStringTerm.indexOf("Hz Set") != -1){
TempStringTerm.remove(0,6);
BHZz = TempStringTerm.toInt();
}
}
//End of Blynk Code
//================================================================================
void setup() {
/*
PCICR |= B00000100; //Turn on ISR for PD
PCMSK1 |= B00000000; //ISR PC for Pin A0 and A1 [No in use at the moment]
PCMSK2 |= B00010100; //ISR PD for Pin D2 and D4
pinMode(2, INPUT_PULLUP);
pinMode(4, INPUT_PULLUP);
*/
//Start of Blynk Setup
//================================================================================
// Debug console
Serial.begin(115200);
// Set ESP8266 baud rate
EspSerial.begin(ESP8266_BAUD);
delay(500);
//Blynk.config(BLYNK_AUTH_TOKEN, "blynk.cloud", 80);
Blynk.begin(BLYNK_AUTH_TOKEN, wifi, ssid, pass);
//Blynk.begin(auth, wifi, ssid, pass, "blynk.cloud", 80);
timer.setInterval(1000L, myTimerEvent);
Serial.println("Done!");
//================================================================================
//End of Blynk Setup
//Motors
//================================================================================
pinMode(motor1F, OUTPUT);
pinMode(motor2B, OUTPUT);
pinMode(motor1F, OUTPUT);
pinMode(motor2B, OUTPUT);
//================================================================================
/*
//LCD
//================================================================================
lcd.init();
lcd.clear();
lcd.setCursor(0, 0);
lcd.begin(16, 2); // 16 characters, 2 lines
lcd.backlight(); // turn backlight on
lcd.setBacklight(100);
lcd.print("Ready");
//================================================================================
//USensor
//================================================================================
USSF.begin();
USSB.begin();
//================================================================================
*/
MBrake();
}
void myTimerEvent() {
//Serial.println("Test");
// You can send any value at any time.
// Please don't send more that 10 values per second.
Blynk.virtualWrite(V1, millis() / 1000);
if (BoxLoad) {
tone(A2, BHZz);
} else {
noTone(A2);
}
if (StartStop) {
//Serial.println("StartStop");
Blynk.syncVirtual(V0, V2);
//MDrive(1);
MRUnit();
}
}
void loop() {
//Blynk!
//================================================================================
Blynk.run();
// You can inject your own code or combine it with other sketches.
// Check other examples on how to communicate with Blynk. Remember
// to avoid delay() function!
HitCheck();
timer.run();
//================================================================================
//End of Blynk
//MUnit(0,0,0);
}
void MBrake() {
digitalWrite(motor1F, 0);
digitalWrite(motor1B, 0);
digitalWrite(motor2F, 0);
digitalWrite(motor2B, 0);
}
void MUnit(int units, int throttle, int Steering) {
//terminal.println("Moving");
int countF = 0;
int countR = 0;
int pasF = digitalRead(A0);
int pasR = digitalRead(A1);
MDrive(0, 255 * constrain(throttle, -1, 1), Steering); // Jump Start Motors
delay(50); // Jump Starting
MDrive(0, throttle, Steering); // Set to main speed
if (abs(Steering) == 255) // steering only
{
while (countF <= units && countR <= units) {
switch (millis() / 1000) {
case breaksecs: //If code Runs for x number of seconds Break
units = 0;
break;
default:
if (pasR == digitalRead(A1)) {
pasR = digitalRead(A1);
} else {
pasR = digitalRead(A1);
countR++;
}
if (pasF == digitalRead(A0)) {
pasF = digitalRead(A0);
} else {
pasF = digitalRead(A0);
countF++;
}
}
}
} else // Forward and backward with some steering
{
while (countF <= units || countR <= units) {
switch (millis() / 1000) {
case breaksecs: //If code Runs for x number of seconds Break
units = 0;
break;
default:
if ((millis() / 1000) % 2) {
switch (constrain(throttle, -1, 1)) //Wall detection
{
case -1:
if (RangeB() <= 10) {
//terminal.println("Wall Detected Stopping");
MBrake();
units = 0;
} else if (RangeB() <= 50) {
//terminal.println("Slowing down");
MDrive(0, -60, Steering);
}
break;
case 1:
if (RangeF() <= 10) {
//terminal.println("Wall Detected Stopping");
MBrake();
units = 0;
} else if (RangeF() <= 50) {
//terminal.println("Slowing Down");
MDrive(0, 60, Steering);
}
break;
default:
break;
}
}
if (pasR == digitalRead(A1)) {
pasR = digitalRead(A1);
} else {
pasR = digitalRead(A1);
countR++;
}
if (pasF == digitalRead(A0)) {
pasF = digitalRead(A0);
} else {
pasF = digitalRead(A0);
countF++;
}
}
if (countF >= units) {
digitalWrite(motor1F, 0);
digitalWrite(motor1B, 0);
}
if (countR >= units) {
digitalWrite(motor2F, 0);
digitalWrite(motor2B, 0);
}
}
}
MBrake();
}
void MRUnit() {
switch (Trigger) {
case false:
break;
case true:
Serial.println("Starting Motor");
Blynk.syncVirtual(V10);
MUnit(RunitsF, Speed, Rsteer);
default:
break;
}
}
void MDrive(boolean Debug, int j, int k) {
switch (Debug) {
case 1:
Blynk.syncVirtual(V8, V9); //Debug remote drive //Very laggy
j = Y;
k = X;
default:
int A = 0, B = 0, C = 0, D = 0;
if (j == 0) {
A = 0, B = 0, C = 0, D = 0;
} else {
//Forward Backwards
A = constrain(j, 0, 255);
C = constrain(j, 0, 255);
B = abs(constrain(j, -255, 0));
D = abs(constrain(j, -255, 0));
//Steering
A = A - (abs(constrain(k, -255, 0)) * constrain(A, 0, 1));
C = C - (abs(constrain(k, 0, 255)) * constrain(C, 0, 1));
B = B - (abs(constrain(k, -255, 0)) * constrain(B, 0, 1));
D = D - (abs(constrain(k, 0, 255)) * constrain(D, 0, 1));
}
analogWrite(motor1F, A);
analogWrite(motor1B, B);
analogWrite(motor2F, C);
analogWrite(motor2B, D);
break;
}
}
void HitCheck() {
if (pulseIn(2, LOW)) {
switch (StartStop) {
case true:
StartStop = false;
//terminal.println("Falsed");
break;
case false:
StartStop = true;
//terminal.println("Trued");
break;
}
} else if (pulseIn(4, LOW)) {
switch (BoxLoad) {
case true:
BoxLoad = false;
//terminal.println("Tone");
break;
case false:
BoxLoad = true;
//terminal.println("NoTone");
break;
}
}
}
int RangeB() {
long pulseDuration; //variable needed by the ultrasound sensor code
int Distance; // Ultrasound distance in cm
// this main code runs repeatedly:
// 1. Produce a 15us (micro-second) HIGH pulse in Trig to trigger the sensor...
digitalWrite(trigPinF, LOW);
delayMicroseconds(2);
digitalWrite(trigPinF, HIGH);
delayMicroseconds(5);
digitalWrite(trigPinF, LOW);
// 2. Use pulseIn() function to measure the duration of the HIGH pulse in Echo
// every 58 us is an obstacle distance of 1 cm
pulseDuration = pulseIn(echoPinF, HIGH);
Distance = pulseDuration / 58;
return Distance;
}
int RangeF() {
long pulseDuration; //variable needed by the ultrasound sensor code
int Distance; // Ultrasound distance in cm
// this main code runs repeatedly:
// 1. Produce a 15us (micro-second) HIGH pulse in Trig to trigger the sensor...
digitalWrite(trigPinB, LOW);
delayMicroseconds(2);
digitalWrite(trigPinB, HIGH);
delayMicroseconds(5);
digitalWrite(trigPinB, LOW);
// 2. Use pulseIn() function to measure the duration of the HIGH pulse in Echo
// every 58 us is an obstacle distance of 1 cm
pulseDuration = pulseIn(echoPinB, HIGH);
Distance = pulseDuration / 58;
return Distance;
}