Skip to content

Instantly share code, notes, and snippets.

@robinvanemden
Last active August 29, 2015 14:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save robinvanemden/9166e06321e528b17b85 to your computer and use it in GitHub Desktop.
Save robinvanemden/9166e06321e528b17b85 to your computer and use it in GitHub Desktop.
/* Control a Peltier Cooler over Arduino Motor Shield from Authorware
Developed for Arduino Uno with a Velleman VMA23 or KA03 Motor Shield
The circuit:
* Peltier elements connected to the motor outputs A and B
* 50 V (max.) external power supply connected to motor input
Created 2015
by Robin van Emden
This example code is in the public domain.
http://www.pavlov.io/2015/05/04/authorware-to-arduino-interface/
Parses <a,b,c,d> into an array of ints where:
a - 0 is port A, 1 is port B
b - 0 is cooling, 1 is warming
c - Power: 0 - 100 (translates into 0-255)
d - How long on continuously? Max = 4000 milliseconds
*/
byte byteRead;
int led = 13;
int field[30];
uint8_t idx = 0;
boolean done = false;
int pwm_a = 3; //PWM control for motor outputs 1 and 2
int pwm_b = 9; //PWM control for motor outputs 3 and 4
int dir_a = 2; //direction control for motor outputs 1 and 2
int dir_b = 8; //direction control for motor outputs 3 and 4
void setup()
{
// Turn the Serial Protocol ON
Serial.begin(9600);
//Set control pins to be outputs
pinMode(pwm_a, OUTPUT);
pinMode(pwm_b, OUTPUT);
pinMode(dir_a, OUTPUT);
pinMode(dir_b, OUTPUT);
}
void loop()
{
if (Serial.available() > 0)
{
done = parsePacket(Serial.read());
}
if (done) // ready to process a package
{
Serial.print("fields:\t");
Serial.println(idx);
for (int i = 0; i < idx; i++)
{
Serial.print(i);
Serial.print("\t");
Serial.println(field[i]);
}
Serial.println("done");
done = false;
if ( (field[0] == 0 || field[0] == 1 || field[0] == 2)
&& (field[1] == 0 || field[1] == 1)
&& field[2] >= 0 && field[2] <= 255
&& field[3] >= 1 && field[3] <= 4000) {
digitalWrite(led, HIGH); // Turn the LED on on the Arduino itself
if (field[0] == 0 || field[0] == 2) {
if (field[1] == 0) digitalWrite(dir_a, HIGH); // Set Element A to Cooling
if (field[1] == 1) digitalWrite(dir_a, LOW); // Set Element A to Warming
analogWrite(pwm_a, int(field[2]*2.55)); // Set power of Element A
}
if (field[0] == 1 || field[0] == 2) {
if (field[1] == 0) digitalWrite(dir_b, HIGH); // Set Element B to Cooling
if (field[1] == 1) digitalWrite(dir_b, LOW); // Set Element B to Warming
analogWrite(pwm_b, int(field[2]*2.55)); // Set power of Element B
}
delay(field[3]); // Set delay
// turn all off
analogWrite(pwm_a, 0);
analogWrite(pwm_b, 0);
digitalWrite(led, LOW); // Turn the LED off on the Arduino itself */
}
}
}
boolean parsePacket(int c)
{
boolean ready = false;
switch (c)
{
case '0'...'9':
field[idx] = field[idx] * 10 + c - '0';
break;
case ',':
idx++;
field[idx] = 0;
break;
case '<':
idx = 0;
field[idx] = 0;
break;
case '>':
idx++;
ready = true;
break;
default:
; // skip
}
return ready;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment