////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//CEB Testing Program
//Written at Factor e Farm, July 2011 by William Neal - william.j.a.neal@gmail.com
//This is a concept program designed to highlight and test the possibility of a Python-Based GUI control for the CEB Press
//Please feel free to offer feedback and suggestions
//
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//Interface from Python
//Python sends a four character code - one letter and three digits
//The letter tells the Arduino which mode to enter
//The three digits offer additional detail for certain modes.
int LetterCode = 0; // ASCII Code of Letter Being Sent
int d0 = 0; //Hundreds Digit
int d1 = 0; //Tens Digit
int d2 = 0; //Ones Digit
int Mode;
int Configuration = 0;
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//Variable setup - Arduino pins for various functions
int SensorPin0 = A0;
int SensorPin1 = A1;
int SensorPin2 = A2;
int LedPin = 13;
int SupplyPin = 12;
int SensorValue0 = 0;
int SensorValue1 = 0;
int SensorValue2 = 0;
//Assignment of pins for the PWM Solenoid drivers
int pwm_a = 11;
int pwm_b = 10;
int pwm_c = 9;
int pwm_d = 6;
int pwm_e = 3;
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//Other Variables
int i;
//Tolerance: magenetinc tolerance level for the sensors. This may need to change, depending on the exact location of the sensors in relation to the magnets
//and the value of the pull-up resistors being used.
int Tolerance = 80;
//Adjust this value to determine how long the Solenoid drivers are turned on.
//60 seconds is recommended for rigorous testing; 1 second for a quick test.
int Seconds = 1;
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void setup() {
Serial.begin(9600); // opens serial port, sets data rate to 9600 bps
pinMode(LedPin, OUTPUT);
pinMode(SupplyPin, OUTPUT);
pinMode(SensorPin0, INPUT);
pinMode(SensorPin1, INPUT);
pinMode(SensorPin2, INPUT);
digitalWrite(SupplyPin, HIGH);
}
void loop() {
// This code receives the four characters from Python:
if (Serial.available() > 0) {
// read the incoming byte:
LetterCode = Serial.read();
}
if (Serial.available() > 0) {
d0 = Serial.read();
}
if (Serial.available() > 0) {
d1 = Serial.read();
}
if (Serial.available() > 0) {
d2 = Serial.read();
}
Serial.flush();
//This prints the received letters for testing purposes
Serial.println(LetterCode);
Serial.println(d0);
Serial.println(d1);
Serial.println(d2);
//Here, we decide on the Mode based on the received LetterCode
switch (LetterCode){
case 65:
//In case it's an A, run the Blink program
Serial.println("Blink");
Mode = 1;
break;
case 66:
//In case it's a B, setup the sensors for testing
Serial.println("Sensor");
Mode = 2;
break;
case 67:
//In case it's a C, cycle the Mosfets through a test.
Serial.println("Mosfets");
Serial.println(d2);
Mode = 3;
//Subtract 48 from the received character to obtain a digit from 0 to 9:
//The configuration determines the nature of the test.
Configuration = d2-48;
}
switch(Mode){
case 1:
pinMode(13, OUTPUT);
digitalWrite(13, HIGH); // set the LED on
delay(1000); // wait for a second
digitalWrite(13, LOW); // set the LED off
delay(1000); // wait for a second
case 2:
// Read the value from the 3 sensors:
SensorValue0 = analogRead(SensorPin0);
Serial.println (SensorValue0, DEC);
SensorValue1 = analogRead(SensorPin1);
Serial.println (SensorValue1, DEC);
SensorValue2 = analogRead(SensorPin2);
Serial.println (SensorValue2, DEC);
// Turn the ledPins on or off depending on the reading of each sensor
if(SensorValue0 > Tolerance){
digitalWrite(13, HIGH);
}
if(SensorValue0 < Tolerance){
digitalWrite(13, LOW);
}
if(SensorValue1 > Tolerance){
digitalWrite(12, HIGH);
}
if(SensorValue1 < Tolerance){
digitalWrite(12, LOW);
}
if(SensorValue2 > Tolerance){
digitalWrite(11, HIGH);
}
if(SensorValue2 < Tolerance){
digitalWrite(11, LOW);
}
delay(10);
case 3:
Serial.println("Testing:");
Serial.println(Configuration);
// Set all channels to 0 as a precaution - it is important that opposite solenoids are not simultaneously engaged
//It is also important that both cylinders do not engage at the same time so as to cause internal damage to the machine
analogWrite(pwm_a, 0);
analogWrite(pwm_b, 0);
analogWrite(pwm_c, 0);
analogWrite(pwm_d, 0);
analogWrite(pwm_e, 0);
switch(Configuration){
case 0:
break;
case 1:
test_channel(pwm_a);
break;
case 2:
test_channel(pwm_b);
break;
case 3:
test_channel(pwm_c);
break;
case 4:
test_channel(pwm_d);
break;
case 5:
test_channel(pwm_e);
break;
Configuration = 0;
Mode = 0;
LetterCode = 0;
}
break;
}
}
void test_channel(int channel)
//This code engages a channel according to the time alloted to the variable 'Seconds'
{
Serial.print("Testing PWM channel ");
Serial.println(channel);
analogWrite(channel, 255);
Serial.println("PWM channel at max.");
for(int i=0; i<Seconds; i++){
delay(1000);
}
analogWrite(channel, 0);
Serial.println("PWM channel at 0.");
}