Code not working.

Ok everyone, Im stuck with some camera slider shutter code.
I recently acquired a camera slider and I want to use a stepper motor (bipolar) and the Arduino motor shield.

My code is meant to do:
Check the Pot value to set the speed of slider
Then Move the slider 1 interval
Then Focus the camera
Then Release the shutter
Then Repeat.

Currently I have an LED wired up and it seems to be registering the shutter, which works but the stepper motor is not working as it is not moving.Im using the Arduino motor shield.

Does anyone know whats wrong with the code.

Also the Potentiometer doesn't seem to be registering on A0 when the wiper (middle pin) is in A0, +,- on the other pins.

const int c_focus =2;
const int c_shutter =4;
int cfocusdelay = 300;
int cshutterdelay = 300;
int betweendelayC = 100;
int PotPin = A0;
int val = analogRead(PotPin);

int delaylegnth = (val);

void setup() {
  
  //establish motor direction toggle pins
  pinMode(12, OUTPUT); //CH A -- HIGH = forwards and LOW = backwards???
  pinMode(13, OUTPUT); //CH B -- HIGH = forwards and LOW = backwards???
  
  //establish motor brake pins
  pinMode(9, OUTPUT); //brake (disable) CH A
  pinMode(8, OUTPUT); //brake (disable) CH B
  pinMode(c_focus, OUTPUT); // Focus Camera
  pinMode(c_shutter, OUTPUT); // Triggers Camera Shutter
  pinMode(PotPin, INPUT); // Input for Potentiometer
  
 

 val = map(val, 0, 1023, 10, 30000);
 val = constrain(val, 10, 30000);
  
  
}

void loop(){

  
 
  digitalWrite(9, LOW);  //ENABLE CH A
  digitalWrite(8, HIGH); //DISABLE CH B

  digitalWrite(12, HIGH);   //Sets direction of CH A
  analogWrite(3, 255);   //Moves CH A
  
  delay(val);
  
  digitalWrite(9, HIGH);  //DISABLE CH A
  digitalWrite(8, LOW); //ENABLE CH B

  digitalWrite(13, LOW);   //Sets direction of CH B
  analogWrite(11, 255);   //Moves CH B
  
  delay(val);
  
  digitalWrite(9, LOW);  //ENABLE CH A
  digitalWrite(8, HIGH); //DISABLE CH B

 delay(35);

 digitalWrite(c_focus,HIGH); //Focus camera ON
 delay(cfocusdelay);
 digitalWrite(c_focus,LOW); //Focus camera OFF
 delay(betweendelayC);
 digitalWrite(c_shutter,HIGH);
 delay(300);
 digitalWrite(c_shutter,LOW);
 

 
  
  delay(val);

}

Thanks!!!

You only read the pot in setup(). Is that intentional ?

Then you assign the value read to a variable that you never use in the program. What is that all about ?

UKHeliBob:
You only read the pot in setup(). Is that intentional ?

Then you assign the value read to a variable that you never use in the program. What is that all about ?

Im really bad at programming and not sure where they go,if you can guide me or fix it It would be greatly appreciated!

Im really bad at programming

You'll never get any better if you don't fix that attitude.

ot sure where they go

There are two possible places. Code can go it setup() (or some function called from setup()), where it is executed once. Or, code can go in loop() (or some function called from loop()), where it is executed over and over.

Since it appears that you want to read the potentiometer often, setup() doesn't seem like the right place.

As for not using the variable that you stored the value in, well, USE IT!

So is it possible to Loop the checking of the potentiometer by itself or Do i just include it in the Void loop?

It would be possible to have a loop structure in setup() to read the pot at startup or it would be possible to read it one or more times in loop()

How do you want the program to work will determine how you implement it. The two choices are basically read the pot once and use the value or read the pot multiple times and use the values. So which is it ?

I want it to continually read the pot

TillMarkowitz:
I want it to continually read the pot

So put the reading of the pot in loop() rather than setup() so that the delay(), which is probably not the best way to do what you want, depends on the most recent value read from the pot.

If anyone here wants to go ahead and fix the code so it works, would be greatly appreciated as I wish to have it working by friday

Classroom assignment due Friday and no incentive to learn anything in the meantime?

No its a private project

Why not have a go at changing the program yourself ?

Put the analogRead() and map() sections of code into loop() and see what happens.

TillMarkowitz:
I wish to have it working by friday
...
No its a private project

Why Friday? Will Saturday not do?

Yeah, to be fair even if i've got it done by about Sunday it should be cool. School holidays coming and wanna shoot some Awesome Timelapses!

even if i've got it done by about Sunday it should be cool

Start now. Don't wait

Im really confused what i have to change to make it work

TillMarkowitz:
Im really confused what i have to change to make it work

See reply #7

Take these lines of code out of setup() and put them at the start of loop()

  int val = analogRead(PotPin);
  val = map(val, 0, 1023, 10, 30000);
  val = constrain(val, 10, 30000);

Other things may need changing or would be better done other ways or in other places, but start by reading the input each time through loop() by moving the code.

Well I have put that in.

Note Im using the official Arduino shield for motors and a Modded 28bjy-48 so its bipolar. The code now has the motor on full motor brake and not ever the shutter pin is triggering a led in place to detect the signal.

const int c_focus =2;
const int c_shutter =4;
int cfocusdelay = 300;
int cshutterdelay = 300;
int betweendelayC = 100;
int PotPin = A0;




void setup() {
  
  //establish motor direction toggle pins
  pinMode(12, OUTPUT); //CH A -- HIGH = forwards and LOW = backwards???
  pinMode(13, OUTPUT); //CH B -- HIGH = forwards and LOW = backwards???
  
  //establish motor brake pins
  pinMode(9, OUTPUT); //brake (disable) CH A
  pinMode(8, OUTPUT); //brake (disable) CH B
  pinMode(c_focus, OUTPUT); // Focus Camera
  pinMode(c_shutter, OUTPUT); // Triggers Camera Shutter
  pinMode(PotPin, INPUT); // Input for Potentiometer
  
 
 
}

void loop(){

  int val = analogRead(PotPin);
 val = map(val, 0, 1023, 10, 30000);
 val = constrain(val, 10, 30000);
  
  
 
  digitalWrite(9, LOW);  //ENABLE CH A
  digitalWrite(8, HIGH); //DISABLE CH B

  digitalWrite(12, HIGH);   //Sets direction of CH A
  analogWrite(3, 255);   //Moves CH A
  
  delay(val);
  
  digitalWrite(9, HIGH);  //DISABLE CH A
  digitalWrite(8, LOW); //ENABLE CH B

  digitalWrite(13, LOW);   //Sets direction of CH B
  analogWrite(11, 255);   //Moves CH B
  
  delay(val);
  
  digitalWrite(9, LOW);  //ENABLE CH A
  digitalWrite(8, HIGH); //DISABLE CH B

 delay(35);

 digitalWrite(c_focus,HIGH); //Focus camera ON
 delay(cfocusdelay);
 digitalWrite(c_focus,LOW); //Focus camera OFF
 delay(betweendelayC);
 digitalWrite(c_shutter,HIGH);
 delay(300);
 digitalWrite(c_shutter,LOW);
 

 
  
  delay(val);

}

Infact I just re-tried. I set the 25k pot (I think) (I know its no the right one) (Getting new one 2moro) to a diff position and pressed the Reset button. Now im getting 3 signals to the motor but still no movement. But i am getting a shutter release and focus pulse

Forget all the clever stuff for now. Write a simple program that just moves the motor using fixed delay()s. This will test whether the hardware is working.

When that works you can move on to altering the movement using the post.