//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// //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 //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// //Variable setup - Arduino pins for various functions int SensorPin0 = A0; int SensorPin1 = A1; int LedPin1 = 13; int LedPin2 = 12; int SensorValue0 = 0; int SensorValue1 = 0; int channel; //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// //Other Variables //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; //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// void setup() { Serial.begin(9600); // opens serial port, sets data rate to 9600 bps pinMode(LedPin1, OUTPUT); pinMode(LedPin2, OUTPUT); pinMode(SensorPin0, INPUT); pinMode(SensorPin1, INPUT); digitalWrite(LedPin1, HIGH); delay(500); digitalWrite(LedPin2, HIGH); } void loop() { delay(100); // This code receives the four characters from Python: if (Serial.available() > 0) { 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("ASCII Code"); Serial.println(LetterCode); Serial.println(d0); Serial.println(d1); Serial.println(d2); switch(LetterCode){ case 65: digitalWrite(13, HIGH); delay(500); digitalWrite(13, LOW); delay(500); break; case 66: Serial.println("Sensor Readings"); SensorValue0 = analogRead(SensorPin0); Serial.println (SensorValue0, DEC); SensorValue1 = analogRead(SensorPin1); Serial.println (SensorValue1, 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); } break; case 67: switch(d2){ case 53: channel = 3; break; case 52: channel = 6; break; case 51: channel = 9; break; case 50: channel = 10; break; case 49: channel = 11; break; } Serial.println("channel"); Serial.println(channel); digitalWrite(channel, HIGH); delay(1000); digitalWrite(channel, LOW); break; } }