Servo with potentiometer with memory not working

Hi guys, im trying to do what this vid did: Ep.53 Arduino Projects - Potentiometer Servo Control & Memory - YouTube

using this code:

#include <Servo.h>
Servo Servo1;
int AnalogOut = 0;
int NewAnalogOut = 0;
int pin_Button = 9;
int pin_Button_State = 0;
int pin_Button_State_Last = 0;
int storage[800];
int storage_loc = 0;
int recording = 0;





void setup() {
  Serial.begin(9600);
  Servo1.attach(10);
  pinMode(pin_Button, INPUT);
  // put your setup code here, to run once:
}

void loop() {
  pin_Button_State = digitalRead(pin_Button);
  if (pin_Button_State != pin_Button_State_Last) {
    if (pin_Button_State == HIGH) {
      recording++;
      if (recording == 2) {
        storage[storage_loc] = 777;
        // put your main code here, to run repeatedly:
      }
    }
    delay(50);
  }
  pin_Button_State_Last = pin_Button_State;

  if (recording == 0) {
    int sensorValue = analogRead(A0);
    NewAnalogOut = map(sensorValue, 0, 1023, 0, 180);
    if (abs(NewAnalogOut - AnalogOut) > 2) {
      Servo1.write(AnalogOut);
      AnalogOut = NewAnalogOut;
      
    }
  }
  delay(1);
  if ( recording == 1) {
    recording = 1;
    if (storage_loc < 1000) {
      storage[storage_loc] = NewAnalogOut;
      delay(100);
      Serial.println(storage[storage_loc]);
      storage_loc++;
      
    }
  } else if (recording > 1) {
    while (1 == 1) {
      storage_loc = 0;
      if (storage_loc < 800 || storage[storage_loc] != 777) {
        Servo1.write(storage[storage_loc]);
        delay(100);
      }
    }
  }

}

After uploading there's also red text at the button which says: low memory available, stability problems may occur.
I can control the servo with the potentiometer just fine till i press the button then the servo just freezes no matter how much i turn the potentiometer.

Anyone know how to fix this?

Freezes until you reset the board, or just until you release the button?

You update the "Last" state every time through loop(). Why?

Consider making storage an array of byte, rather than int to save RAM.

The array is delared [800] but in the code You test if the index is less than 1000....

1 Like

You are telling your code to playback sample 0 an infinite number of times:

I think you only want to play back until you reach the 777 marker.

    storage_loc = 0;
    while (storage_loc < 800 && storage[storage_loc] != 777)) {
        Servo1.write(storage[storage_loc]);
        delay(100);
      }
  recording = 0; // Start over

Not a good idea to run servo power from the Arduino as the video shows or indeed even via a breadboard.
Give the servo some decent power, say 4 separate AA cells and run a common line from the aa negative back to the Arduino negative.
Signal to the Arduino pin 11 is ok.

Hi,
Can you post a link to data/specs of the servo?

Can you please post a schematic of your project?
Please do not use Fritzy, a hand drawn image will be fine.
Include ALL power supplies, component names and pin labels.

What power supply are you using?

Thanks.. Tom... :smiley: :+1: :coffee: :australia:

it freezes until i reset the board, after I press the button no matter how much i hold or press it the servo won't move again.

I replaced that code with your code and its still not working

I forgot the increment.

    storage_loc = 0;
    while (storage_loc < 800 && storage[storage_loc] != 777)) {
        Servo1.write(storage[storage_loc++]);
        delay(100);
      }
  recording = 0; // Start over

or

    for (storage_loc = 0; storage_loc < 800 && storage[storage_loc] != 777; storage_loc++) {
        Servo1.write(storage[storage_loc]);
        delay(100);
      }
  recording = 0; // Start over

Not sure exactly what you mean here as the video shows first press to "record" whatever servo movement, any successive press, reads back the recorded movement from memory.
Do you have everything setup exactly the same as the video...??
There is also a later video showing more advanced code but also, the use of a separate supply for the servo, admittedly a larger servo but.........

also this line needs to change to 800.

 if ( recording == 1) {
    recording = 1;
    if (storage_loc < 1000) {

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