LED Ultrasonic Sensor

Hello, I hope everyone is doing well. I have a problem with my code and was hoping someone could help me out. If you could that'd be wonderful.

Here is my code:

void setup() {
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
Serial.begin(9600);

void loop() {
const int trigPin = 9;
const int echoPin = 10;
float duration, distance;

}

digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration*.0343)/2;
Serial.print("Distance: ");
Serial.println(distance);
delay(100);

}

const int groundpin = 18; // analog input pin 4 -- ground
const int powerpin = 19; // analog input pin 5 -- voltage
const int xpin = A3; // x-axis of the accelerometer
const int ypin = A2; // y-axis
const int zpin = A1; // z-axis (only on 3-axis models)

int ledPin = 3; // LED is plugged in at pin 3
int motionSensorValue;
int ypinacc = 0;

int low = 275; // the severity will be considered low when the motion detected is 2
int mild = 325; // the severity will be considered low when the motion detected is 4
int severe = 375; // the severity will be considered low when the motion detected is 8

int ledState = LOW;
long intervalLow = 1000;
long intervalMild = 5000;
long intervalSevere = 9000;

void setup()
pinMode (ypin, INPUT); // initialized motion sensor as input
pinMode (ledPin, OUTPUT); // initialized digital pin as output
Serial.begin(9600);
pinMode(groundpin, OUTPUT);
pinMode(powerpin, OUTPUT);
digitalWrite(groundpin, LOW);
digitalWrite(powerpin, HIGH);
}

void loop() {
ypinacc = analogRead(ypin);
// print the sensor values:
Serial.print(analogRead(xpin));
// print a tab between values:
Serial.print("\t");
Serial.print(analogRead(ypin));
// print a tab between values:
Serial.print("\t");
Serial.print(analogRead(zpin));
Serial.println();
//digitalWrite(ledPin, HIGH);

if (ypinacc < low) { //no seizure
digitalWrite(ledPin, LOW);
// the LED will remain off if no abnormal motion is detected

} else if (ypinacc >= low && ypinacc < mild) { //low
intervalLow = millis();
digitalWrite(ledPin, intervalLow);
/* If the value of the sensor is greater than low but less than mild, the LED will
* light up at a brightness of 1000 and stay on for 4 seconds.
*/

} else if (ypinacc >= mild && ypinacc < severe) { //mild
intervalMild = millis();
digitalWrite(ledPin, intervalMild);
/* If the value of the sensor is greater than mild but less than severe, the LED
* will light up at a brightness of 3000 and stay on for 7 seconds.
*/

} else if (ypinacc >= severe) { //severe
intervalSevere = millis();
digitalWrite(ledPin, intervalSevere);
/* If the sensor value is greater than the severe value, the LED will light
* up at a brightness of 3000 and stay on for 10 seconds.
*/
}

Please follow the advice given in the link below when posting code , use code tags to make it easier to read and copy for examination

Hello
What shall your sketch do?

It should see an object and blink the LED.

Sorry, this is my first time on Arduino and making a project using an Arduino. I don't know how to code this and am trying my best.

An Arduino sketch can have only 1 setup() function and 1 loop() function. Are you attempting to merge 2 different codes? If so, see here.

void setup()
{
   pinMode(trigPin, OUTPUT);
   pinMode(echoPin, INPUT);
   Serial.begin(9600);

   void loop()
   {
      const int trigPin = 9;
      const int echoPin = 10;
      float duration, distance;

You must declare variables trigPin and echoPin before you use them.

If you use the IDE autoformat tool (ctrl-t or Tools, Auto Format) you will see that the first loop() function is inside the first setup() function. Not legal and an error.

intervalLow = millis();
      digitalWrite(ledPin, intervalLow);

What do you expect to happen when you digitaWrite a long to a pin?

How do you declare the variables trigPin and echoPin? What did you mean by, "What do you expect to happen when you digitaWrite a long to a pin?"

const int trigPin = 9;
const int echoPin = 10;

Those lines declare (create) the variables. Declare means that you give the variables names, allocate memory for them (int data type takes 2 bytes) and optionally give the variables values (initialize them). That must be done before the variables can be used in the code.

A long data type can hold a number -2,147,483,648 to 2,147,483,647. The digitalWrite() function writes a LOW (0) or HIGH (1) to a pin.

/* If the value of the sensor is greater than low but less than mild, the LED will
         light up at a brightness of 1000 and stay on for 4 seconds.
      */

Like I said, digitalWrite will write a 1 or 0 to a pin. That can turn a LED off or turn it on full brightness. To dim a LED you would use analogWrite() and the values for analogWrite() are 0 to 255. I do not know where you get the 4 seconds.

Here is your code with the variable declarations all before setup(), the extra setup() and loop() functions removed and the curly brackets so that the code will compile.

Don't know if it does what you want, but it does compile.

Posted in code tags and formatted with the IDE auto format tool.


const int trigPin = 9;
const int echoPin = 10;
float duration, distance;

const int groundpin = 18; // analog input pin 4 -- ground
const int powerpin = 19; // analog input pin 5 -- voltage
const int xpin = A3; // x-axis of the accelerometer
const int ypin = A2; // y-axis
const int zpin = A1; // z-axis (only on 3-axis models)

int ledPin = 3; // LED is plugged in at pin 3
int motionSensorValue;
int ypinacc = 0;

int low = 275; // the severity will be considered low when the motion detected is 2
int mild = 325; // the severity will be considered low when the motion detected is 4
int severe = 375; // the severity will be considered low when the motion detected is 8

int ledState = LOW;
long intervalLow = 1000;
long intervalMild = 5000;
long intervalSevere = 9000;


void setup()
{
    Serial.begin(9600);
   pinMode(trigPin, OUTPUT);
   pinMode(echoPin, INPUT);  
   pinMode (ypin, INPUT); // initialized motion sensor as input
   pinMode (ledPin, OUTPUT); // initialized digital pin as output
  
   pinMode(groundpin, OUTPUT);
   pinMode(powerpin, OUTPUT);
   digitalWrite(groundpin, LOW);
   digitalWrite(powerpin, HIGH);
}

void loop()
{
   digitalWrite(trigPin, LOW);
   delayMicroseconds(2);
   digitalWrite(trigPin, HIGH);
   delayMicroseconds(10);
   digitalWrite(trigPin, LOW);
   duration = pulseIn(echoPin, HIGH);
   distance = (duration * .0343) / 2;
   Serial.print("Distance: ");
   Serial.println(distance);
   delay(100);

   ypinacc = analogRead(ypin);
   // print the sensor values:
   Serial.print(analogRead(xpin));
   // print a tab between values:
   Serial.print("\t");
   Serial.print(analogRead(ypin));  // ****** why read Y again?
   // print a tab between values:
   Serial.print("\t");
   Serial.print(analogRead(zpin));
   Serial.println();
   //digitalWrite(ledPin, HIGH);

   if (ypinacc < low)   //no seizure
   {
      digitalWrite(ledPin, LOW);
      // the LED will remain off if no abnormal motion is detected
   }
   else if (ypinacc >= low && ypinacc < mild)     //low
   {
      intervalLow = millis();
      digitalWrite(ledPin, intervalLow);
      /* If the value of the sensor is greater than low but less than mild, the LED will
         light up at a brightness of 1000 and stay on for 4 seconds.
      */
   }
   else if (ypinacc >= mild && ypinacc < severe)     //mild
   {
      intervalMild = millis();
      digitalWrite(ledPin, intervalMild);
      /* If the value of the sensor is greater than mild but less than severe, the LED
         will light up at a brightness of 3000 and stay on for 7 seconds.
      */
   }
   else if (ypinacc >= severe)     //severe
   {
      intervalSevere = millis();
      digitalWrite(ledPin, intervalSevere);
      /* If the sensor value is greater than the severe value, the LED will light
         up at a brightness of 3000 and stay on for 10 seconds.
      */
   }
}

Thank you so much, this helps more than you can imagine.