I am building the levitation machine found on page 159 in this book: https://www.scribd.com/doc/176638746/15-Dangerously-Mad-Projects-for-the-Evil-Genius
Everything is wired up correctly (I think), but I have two problems.
-
The IR sensor (signal on A0) is at about 4.7 V when nothing is in the way, and 5.11 V when blocked. I need help finding out how to tell the code to read A0 more sensitively and register A0 = 4.7 V as nothing in the way of the sensor, i.e. more current through the coil to raise the object. (If I pull the pin out from A0 and force it to be 0 V, then I get a gate voltage from pin 11 and some current through the coil).
-
But when I put 0 V on A0, I get about 1.5 V out of pin 11 (to the gate of my transistor). How do I make the magnitude of this voltage bigger? Because I separately determined that I will need about 3 V on the gate to get enough current to hold the object up.
Here is the code:
// Project 13 - Anti-gravity
// 15 Dangerous Projects for the Evil Genius
#define coilPin 11
#define irPin 13
#define sensorPin 0
int A = 2;
// Adjust B to improve stability
int B = 60;
int C = 20;
int D = 1000;
int maxPower = 255;
long powerCountThreshold = 300000;
int objectPresent = 0;
int monitoring = false;
void setup()
{
pinMode(coilPin, OUTPUT);
pinMode(irPin, OUTPUT);
pinMode(sensorPin, INPUT);
Serial.begin(9600);
Serial.println("Ready");
Serial.println("m - toggle monitoring");
Serial.println("B - increase B");
Serial.println("b - decrease B");
}
void loop()
{
static int count = 0;
static int oldPosition = 0;
static int ambient = 0;
static long powerCount = 0;
count ++;
if (count == 1000)
{
ambient = readAmbient();
count = 0;
objectPresent = (powerCount < powerCountThreshold);
powerCount = 0;
}
int raw = 1024 - analogRead(sensorPin);
// position from top (0) of sensor region to the bottom (650)
int position = raw - ambient;
// positive value means going downwards, negative going upwards
int velocity = position - oldPosition;
int power = position / A + velocity * B + C;
powerCount += power;
oldPosition = position;
// clip
if (power > maxPower) power = maxPower;
if (power < 0) power = 0;
checkSerial();
if (monitoring)
{
Serial.print(position); Serial.print(",");
Serial.println(velocity);
}
analogWrite(coilPin, power * objectPresent);
delayMicroseconds(D);
}
int readAmbient() //todo try speding up delay in micros
{
digitalWrite(irPin, LOW);
// allow time for LED and phototransistor to settle
delayMicroseconds(100);
int ambient = 1024 - analogRead(sensorPin);
digitalWrite(irPin, HIGH);
return ambient;
}
void checkSerial()
{
if (Serial.available())
{
char ch = Serial.read();
if (ch == 'm')
{
monitoring = ! monitoring;
}
if (ch == 'B')
{
B += 5;
Serial.println(B);
}
if (ch == 'b')
{
B -= 5;
Serial.println(B);
}
}
}
Thanks a bunch!