Skip to content

Instantly share code, notes, and snippets.

@Frijol
Created August 22, 2014 17:44
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 Frijol/ac1abc1ea34bdf91e9d9 to your computer and use it in GitHub Desktop.
Save Frijol/ac1abc1ea34bdf91e9d9 to your computer and use it in GitHub Desktop.
Arduino Control AutoFrost
const int logicpinx = 8;
const int pulsepinx = 9;
const int logicpiny = 10;
const int pulsepiny = 11;
const int frostpin = 12;
const int limitpinx = 3;
const int limitpiny = 4;
int limitpin;
int logicpin = logicpinx;
int pulsepin = pulsepinx;
int xory = 'x';
int pulsestate = HIGH;
long msg = 0; // variable to hold data from serial
char raw;
long dist;
int freq;
int freqx = 1;
int freqy = 1;
int count = 0;
int neg;
int frosting = 0;
int startup = 1;
int val = LOW;
void setup(){
pinMode(frostpin, OUTPUT);
pinMode(limitpinx, INPUT);
pinMode(limitpiny, INPUT);
pinMode(logicpinx, OUTPUT);
pinMode(pulsepinx, OUTPUT);
pinMode(logicpiny, OUTPUT);
pinMode(pulsepiny, OUTPUT);
Serial.begin(9600);
Serial.print("Program Initiated\n");
}
void loop(){
if(startup == 1){
while(val == LOW){
val = digitalRead(limitpinx);
digitalWrite(logicpinx, HIGH);
digitalWrite(pulsepinx, HIGH);
delay(1);
digitalWrite(pulsepinx, LOW);
delay(1);
}
val = LOW;
while(val == LOW){
val = digitalRead(limitpiny);
digitalWrite(logicpiny, LOW);
digitalWrite(pulsepiny, HIGH);
delay(1);
digitalWrite(pulsepiny, LOW);
delay(1);
}
startup = 0;
val = LOW;
}
// while(val != HIGH){
while (Serial.available()){ //while data is sent over serial...
delay(20);
msg = 0; //reset msg
raw = Serial.read(); //set raw as serial data
while (raw > 44){
switch(raw) {
case '0'...'9': //comma delimited data
msg = msg * 10 + (raw - '0'); //find # value of raw
break;
case '-':
neg = 1;
}
raw = Serial.read(); //next item from serial
}
if (neg == 1){
msg = -msg;
neg = 0;
}
Serial.print("msg: ");
Serial.println(msg);
dist = abs(msg) * 2; //be careful about 1000s!!!
if (xory == 'yn'){
if (msg == 0){
digitalWrite(frostpin, LOW);
}
else{
digitalWrite(frostpin, HIGH);
}
xory = 'x';
}
if (xory == 'x'){
logicpin = logicpinx;
pulsepin = pulsepinx;
limitpin = limitpinx;
if (msg > 0){
digitalWrite(logicpin, HIGH);
}
else{
digitalWrite(logicpin, LOW);
}
count = 0;
while (count < dist){
digitalWrite(pulsepin, HIGH;
delay(freq);
digitalWrite(pulsepin, LOW);
delay(freq);
count = count + 1;
val = digitalRead(limitpin);
}
freq = freqx;
xory = 'y';
}
else{
logicpin = logicpiny;
pulsepin = pulsepiny;
limitpin = limitpiny;
freq = freqy;
xory = 'y';
}
if (msg > 0){
digitalWrite(logicpin, HIGH);
}
else{
digitalWrite(logicpin, LOW);
}
count = 0;
while (count < dist){
digitalWrite(pulsepin, HIGH);
delay(freq);
digitalWrite(pulsepin, LOW);
delay(freq);
count = count + 1;
val = digitalRead(limitpin);
}
}
// }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment