I dont know how to start my code for a project with many sensors

I've started a new project that requires input from a sonar aka a hand in front to open the lid using a servo. Once the hand is taken away, the servo will reset (closing the lid) which presses a limit switch and turns on a UV light which will turn on for either 10 minutes or until the lid is reopened.

here's what i have so far, mainly mocked up so its not correct
I've coded in python and an engineering language, but never c++

#include <iostream>
using namespace std;
void Sonar_Setup()  // run at setup once
    {
         #define TRIGGER_PIN  20  // Arduino trigger pin on sonar
         #define ECHO_PIN     21  // Arduino echo pin on sonar
         #define MAX_DISTANCE 200
    }
if 
    //if sonar value = true (values that would be close to the sonar))
    //the servo will set to the specific point needed
    else 
    //the servo will not move or will reset to the srtart point 
if 
    //if limit switch= yes 
    //uv light will turn on for 10 minutes or until values changes 
    else 
    //uv light will turn off 
int main()
 if ()

I'm using an Arduino Uno, micro limit switch, arduino servo, and Arduino Sonar and a UVC light

In the Arduino world we usually use the functions setup() and loop(); you don't have to but it will make life a lot easier.

Start by having a look at example code for your sonar. Once you have figured that out, you can have a look how to control a servo; again, there are examples.

Next combine those two.

Next there are also some examples in the IDE that demonstrate how to read a pin (your limit switch) and write a pin (your UV light). Study them, combine them and so on.

Lastly merge everything together.

Note:
Your use of the #defines is incorrect.

1 Like

https://docs.arduino.cc/built-in-examples/basics/BareMinimum

https://docs.arduino.cc/built-in-examples/digital/InputPullupSerial/

https://docs.arduino.cc/learn/electronics/servo-motors/

thanks so much for the help!

As you practice each stage, post any questions here. Step by step you will have your project working.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.