r/arduino • u/Worldly-Actuator-832 • 4d ago
Software Help Is my code okay? (in desc)
/*
tests the following:
Potentiometer on VP(36)
Potentiometer on VN(39)
MQ2 A0 on 34, D0 on 35
Joystick X on 33, Y on 32, SW on 25
Ultrasonic Sensor Trig 26, Echo 27
Servo on 14
Ir on 12
DHT11 on 13
L298N on (look down) //L298NONLY LEFT
OLED SDA on 21, SCL on 22
*/
//one pushbutton on 19
//one pushbutton on 18
//one pushbutton on 5
const int pot = 36;
const int pot2 = 39;
const int MQAO = 34;
const int MQDO = 35;
const int JoyX = 33;
const int JoyY = 32;
const int JoySW = 25;
const int trig = 26;
const int echo = 27;
const int IR = 12;
const int button1 = 19;
const int button2 = 18;
const int button3 = 5;
const int IN1 = 17;
const int IN2 = 16;
const int IN3 = 4;
const int IN4 = 23;
const int InputPins[] = { pot, pot2, MQAO, MQDO, JoyX, JoyY, JoySW, echo, IR };
const int OutputPins[] = { IN1, IN2, IN3, IN4, trig };
const int BUTTONS[] = { button1, button2, button3 };
#include <Arduino.h>
#include <Adafruit_SSD1306.h>
#include <Adafruit_GFX.h>
#include <Wire.h>
Adafruit_SSD1306 display(128, 64, &Wire, -1);
#include <DHT.h>
DHT dht(13, DHT11);
#include <ESP32Servo.h>
Servo Servo;
#include "BluetoothSerial.h"
BluetoothSerial SerialBT;
float temperature;
float humidity;
int potVal;
int potVal2;
unsigned long duration;
int distance;
int Xval;
int Yval;
int ClickVal;
int angle;
int GasA;
int GasD;
int ir_value;
void setup() {
for (int i = 0; i < 9; i++) {
pinMode(InputPins[i], INPUT);
}
for (int i = 0; i < 5; i++) {
pinMode(OutputPins[i], OUTPUT);
}
for (int i = 0; i < 3; i++) {
pinMode(BUTTONS[i], INPUT_PULLUP);
}
Servo.attach(14);
Servo.write(0);
SerialBT.begin("COMPONENT TESTER");
dht.begin();
Serial.begin(115200);
if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
for (;;)
;
}
display.clearDisplay();
display.display();
display.setTextColor(SSD1306_WHITE);
display.setTextSize(2);
display.setCursor(0, 0);
display.println("COMPONENT");
display.println("TESTER");
display.println("INITIAZLING");
display.display();
delay(2000);
display.clearDisplay();
display.display();
}
void dhtRead() {
temperature = dht.readTemperature();
humidity = dht.readHumidity();
Serial.print("Temperature: ");
Serial.println(temperature);
Serial.print("Humidity: ");
Serial.println(humidity);
}
void dhtPrint() {
display.clearDisplay();
display.display();
dhtRead();
display.setCursor(0, 0);
display.println("DHT11");
display.print("Temperature: ");
display.println(temperature);
display.print("Humidity: ");
display.println(humidity);
display.display();
delay(1000);
}
void potentiometerRead() {
potVal = analogRead(pot);
potVal2 = analogRead(pot2);
Serial.print("Potentiometer Value 1: ");
Serial.println(potVal);
Serial.print("Potentiometer Value 2: ");
Serial.println(potVal2);
}
void potentiometerPrint() {
display.clearDisplay();
display.display();
potentiometerRead();
display.setCursor(0, 0);
display.println("Potentiometer");
display.print("First Value: ");
display.println(potVal);
display.print("Second Value: ");
display.print(potVal2);
display.display();
delay(1000);
}
void ReadUltrasonic() {
digitalWrite(trig, LOW);
delayMicroseconds(2);
digitalWrite(trig, HIGH);
delayMicroseconds(10);
digitalWrite(trig, LOW);
duration = pulseIn(echo, HIGH, 7000);
distance = duration * 0.034 / 2;
Serial.print("Distance: ");
Serial.println(distance);
}
void ultrasonicPrint() {
display.clearDisplay();
display.display();
ReadUltrasonic();
display.setCursor(0, 0);
display.println("Ultrasonic");
display.print("Distance: ");
display.println(distance);
display.display();
delay(1000);
}
void joyRead() {
Xval = analogRead(JoyX);
Yval = analogRead(JoyY);
ClickVal = digitalRead(JoySW);
Serial.print("X Value: ");
Serial.println(Xval);
Serial.print("Y Value: ");
Serial.println(Yval);
Serial.print("Click Value: ");
Serial.println(ClickVal);
}
void joyPrint() {
display.clearDisplay();
display.display();
joyRead();
display.setCursor(0, 0);
display.println("Joystick: ");
display.print("X Value: ");
display.println(Xval);
display.print("Y Value: ");
display.println(Yval);
display.display();
delay(1000);
}
void MQRead() {
GasA = analogRead(MQAO);
GasD = digitalRead(MQDO);
Serial.print("GAS VALUES: ");
Serial.print(GasA);
Serial.println(GasD);
}
void MQPrint() {
display.clearDisplay();
display.display();
MQRead();
display.setCursor(0, 0);
display.println("MQ2");
display.println("GAS VALUES: ");
display.println(GasA);
display.println(GasD);
}
void ServoMotor() {
for (int i = 0; i < 179; i++) {
Servo.write(i);
display.clearDisplay();
display.display();
display.println("Servo");
display.println("Angle: ");
display.print(i);
Servo.write(i);
delay(40);
}
}
void IRread() {
ir_value = digitalRead(IR);
Serial.print("IR VALUE: ");
Serial.println(ir_value);
}
void IRprint(){
display.clearDisplay();
display.display();
IRread();
display.setCursor(0,0);
display.println("IR");
display.println("IR VALUES: ");
display.println(ir_value);
}
void forward(){
digitalWrite(IN1,HIGH);
digitalWrite(IN2,LOW);
digitalWrite(IN3,HIGH);
digitalWrite(IN4,LOW);
}
void backward(){
digitalWrite(IN1,LOW);
digitalWrite(IN2,HIGH);
digitalWrite(IN3,LOW);
digitalWrite(IN4,HIGH);
}
void stop(){
digitalWrite(IN1,LOW);
digitalWrite(IN2,LOW);
digitalWrite(IN3,LOW);
digitalWrite(IN4,LOW);
}
void movement(){
display.clearDisplay();
display.display();
display.setCursor(0,0);
display.println("L298N");
display.println("Front");
forward();
delay(1000);
backward();
display.clearDisplay();
display.display();
display.setCursor(0,0);
display.println("L298N");
display.println("Back");
delay(10000);
stop();
display.clearDisplay();
display.display();
display.setCursor(0,0);
display.println("L298N");
display.println("Stop");
delay(10000);
}
/*
1 = dht
2 = ir
3 = joy
4 = MQ
5 = Ultrasonic
6 = potentiometer
*/
void loop() {
}
So, this is like my ninth project I myself am coding. I wanna see if the code is okay till now, I will add like a button mechanism to decide when one sensor is gonna do its things. Basically, im going to use buttons to decide with one button making the program go to the next sensor/module and yeah. I also have bluetooth printing left. So, i will fix the loop and bluetooth later.



