// // smugglers.cpp // Arduino Nano random voltage generator and 1 mA // current tester for EEPP Smugglers' Challenge. // // Written by Ted Burke, last updated 2-10-2018 // #include <Servo.h> Servo myservo; #define LEDPIN 2 #define PWMPIN 3 #define SERVOPIN 9 void setup() { // Generate random output voltage between (approximately) 0.5 and 2.0 V on pin D3 int vout; randomSeed(analogRead(0)); vout = random(25, 100); pinMode(PWMPIN, OUTPUT); analogWrite(PWMPIN, vout); // Red and green LEDs are both on pin D8 (set D8 high for green, low for red) pinMode(LEDPIN, OUTPUT); // Use internal 1.1 V voltage reference for analog input analogReference(INTERNAL); // The measured current is printed in mA Serial.begin(9600); // Initialise servo motor on pin D9 myservo.attach(SERVOPIN); } void loop() { float I_mA; // Calculate current from current sensing voltage on pin A7 I_mA = (2.0*1.1/1023.0)*analogRead(A7); // Print measured current Serial.print(I_mA); Serial.println(" mA"); // Check if measured current is within tolerance of target (1 mA) if (I_mA > 0.95 && I_mA<1.05) { // Light green LED and move servo to open position digitalWrite(LEDPIN, HIGH); myservo.write(45); } else { // Light red LED and move servo to closed position digitalWrite(LEDPIN, LOW); myservo.write(135); } // Update once every second delay(1000); }