# Galvo control using H-bridge ramping voltage and changing direction

Hi,
First of all an apology for asking for help on my first post.

So I am trying to control a galvonometer. It basically is a coil that moves something when a voltage is applied. With no voltage it sits in the zero position. When a voltage is applies it swing in one direction. Reverse the polarity and it moves the opposite way. So I want to get the galvo to move to one extreme wait a little while and slow swing from that point, through to the other extreme and then stop.

I have used “if” and “else if” to attempt to do this, but all I have is from the extreme in one direction and moving to zero. There is no moving in the opposite direction, stopping or waiting in the extreme position to start.

Any pointers would be greatly appreciated.

// This program is to make a galvo move from its centre zero position
// wait for a second at it’s furthest point
// then decrease the value until it reaches the middle
// at zero it will then switch direction
// and move until the most extreme point is reached
// at that point it should stop

//parameters
int galvo = 3; // sets the pin for the galvo to pin 3
int positiona = 260; // initial extreme galvo position
int increment = 1; //increment of galvo position
int dwelltime = 30; // the time used in loops to dwell before increment

void setup() {

//Setup galvo
pinMode(12, OUTPUT); // initiates motor channel a pin direction for galvo
pinMode(9, OUTPUT); //initiates Brake channel A pin
pinMode(galvo, OUTPUT); // sets pin as an output
}

void loop() {

//initialise galvo
if (positiona > 258){ // if positiona is greater than 258 then
digitalWrite(12, HIGH); // put galvo in forwards direction
digitalWrite(9, LOW); // Brakes off
analogWrite(galvo, positiona); // turn the galvo to position a
delay (1000); // wait 1 second
positiona = positiona - 5; // reduce positiona by 5
}

//stop galvo on one pass of fwd & bck
else if (positiona == 255 && 12, LOW){ // if speed is equal to 255 and is in reverse then;
digitalWrite(9, HIGH); //put the brakes on
}

// galvo from most positive position to middle
else if(positiona <= 255 && positiona >= 1 && 12,HIGH){ // if galvo positiona is less than 255 and greater than 1 and in the forward direction then;
digitalWrite (12, HIGH); // keep going forward
digitalWrite (9, LOW); //with the brakes off
analogWrite(galvo, positiona); // move galvo to positiona
delay (dwelltime); //hold the position for 30 ms
positiona = positiona - increment; // do some maths to reduce positive direction
}

// change direction if galvo is in the middle position
else if (positiona == 0){ // if speed a is 0 then
digitalWrite(12, LOW); // turn direction of galvo in reverse
digitalWrite(9, LOW); // keep the brakes off
analogWrite(galvo, positiona); // move the galvo to positiona
delay(dwelltime); // wait 30ms
positiona = positiona + increment; // increase the value for the negative direction
}

// move galvo from middle position to most negative position
else if (positiona >= 1 && positiona <= 254 && 12, HIGH){ // if position greater than or equal to 1 and less than or equal to 254 AND in the reverse direction then;
digitalWrite(12, LOW); // keep in reverse direction
digitalWrite(9, LOW); //keep brakes off
analogWrite(galvo, positiona); //move galvo to positiona value
delay(dwelltime); //wait 30ms
positiona = positiona + increment; // increase the direction by set amount

}

}

``````(positiona  >= 1 && positiona <= 254 && 12, HIGH)
``````

Are you sure that you understand exactly what lines like this are testing?

What do you think the “,” (comma) is doing? Comma operator - Wikipedia

I see what you mean now, I sort of figured that's where I went wrong, just not the reasoning. Thanks. Time to have another play about with the code, I have a new plan.