Build a Machine project – voltage rectifier and integrator

//
// machine.ino - voltage rectifier and integrator circuit
// for the DT009/1 EEPP build-a-machine project
// Written by Ted Burke, 11-2-2019
//

unsigned long t_start, t1, t2;
int n;

void setup()
{
  pinMode(2, OUTPUT);
  pinMode(3, OUTPUT);

  Serial.begin(9600);

  for(n=0 ; n<3 ; ++n)
  {
    digitalWrite(3, HIGH);
    delay(200);
    digitalWrite(3, LOW);
    delay(800);
  }

  for(n=0 ; n<25 ; ++n)
  {
    digitalWrite(3, HIGH);
    delay(20);
    digitalWrite(3, LOW);
    delay(20);
  }

  digitalWrite(2, HIGH);
  digitalWrite(3, LOW);

  t_start = micros();
  t1 = t_start;
  t2 = t_start;
}

void loop()
{
  int v1, v2, v_rect;
  double Vs = 0;

  while(1)
  {
    v1 = analogRead(6);
    v2 = analogRead(7);
  
    v_rect = v1 > v2 ? v1 : v2;
  
    t2 = micros();  
    Vs += (t2 - t1) * 1e-6 * v_rect * (5.0 / 1023.0);
    t1 = t2;
  
    //Serial.print("0 1024 ");
    Serial.print(v1);
    Serial.print(" ");
    Serial.print(v2);
    Serial.print(" ");
    Serial.print(v_rect);  
    Serial.print(" ");
    Serial.println(Vs);

    if (t2 - t_start > 10000000L) break;
  }

  digitalWrite(2, LOW);
  for(n=0 ; n<75 ; ++n)
  {
    digitalWrite(3, HIGH);
    delay(20);
    digitalWrite(3, LOW);
    delay(20);
  }

  while(1)
  {
    Serial.print("Integrated output voltage = ");
    Serial.print(Vs);
    Serial.println(" Vs");
    digitalWrite(3, HIGH);
    delay(200);
    digitalWrite(3, LOW);
    delay(800);
  }
}

Review week: MOSFETs, flyback diodes, capacitors, oscilloscope, optical sensors – whiteboard snapshots

This week, we discussed various elements of the coil gun project including MOSFET load control, flyback diodes, capacitance/capacitors, oscilloscopes, and the use of optical sensors to control the timing of pulses to the coils by tracking the movement of the slug through the barrel.

Random voltage generation and current measurement for Smuggler’s Challenge

//
// 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);
}