I am trying to make a red light green light but I need not make functions but I don't no how to.
If you want my code just ask and can you help please.
Example of a function:
void some_function(int light) {
if (light == 0) do_something();
else do_something_else();
}
How to write an Arduino function.
For more information, Google "arduino functions".
It's frustrating that too many members need so much help to make a proper posting. Read this link: How to get the best out of this forum - Using Arduino / Project Guidance - Arduino Forum
functions are documented in the Arduino reference on this site
Can you give me some code with it or a video of how to do functions please.
Code
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <ESP32Servo.h>
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
const int trigPin = 5;
const int echoPin = 18;
const int servoPin = 12;
//define sound speed in cm/uS
#define SOUND_SPEED 0.034
#define CM_TO_INCH 0.393701
long duration;
int distanceCm;
int distanceInch;
int servoVal = 180;
int buzzPin = 17;
int buzzTime = 100000;
int buzzTime2 = 10000;
int buzzTime3 = 1000;
int buzzTime4 = 4;
Servo myServo;
void setup() {
Serial.begin(115200);
pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
pinMode(echoPin, INPUT); // Sets the echoPin as an Input
pinMode(buzzPin, OUTPUT);
myServo.attach(servoPin);
myServo.write(servoVal);
if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println(F("SSD1306 allocation failed"));
for (;;);
}
delay(500);
display.clearDisplay();
display.setTextSize(2);
display.setTextColor(WHITE);
}
void loop() {
// Clears the trigPin
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
// Sets the trigPin on HIGH state for 10 micro seconds
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Reads the echoPin, returns the sound wave travel time in microseconds
duration = pulseIn(echoPin, HIGH);
// Calculate the distance
distanceCm = duration * SOUND_SPEED / 2;
// Convert to inches
distanceInch = distanceCm * CM_TO_INCH;
// Prints the distance in the Serial Monitor
Serial.print("Distance (cm): ");
Serial.println(distanceCm);
Serial.print("Distance (inch): ");
Serial.println(distanceInch);
display.clearDisplay();
display.setCursor(0, 0);
// Display distance in cm
display.print(distanceCm);
display.print(" cm");
// Display distance in inches
display.setCursor(0, 15);
display.print(distanceInch);
display.print(" in");
display.display();
// Display :)
display.setCursor(0, 30);
display.print(":)");
display.display();
// Display Good
display.setCursor(0, 45);
display.print("Good");
display.display();
// Display ----
display.setCursor(0, 55);
display.print("----");
display.display();
if (distanceCm > 0 && distanceCm < 100) {
Serial.println("Microsoft");
digitalWrite(buzzPin, HIGH);
delayMicroseconds(buzzTime);
digitalWrite(buzzPin, LOW);
delayMicroseconds(buzzTime);
}
else {
digitalWrite(buzzPin, LOW);
}
if (distanceCm > 100 && distanceCm < 500) {
Serial.println("Microsoft");
digitalWrite(buzzPin, HIGH);
delayMicroseconds(buzzTime2);
digitalWrite(buzzPin, LOW);
delayMicroseconds(buzzTime2);
}
else {
digitalWrite(buzzPin, LOW);
}
if (distanceCm > 500 && distanceCm < 1192) {
Serial.println("Microsoft");
digitalWrite(buzzPin, HIGH);
delayMicroseconds(buzzTime3);
digitalWrite(buzzPin, LOW);
delayMicroseconds(buzzTime3);
}
else {
digitalWrite(buzzPin, LOW);
}
delay(500);
}
You mean like post #2 and post #3?
What parts of your code do you want to place in functions? Does your code work the way you want it to, now?
Are you doing it because it's a school assignment?
No just for practice.
See all the code you put in setup? That is a function. See all the code you put in loop? That is a function. Copy the syntax and put it outside of everything else, not in loop or setup.
Void myFuntion (){
Your code here;
}
To “call” your function in loop
myFuntion();
Thanks.
I have both an Italian and English keyboard active hence myFuntion rather than myFunction!
Suggest you always look here: https://www.arduino.cc/reference/en/
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.