I removed the line and the sketch is compiling but the servo isnt rotating and im not sure how to bring uo the array in the serial to see if the values from the potentiometer are actually being stored? Really appreciate the help, thanks.
What do you see if you print vall before writing it to the servo ?
if (currentMillis - previousMillis >= interval)
{
previousMillis = currentMillis;
for (i = 0; i < 720; i++)
{
storage[i] = val1;
}
}
Every 10 milliseconds the array is filled with values from 0 to 719. Is that what you mean to do ?
To display the values in the array use a for loop to read each of them, much as you do to write to them, and print the value read.
Something like
for (i = 0; i < 720; i++)
{
Serial.print(i);
Serial.print("\t");
Serial.println(storage[i]);
}
The serial is only printing 0 in the second column and looping between 0 and 255 in column 1. The potentiometer no longer has any effect.
#include <Servo.h>
Servo servo1;
int potpin1=0;
unsigned long previousMillis = 0;
int storage[360];
int val1;
const long interval = 10;
byte i;
void setup()
{
Serial.begin(9600);
servo1.attach(9);
}
void loop()
{
val1 = analogRead(potpin1);
val1 = map(val1, 0, 1023, 0, 180);
servo1.write(val1);
for (i = 0; i < 360; i++)
{
Serial.print(i);
Serial.print("\t");
Serial.println(storage[i]);
}
}
void record()
{
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval)
{
previousMillis = currentMillis;
for(i=0;i<360;i++)
{
storage[i]=val1;
}
}
}
also changed the array size to 360 to take up less memory
The serial is only printing 0 in the second column
Actually no surprise because you are putting the value of val1 in every position in the array
storage[i] = val1;
or you would be if you ever called the record() function
looping between 0 and 255 in column 1.
That's because i is declared as byte. Try declaring it as int
Wouldn't it make more sense to print the values in the array after recording them ? Perhaps a playback() function called when the array is full.
The variable i is a byte; and that can only hold values from 0..255. Change that to an int. And it's better if you don't use a global variable for that.
Further you never call record() so the array will always be empty.
Lastly, you have a millis() based delay of 10 milliseconds; after that your array will be filled in no time. If you want add a byte to the array every 10 ms
/*
record servo positions
returns:
true if completed, else false
*/
bool record()
{
static int index = 0;
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval)
{
previousMillis = currentMillis;
storage[index++] = val1;
}
if (index == 360)
{
// reset for the next time that we want to record
index = 0;
// indicate that recording is completed
return true;
}
return false;
}
And in loop(), you can use something like
void loop()
{
bool recordingComplete = false;
if (recordingComplete == false)
{
recording();
}
if (recordingComplete == true)
{
for (int i = 0; i < 360; i++)
{
Serial.print(i);
Serial.print("\t");
Serial.println(storage[i]);
}
}
// if your "mode" indicates that you want to record again, set recordingComplete to false
if(...)
{
recordingComplete = false;
}
}
The above is just a framework that needs massaging to fit in your code.
#include <Servo.h>
Servo servo1;
int potpin1=0;
unsigned long previousMillis = 0;
int storage[360];
int val1;
const long interval = 100;
int i;
void setup()
{
Serial.begin(9600);
servo1.attach(9);
}
void loop()
{
val1 = analogRead(potpin1);
val1 = map(val1, 0, 1023, 0, 180);
servo1.write(val1);
samp();
recall();
}
void samp()
{
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval)
{
previousMillis = currentMillis;
for(i=0;i<360;i++)
{
storage[i]=val1;
}
}
else if(i==359)
{
return;
}
}
void recall()
{
for (i = 0; i < 360; i++)
{
Serial.print(i);
Serial.print("\t");
Serial.println(storage[i]);
}
}
Trying to use the else if to retuen from the samp() function but it still keeps reseting the array
Think what happens when you call the samp() function.
If more than 10 milliseconds has elapsed since the function was last called then the for loop will run and fill the array with the current value of val1. What else could happen ?
What is it that you actually want to happen ?
Do you want the samp() function to save the value of val1 in one location and then exit ? If so, then why the for loop ? Why not just save the value in the current location, increment the index and return ? You do need bounds checking somewhere to avoid exceeding the bounds of the array but a for loop is not the way to do it.
#include <Servo.h>
Servo servo1;
int potpin1=0;
unsigned long previousMillis = 0;
int storage[360];
int val1;
const long interval = 200;
int i;
void setup()
{
Serial.begin(9600);
servo1.attach(9);
}
void loop()
{
val1 = analogRead(potpin1);
val1 = map(val1, 0, 1023, 0, 180);
servo1.write(val1);
delay(10);
if(i<359)
{
samp();
}
}
void samp()
{
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval)
{
previousMillis = currentMillis;
storage[i]=val1;
i++;
Serial.print(i);
Serial.print("\t");
Serial.println(storage[i]);
}
}
Code seems to be running how i want with the exception of it not reccording the values. All values of i are 0 as opposed to val1
All values of i are 0 as opposed to val1
Look at this portion of code
storage[i] = val1;
i++;
Serial.print(i);
Serial.print("\t");
Serial.println(storage[i]);
When you print storage you have already incremented i so you are not looking at the value of val1 that you just stored but at the value of the next location in the storage array which, being global, will have been initialised to zero.
Ok it seems to be working now. Thanks