Thursday, August 23, 2012

Arduino control of polarization switcher

Here is the code that I used in the Arduino box to control the polarization rotator (switcher). (I think...)


// polarization rotator experiment
int incomingByte = 1; // for incoming serial data
int pwmPin = 9; // white wire
int dirPin = 11; // green wire
int statusLED = 13;
int delaymsec = 100;
void setup() {
Serial.begin(9600); // opens serial port, sets data rate to 9600 bps
// initialize pwm and dir pins as outputs.
pinMode(pwmPin, OUTPUT);
pinMode(dirPin, OUTPUT);
pinMode(statusLED, OUTPUT); // status LED
}
void loop() {
// check for incoming serial data:
if (Serial.available() > 0) {
// read the incoming byte:
incomingByte = int(Serial.read()) - 48;
}
if (incomingByte == 1) {
//volage-state ON, polarization NOT rotated, status LED ON
digitalWrite(statusLED,HIGH);
digitalWrite(pwmPin, HIGH);
analogWrite(dirPin, 127);
delay(delaymsec);
}
if (incomingByte == 2) {
//volage-state OFF, polarization IS rotated, status LED OFF
digitalWrite(statusLED,LOW);
digitalWrite(pwmPin, LOW);
analogWrite(dirPin, LOW);
delay(delaymsec);
}
incomingByte = 0; // no new data, skip all updates
}

No comments: