New stuff
Posted: Mon Nov 12, 2007 4:01 am
Good news! I believe I am done rewriting the microcontroller code. If you are curious here it is:
And, as you can see with the analogWrite calls I have the PWM stuff implemented. I am now able to control the speed of a vibe from my PC. This python script was able to take it from off to max in in a loop:
You can see that the high nibble is the device number 0x30. Devices 0x20,0x30, 0x40, and 0x70 all support PWM. Thats right, the same code will control 4 vibs. Then it will also do 4 on/off devices too. So, the electromagnet and shocker we already have build will work here too.
Just as a test, I went out and got the cheapest $5 bullet vibe I could find hooked it up to a DC power supply a 4.5v 1000mA and whew that baby was screaming! Normally it only have 2 AA batteries.
I am going to use pins 5, 4, 3, and 2 for the accelerometer and will post again how that is going. There will need to be some new microcontroller code for it but that shouldn't be too difficult.
That's about it for now.
Code: Select all
#define NUMDEVICES 8
// Mode 0 is OnOff and Mode 1 is Level (PWM)
int OnOffPins[] = {13, 12, 8, 7};
int LevelPins[] = {11, 10, 9, 6};
// Pins 5,4,3,2 are used for accelerometer
struct device {
int mode;
int pin;
int level;
};
struct device devArray[NUMDEVICES];
void setup()
{
int i, x;
Serial.begin(9600); // opens serial port, sets data rate to 115200bps
Serial.println("Tantalus -- ready to begin");
delay(100);
for (i = 0; i < NUMDEVICES; i++)
{
devArray[i].pin = 13 - i;
devArray[i].level = 0;
devArray[i].time = millis();
for (x = 0; x < sizeof(OnOffPins); x++)
{
if (devArray[i].pin == OnOffPins[x])
{
devArray[i].mode = 0;
pinMode(devArray[i].pin, OUTPUT);
}
}
for (x = 0; x < sizeof(LevelPins); x++)
{
if (devArray[i].pin == LevelPins[x])
{
devArray[i].mode = 1;
devArray[i].level = 0x0F;
pinMode(devArray[i].pin, OUTPUT);
}
}
}
}
void loop()
{
int i, readnext;
byte devnum, level, incomingByte;
if (Serial.available() > 0)
{
incomingByte = Serial.read();
devnum = (incomingByte & 0xF0) >> 4;
level = (incomingByte & 0x0F);
Serial.print(incomingByte);
if (devnum >= 0 && devnum < NUMDEVICES)
{
devArray[devnum].level = level;
Serial.print("D");
Serial.print(devnum);
Serial.print("L");
Serial.println(level);
}
}
// Loop through all devices and set
for (i = 0; i < NUMDEVICES; i++)
{
if (devArray[i].mode == 0)
{
digitalWrite(devArray[i].pin, devArray[i].level);
}
if (devArray[i].mode == 1)
{
switch(devArray[i].level)
{
case 0:
analogWrite(devArray[i].pin, 0);
break;
case 0x0F:
analogWrite(devArray[i].pin, 255);
break;
default:
analogWrite(devArray[i].pin, devArray[i].level << 4);
}
}
}
}
Code: Select all
x = 0x3f
while 1:
ser.write(struct.pack("B", int(x)))
time.sleep(.1)
x = x - 1
if x == 0x30:
while x != 0x3f:
ser.write(struct.pack("B", int(x)))
time.sleep(.1)
x = x + 1
Just as a test, I went out and got the cheapest $5 bullet vibe I could find hooked it up to a DC power supply a 4.5v 1000mA and whew that baby was screaming! Normally it only have 2 AA batteries.
I am going to use pins 5, 4, 3, and 2 for the accelerometer and will post again how that is going. There will need to be some new microcontroller code for it but that shouldn't be too difficult.
That's about it for now.