put some codes together.... but i dont know how.

Hello… I hope sombody can help me.
Iam a rookie in this territory and just learn how to “play” with arduino… but iam very frustrated for a few days, because I can not get it. My concept was to build a cat feeder. i was inspired by this two here:

but i dont wanna build exactly the same. the diffrence is that i work with an ultrasonic that recognize my cat and let the telegram and wifi part out. How i imagine how its should work:

step.1 melody ring out → ultrasonic+servo are ready for giving food.
step.2 cat hear the melody and want some food → ultrasonic recognize the cat.
step.3 servo react and give some food.
step.4 melody ring out again for the learning effect.
step.5 i need here a time clock… for my cat if she wanna retry it.
step.6 replay → step.1

My big problem here is the “void loop ()”… i guess…because i dont know how i can build this code for my ideas. i learn how the servo or buzzer react on the ultrasound but my mind do not know further.

I dont wanna the full answer… i need something to learn or understand this “write a code” thing.

I would be happy if someone can help me and i accept any criticism. :slight_smile:

my code look like this right now.

#include "pitches.h"
#define NO_SOUND


#include <Servo.h>
Servo servo;

int servoPin = 9;
int trigPin = 7;
int echoPin = 6;
int melodyPin= 3;
int pos = 0;
long distance;
long duration;

#define melodyPin 3
int melody[] = {
  NOTE_E7, NOTE_E7, 0, NOTE_E7,
  0, NOTE_C7, NOTE_E7, 0,
  NOTE_G7, 0, 0,  0,
  NOTE_G6, 0, 0, 0,
 
  NOTE_C7, 0, 0, NOTE_G6,
  0, 0, NOTE_E6, 0,
  0, NOTE_A6, 0, NOTE_B6,
  0, NOTE_AS6, NOTE_A6, 0,
 
  NOTE_G6, NOTE_E7, NOTE_G7,
  NOTE_A7, 0, NOTE_F7, NOTE_G7,
  0, NOTE_E7, 0, NOTE_C7,
  NOTE_D7, NOTE_B6, 0, 0,
 
  NOTE_C7, 0, 0, NOTE_G6,
  0, 0, NOTE_E6, 0,
  0, NOTE_A6, 0, NOTE_B6,
  0, NOTE_AS6, NOTE_A6, 0,
 
  NOTE_G6, NOTE_E7, NOTE_G7,
  NOTE_A7, 0, NOTE_F7, NOTE_G7,
  0, NOTE_E7, 0, NOTE_C7,
  NOTE_D7, NOTE_B6, 0, 0
};

int tempo[] = {
  12, 12, 12, 12,
  12, 12, 12, 12,
  12, 12, 12, 12,
  12, 12, 12, 12,
 
  12, 12, 12, 12,
  12, 12, 12, 12,
  12, 12, 12, 12,
  12, 12, 12, 12,
 
  9, 9, 9,
  12, 12, 12, 12,
  12, 12, 12, 12,
  12, 12, 12, 12,
 
  12, 12, 12, 12,
  12, 12, 12, 12,
  12, 12, 12, 12,
  12, 12, 12, 12,
 
  9, 9, 9,
  12, 12, 12, 12,
  12, 12, 12, 12,
  12, 12, 12, 12,
};
 
void setup() {
servo.attach(servoPin); 
 pinMode(melodyPin, OUTPUT);
 pinMode(trigPin, OUTPUT);
 pinMode(echoPin, INPUT);

}
 
void loop() {


  }

void moveServo() {
    Serial.println("moving servo");
  for (pos = 30; pos <= 120; pos += 1) { // goes from 0 degrees to 90 degrees
    servo.write(pos);              // tell servo to go to position in variable 'pos'
    delay(15);                     // waits 15ms for the servo to reach the position

  }
  delay(5000);  // keep the box open for a time interval based on the amount of food you want to deliver
  for (pos = 120; pos >= 30; pos -= 1) { // goes from 90 degrees to 0 degrees
    servo.write(pos);              // tell servo to go to position in variable 'pos'
    delay(15);                     // waits 15ms for the servo to reach the position
  }
}
 
void ultra(){
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);
  distance = duration*0.034/2;
  }

void playMelody(int melody[], int noteDurations[], int numberOfNotes ) {
  Serial.println("Playing melody");

  for (int thisNote = 0; thisNote < numberOfNotes; thisNote++) {

    // to calculate the note duration, take one second divided by the note type.
    //e.g. quarter note = 1000 / 4, eighth note = 1000/8, etc.
    int noteDuration = 1000 / noteDurations[thisNote];
    tone(melodyPin, melody[thisNote], noteDuration);

    // to distinguish the notes, set a minimum time between them.
    // the note's duration + 30% seems to work well:
    int pauseBetweenNotes = noteDuration * 1.30;
    delay(pauseBetweenNotes);
    // stop the tone playing:
    noTone(melodyPin);
  }

}

I was going to ask what your code currently does but I see it does nothing. You have defined several functions to do things but you have never called them so they never run.

The only thing that automatically runs is the loop() function. But in your program it is empty so it never does anything. Try putting calls to your functions in there. E.g. to get the moveServo() function to actually run and do something you need to put the line moveServo() in there. Same for the other functions.

Steve

I think this is not the point. it was my intention to let the “void loop” part empty because i dont know how i can work with my idea. i know i can write like:

void loop() {
  ultra();
  servo1.write(0);
  if(distance <= 10){
  servo1.write(90);
  }

but i dont have the knowlege to work this out further.

Nove9:
step.1 melody...
step.2 cat hear...
step.3 servo react...
step.4 melody ring...
step.5 i need here a time clock.... for my cat if she wanna retry it.
step.6 replay -> step.1

Research and study 'switch/case'. This provides a construct for doing things in a defined sequence. A sequential function chart (not the same thing! ) gives a visual representation of the concept. You'll likely have to break down the sequence into more steps.

Start with just your first two steps. You don't even need real outputs, the progress can be displayed on the serial monitor with print statements.