Here is a script I use to do pulse width modulation for a electroplating tank
the pulse width is determined by a potentiometer to the analog in.
You could just as well control it with input from the USB port. There are lots of sample of how to do this in the Forum
// set pin numbers:
int platepin = 2;
int plateGrnd =3;
int unplatepin =4;
int unplateGrnd=5;
int DutyHi;
int DutyLo;
float DutyRatio;
void setup() {
// set the digital pin as output:
pinMode(13, OUTPUT);
pinMode(2, OUTPUT);
pinMode(3,OUTPUT); // go low when 6 is high
pinMode(4, OUTPUT);
pinMode(5, OUTPUT);// go low when 7 is high
}
void Sequence_plating()
{
digitalWrite(unplatepin,LOW);
digitalWrite(unplateGrnd,HIGH);
digitalWrite(platepin,HIGH);
digitalWrite(plateGrnd,LOW);
delay(10 *DutyHi/1023);
digitalWrite(platepin, LOW);
digitalWrite(plateGrnd,HIGH);
digitalWrite(unplatepin, HIGH);
digitalWrite(unplateGrnd,LOW);
delay(10*(1023-DutyHi)/1023);
}
void Get_Ratio()
{
DutyHi = analogRead(0);
DutyLo = 1023; //analogRead(1);
DutyRatio = (DutyHi+1/(DutyLo+1)); // 1023/0 max
if ( DutyHi <= 700) // warn that duty cycle is approching unplating
{
digitalWrite(13,HIGH);
}
else
{
digitalWrite(13,LOW);
}
}
void loop()
{
Get_Ratio();
Sequence_plating();
}