// STM32 Nucleo-C031C6 SPI LCD Example
// Simulation: https://wokwi.com/projects/365549388158011393
#include "SPI.h"
#include "Adafruit_GFX.h"
#include "Adafruit_ILI9341.h"
#define TRIG_PIN D9
#define ECHO_PIN D8
#define BUZZER_PIN D10
#define LED_PIN_BLUE D6
#define LED_PIN_GREEN D5
#define LED_PIN_RED D4
#define TFT_DC 2
#define TFT_CS 3
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC);
void setup() {
Serial.begin(115200);
Serial.println("Hello, WOKWI");
tft.begin();
// Set up the ULTRSONIC pin
pinMode(TRIG_PIN, OUTPUT);
pinMode(ECHO_PIN, INPUT);
// Set up the buzzer pin
pinMode(BUZZER_PIN, OUTPUT);
pinMode(LED_PIN_BLUE, OUTPUT);
pinMode(LED_PIN_GREEN, OUTPUT);
pinMode(LED_PIN_RED, OUTPUT);
tft.setCursor(20, 120);
tft.setTextColor(ILI9341_RED);
tft.setTextSize(3);
tft.println("Hello USER");
}
void loop() {
long duration, distance;
// Sets the trigPin on HIGH state for 10 microseconds
digitalWrite(TRIG_PIN, HIGH);
delayMicroseconds(10);
digitalWrite(TRIG_PIN, LOW);
// Reads the echoPin, returns the sound wave travel time in microseconds
duration = pulseIn(ECHO_PIN, HIGH);
// Calculating the distance
distance = duration * 0.034 / 2;
// tft.setCursor(24, 160);
// tft.setTextColor(ILI9341_YELLOW);
// tft.setTextSize(3);
// tft.print("Dist=");
// tft.print(distance);
if (distance < 50)
{
digitalWrite(LED_PIN_RED, HIGH);
digitalWrite(LED_PIN_BLUE, LOW);
digitalWrite(LED_PIN_GREEN, LOW);
Serial.println(distance);
digitalWrite(BUZZER_PIN, HIGH);
delayMicroseconds(150);
digitalWrite(BUZZER_PIN, LOW);
// tft.setCursor(114, 160);
// tft.setTextColor(ILI9341_BLACK);
// tft.setTextSize(3);
// tft.print(distance);
// tft.setCursor(114, 160);
// tft.setTextColor(ILI9341_YELLOW);
// tft.setTextSize(3);
// tft.print(distance);
// tft.print(" cm");
tft.setCursor(20, 240);
tft.setTextColor(ILI9341_BLACK);
tft.setTextSize(3);
tft.print("Slow=");
tft.print(distance);
tft.setCursor(20, 280);
tft.setTextColor(ILI9341_BLACK);
tft.setTextSize(3);
tft.print("Safe=");
tft.print(distance);
tft.setCursor(20, 200);
tft.setTextColor(ILI9341_BLACK);
tft.setTextSize(3);
tft.print("STOP=");
tft.setCursor(20, 200);
tft.setTextColor(ILI9341_RED);
tft.setTextSize(3);
tft.print("STOP=");
tft.print(distance);
}
if (51 < distance && distance< 150)
{
digitalWrite(LED_PIN_BLUE, HIGH);
digitalWrite(LED_PIN_RED, LOW);
digitalWrite(LED_PIN_GREEN, LOW);
Serial.println(distance);
// tft.setCursor(114, 160);
// tft.setTextColor(ILI9341_BLACK);
// tft.setTextSize(3);
// tft.print(distance);
// tft.setCursor(114, 160);
// tft.setTextColor(ILI9341_YELLOW);
// tft.setTextSize(3);
// tft.print(distance);
// tft.print(" cm");
tft.setCursor(20, 280);
tft.setTextColor(ILI9341_BLACK);
tft.setTextSize(3);
tft.print("Safe=");
tft.print(distance);
tft.setCursor(20, 200);
tft.setTextColor(ILI9341_BLACK);
tft.setTextSize(3);
tft.print("STOP=");
tft.print(distance);
tft.setCursor(20, 240);
tft.setTextColor(ILI9341_BLACK);
tft.setTextSize(3);
tft.print("Slow=");
tft.setCursor(20, 240);
tft.setTextColor(ILI9341_BLUE);
tft.setTextSize(3);
tft.print("Slow=");
tft.print(distance);
}
if (distance > 150)
{
digitalWrite(LED_PIN_BLUE,LOW );
digitalWrite(LED_PIN_RED, LOW);
digitalWrite(LED_PIN_GREEN, HIGH);
Serial.println(distance);
// tft.setCursor(114, 160);
// tft.setTextColor(ILI9341_BLACK);
// tft.setTextSize(3);
// tft.print(distance);
// tft.setCursor(114, 160);
// tft.setTextColor(ILI9341_YELLOW);
// tft.setTextSize(3);
// tft.print(distance);
// tft.print(" cm");
tft.setCursor(20, 200);
tft.setTextColor(ILI9341_BLACK);
tft.setTextSize(3);
tft.print("STOP=");
tft.print(distance);
tft.setCursor(20, 240);
tft.setTextColor(ILI9341_BLACK);
tft.setTextSize(3);
tft.print("Slow=");
tft.print(distance);
tft.setCursor(20, 280);
tft.setTextColor(ILI9341_BLACK);
tft.setTextSize(3);
tft.print("Safe=");
tft.setCursor(20, 280);
tft.setTextColor(ILI9341_GREEN);
tft.setTextSize(3);
tft.print("Safe=");
tft.print(distance);
}
else{}
delay(10);
}
Loading
st-nucleo-c031c6
st-nucleo-c031c6