HELP program arudino :c

Hi, we're making a vending machine. Using Arduino, we want it to do this: when motion and light are detected, a servomotor is activated, which moves the lever and sounds a buzzer to indicate that the container has been reset (filled), and at the same time, an LED is turned on.

(We have the code, but we don't know how to connect it efficiently and make it work;-:wink:
We're desperate for help or advice. We should do it for you. Thank you for your attention. :pensive_face: sob:

``#include <Servo.h>´´
Servo servo1;
int angulo1 = 0 ;
int TRIG = 10;
int ECO=9;

int DURACION;
int DISTANCIA;
//---------------------------------------------------------------------------------
const unsigned int PIN_ZUM = 9;
const int nTonos = 10;
const int tonos[] = {261, 277, 294, 311 ,330, 349,370, 392, 370,415,440, 466,494};
//[nota:buscar , un tono cancion pendiente :D]
//----------------------------------------------------------------------------------

void setup() {
pinMode(LED_BUILTIN, OUTPUT);
//<em><strong><strong><strong><strong><strong><strong><strong><strong><strong><strong><strong><strong><strong>|
pinMode(PIN_ZUM, OUTPUT);
//</strong></strong></strong></strong></strong></strong></strong></strong></strong></strong></strong></strong></strong></em>|
pinMode(TRIG,OUTPUT);
pinMode(ECO,INPUT);

servo1.attach(9) ;

Serial.begin(9600);

}
//_________________________________________________________________________________
//si el sensor ultrasonico y de luz detectan luz y proximidad , se ensendera el servo
//y cundo de una vuelta comple se detendra el servo y se ensendera un buzer y un led
//(si no se cumplen las condiciones de luz o movimiento al mismo tiempo se detendra el led y buzer)
//---------------------------------------------------------------------------------
void loop() {

digitalWrite (TRIG,HIGH);
delay(1);

digitalWrite (TRIG,LOW);

DURACION = pulseIn(ECO,HIGH);
DISTANCIA = DURACION /58.2;

Serial.print("Detectado");
delay(200);

//***FOTORESISTENCIA)***_____________________________
{
digitalWrite(LED_BUILTIN, HIGH);
delay(1000); // Wait for 1000 millisecond(s)
digitalWrite(LED_BUILTIN, LOW);
delay(1000); // Wait for 1000 millisecond(s)
}
//------------------------------------------------------------
if (LED_BUILTIN >0 && DISTANCIA > 0){

//***************SERVOMOTOR***************_______________
{
for(angulo1 = 0; angulo1 <= 180; angulo1 += 1)
{
servo1.write(angulo1);
delay(25);
}
for(angulo1 = 360; angulo1 >=0; angulo1 -=1 )
{
servo1.write( angulo1 );

delay(25);
}


}
}
//--------------------------------------------------------------------------
for(int tono = 0; tono < nTonos; tono++) {
tone(PIN_ZUM, tonos[tono]);
delay(1000);

noTone(PIN_ZUM);
delay(1000);
}
}`

So say many... Your code "compiles" but what do you observe not working as you intended?

#include <Servo.h>
Servo servo1;
int angulo1 = 0 ;
int TRIG = 10;
int ECO = 9;

int DURACION;
int DISTANCIA;
//---------------------------------------------------------------------------------
const unsigned int PIN_ZUM = 9;
const int nTonos = 10;
const int tonos[] = {261, 277, 294, 311, 330, 349, 370, 392, 370, 415, 440, 466, 494};
//[nota:buscar , un tono cancion pendiente :D]
//----------------------------------------------------------------------------------

void setup() {
  pinMode(LED_BUILTIN, OUTPUT);
  pinMode(PIN_ZUM, OUTPUT);
  pinMode(TRIG, OUTPUT);
  pinMode(ECO, INPUT);

  servo1.attach(9) ;
  Serial.begin(9600);
}
//_________________________________________________________________________________
//si el sensor ultrasonico y de luz detectan luz y proximidad , se ensendera el servo
//y cundo de una vuelta comple se detendra el servo y se ensendera un buzer y un led
//(si no se cumplen las condiciones de luz o movimiento al mismo tiempo se detendra el led y buzer)
//---------------------------------------------------------------------------------
void loop() {

  digitalWrite (TRIG, HIGH);
  delay(1);

  digitalWrite (TRIG, LOW);

  DURACION = pulseIn(ECO, HIGH);
  DISTANCIA = DURACION / 58.2;

  Serial.print("Detectado");
  delay(200);
 //***FOTORESISTENCIA)***_____________________________
  {
    digitalWrite(LED_BUILTIN, HIGH);
    delay(1000); // Wait for 1000 millisecond(s)
    digitalWrite(LED_BUILTIN, LOW);
    delay(1000); // Wait for 1000 millisecond(s)
  }
  //------------------------------------------------------------
  if (LED_BUILTIN > 0 && DISTANCIA > 0) {

    //***************SERVOMOTOR***************_______________
    {
      for (angulo1 = 0; angulo1 <= 180; angulo1 += 1)
      {
        servo1.write(angulo1);
        delay(25);
      }
      for (angulo1 = 360; angulo1 >= 0; angulo1 -= 1 )
      {
        servo1.write( angulo1 );
        delay(25);
      }
    }
  }
  //--------------------------------------------------------------------------
  for (int tono = 0; tono < nTonos; tono++) {
    tone(PIN_ZUM, tonos[tono]);
    delay(1000);

    noTone(PIN_ZUM);
    delay(1000);
  }
}

There are a bunch of delays() that aren’t helping you. You should probably look into the example sketch “BlinkWithoutDelay” and adapt your code to it.

1 Like

You also don’t want to use pulseIn() as it also blocks the code from doing anything else until it’s done. You should look for an alternative here in the forum as many others have had similar issues.

1 Like

Can we see the actual vending machine enclosure that you are mounting all this stuff into?

Oh, I see, I'll keep looking for more alternatives, thanks for the advice.

That's the main idea, it's a school project, we're just looking at the circuit and the code first.
:´)

You could say yes, I would appreciate any advice. :pensive_face:

What do you observe your project doing?

Multiple topics, same subject.

And leaving all the mechanical stuff till later?

1 Like