I had a viewer of my Youtube channel request the code accompanying this video. I made this video a long time back because I wanted to trigger a battery-powered airsoft gun from my robot. The Picoswitch is a solid state relay that works with pulse width modulation, same as a standard hobby servo. What this means is, you can supply power to the switch, and a load, such as an electric motor and you can toggle off and on any device up to 1 Amp from a standard R/C radio or microcontroller. If you need more current flow, try Dimension Engineering's Battleswitch. I know, the code says servo, but treat it exactly the same and it works.
Here ya go!// Controlling a servo position using a potentiometer (variable resistor)
// by Michal Rinott <http://people.interaction-ivrea.it/m.rinott>
#include <Servo.h>
Servo myservo; // create servo object to control a servo
int potpin = 0; // analog pin used to connect the potentiometer
int val; // variable to read the value from the analog pin
int pulse; // variable to show pulse width
void setup()
{
Serial.begin(9600); // begins serial communication with the computer
myservo.attach(12); // attaches the servo on pin 12 to the servo object
}
void loop()
{
val = analogRead(potpin); // reads the value of the potentiometer (value between 0 and 1023)
val = map(val, 0, 1023, 0, 179); // scale it to use it with the servo (value between 0 and 180)
pulse = map(val, 0, 179, 750, 2250); // get a pulse width for current pot position
myservo.write(val); // sets the servo position according to the scaled value
Serial.print("degrees ");
Serial.print(val);
Serial.print(" pulse" );
Serial.println(pulse);
delay(15); // waits for the servo to get there
}