help needed for a beginner, panorama timelapse device with basic arduino element

Hello everyone

I am a artist from the netherlands. Recently I stumbled upon arduino and I ordered the starter kit to understand the arduino.
Half way through the project book i couldn’t resist to start a project myself with some basic projects scrambled together.
So the thing i would like to make is the next. I would like to use the servomotor and knocksensor (piezo) to create a panorama timelapse device. I will the use footage of the timelapse for an installation. Basiscly i can show a 180 degree movie with a beamer.

The idea is to attach the knocksensor against the speaker of my iPhone. Or maybe even a dslr. After the shuttersound the knocksensor should register the noise and with a small delay of 1,5 sec it should turn 90 degrees with the servomotor. Then it must wait on 90degrees for the next sound (knock)to come. When the next sound is picked up it should turn to the position of 180 degrees wait 1.5 seconds and turn back to 0 and wait for the next sound to come. Then after the next sound it should go to 90 again etc.

This is where i get stuck. I managed to get the knocksenor talking to the servo motor. But what happens is that it doesn’t stop at 90 degrees. Also the servo is making some noise and its vibrating a bit, it waits for the next sound then after a knock it goes to 0, 1.5 second delays, goes to 90, 1.5 seconds delay finaly to 180 stops and vibrates and waits for the next nock. Can anyone help me out with that part of the code?

Also it seems that the servo doesnt go the full 180 degrees. from 0 till 90 is perfect perpendicular, but then it doesnt go all the way, i would say it goes until 150/160 degrees. Is this a problem with the servo motor or is there something wrong with the code?

I’m verry new to this all. So probably these questions sound a bit noobish :*:, but I would be very helped if I could get this to work. I cannot offer any money, but a I can offer a lot of love, grace and honour :stuck_out_tongue:

the code:

// load servo library and create servo object
#include <Servo.h> 
Servo myServo;

// constants
const int knockSensor = A0; // the piezo is connected to analog pin 2
const int threshold = 400;  // threshold value to decide when the detected sound is a knock or not
const int servoPin = 9;  // the servo is connected to digital pin 6

// variables
int sensorReading = 0;      // variable to store the value read from the sensor pin
int pos = 0;
void setup() {
 myServo.attach(servoPin);  // attaches the servo on pin to the servo object 


 Serial.begin(9600);       // serial port
}

void loop() {
  
  
  // read the sensor and store it in the variable sensorReading:
  sensorReading = analogRead(knockSensor);    
  // if the sensor reading is greater than the threshold:
  if (sensorReading >= threshold) {

    myServo.write(0);
    Serial.println(sensorReading);
    Serial.println("1.5 Second delay");
    delay(1500);


  if (sensorReading >= threshold) 
    myServo.write(90);
    Serial.println(sensorReading);
    Serial.println("1.5 Second delay");
    delay(1500);
  


 if (sensorReading >= threshold) 
    myServo.write(180);
    Serial.println(sensorReading);
    Serial.println("1.5 Second delay");
    delay(1500);
  }
}

Please put your code in between code tags, it makes it easier to read.

Also the servo is making some noise and its vibrating a bit

My servo does this as well, I believe there is a way to adjust the trim of the servo to fix this, but I haven't not looked into it too much.

Also it seems that the servo doesnt go the full 180 degrees. from 0 till 90 is perfect perpendicular, but then it doesnt go all the way, i would say it goes until 150/160 degrees. Is this a problem with the servo motor or is there something wrong with the code?

This is how most servos operate. Most of them don't go a full 180 degrees, and trying to make them go farther could damage the servo. What you may want to try is getting your hands on a stepper motor. If you have any old cd-rom drives they usually have a stepper motor that you may be able to salvage.

You have a number of issues. First though, as laadams85 advised, since your servo apparently won't move 180 degrees (this is not uncommon) you'll need to find out what range it has. Search for posts by zoomkat - he has some servo test code he posts from time to time.

Looking at your if statements, the indenting you have used suggests that you expect each of them to control the four statements below them. You have not put in the apropriate curly braces to make this happen, except for the first if, which then contains the others.

Even if that were corrected, you don't reread the sensor between moves, so if a noise is detected, all three ifs will be true, so your servo will move from 0 to 90 to its limit whenever a noise is detected. You might get it to work in a flaky fashion if you read the noise level before each if.

You need to keep some state information and whenever you detect a noise, update that state. Let the state data tell you where the servo is set currently.

thnx for the advice :) i will look in to it. Maybe its better indeed to use a step motor.