volatile byte rpmcount=0;
unsigned long timeold=0;
unsigned long timing=0;
int fillertime = 2750; //microseconds delay between the activation
//and deactivation of the RTS pin to get the bytes.
int extrafillertime=100; //microseconds delay used as a filler-up
//when no bytes are being received
byte timeout=50; //rounds turned
int noserialpause=180; //microseconds
byte standby=0;
byte lightbyte[16] = {255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255};
byte startbyte=0;
int endbyte=8;
int stepbyte=1;
byte mirrored=0;
byte motoron=0;
int timecounter=0;
int x=8;
int ontime=100;
int offtime=110;
int lineduration;
byte pixels=8;
byte filltimes=0;
byte mousenr[8];
byte multiplerounds=0;
int motorspeed;
int motorfactor=866;
int mirrordelay[8];
int buttonState = 0;
int lastButtonState = 0;
int buttonPushCounter = 0;
int temp;
byte frontbyte;
byte backbyte;
byte tempbyte[16];
#define motorPin 10
#define laserPin 9
#define RTSPin 7
#define mirroredButton 12
void setup()
{
//all delays + the linedurations add up to 50.040 microseconds.
//When one turn takes 53.000 microseconds, there are 2960 microseconds
//left before the first pixel. 2100 microseconds are used to buffer
//and read the incoming bytes.
lineduration = ((ontime+offtime)*16);
mirrordelay[0] =1;
mirrordelay[1] =6166 - lineduration;
mirrordelay[2] =6226 - lineduration;
mirrordelay[3] =6593 - lineduration;
mirrordelay[4] =6386 - lineduration;
mirrordelay[5] =6396 - lineduration;
mirrordelay[6] =6806 - lineduration;
mirrordelay[7] =4966 - lineduration;
Serial.begin(57600);
attachInterrupt(0, rpm_fun, RISING);
pinMode(motorPin, OUTPUT);
pinMode(laserPin, OUTPUT);
pinMode(RTSPin, OUTPUT);
pinMode(mirroredButton, INPUT);
}
void loop()
{
///Button the flip the image horizontally to change between
//back-side and front-side projection modes.
buttonState = digitalRead(mirroredButton);
if (buttonState != lastButtonState)
{
if (buttonState == HIGH)
{
buttonPushCounter++;
}
else{}
}
lastButtonState = buttonState;
if (buttonPushCounter % 2 == 0)
{
mirrored=1;
startbyte=7;
endbyte = -1;
stepbyte =-1;
}
else
{
mirrored=0;
startbyte=0;
endbyte = 8;
stepbyte =1;
}
if (motoron==0)
{
delay(500);
Serial.flush();
digitalWrite(RTSPin,0);
delay(5);
digitalWrite(RTSPin,1);
if (Serial.available()>0)
{
for (int x=0; x<=Serial.available(); x++)
{
int temp = Serial.read();
}
motoron=1;
multiplerounds=0;
analogWrite(10, 60);
}
}
if (rpmcount >= 1)
{
if (motoron==1)
{
timecounter++;
motorspeed = timing / motorfactor;
if (motorspeed>255)
{
motorspeed=60;
}
analogWrite(motorPin, 60-(5*(60-motorspeed)));
}
if (motoron==0)
{
timecounter=timeout;
digitalWrite(motorPin, LOW);
}
if (timecounter>=timeout)
{
motoron=0;
timecounter=0;
standby=1;
multiplerounds=3;
digitalWrite(motorPin, LOW);
//delay(16000);
digitalWrite(RTSPin,0);
delayMicroseconds(fillertime);
digitalWrite(RTSPin,1);
Serial.flush();
for (int x=0; x<16; x++)
{
lightbyte[x]=0;
}
}
multiplerounds++;
if (multiplerounds==1)
{
// Serial.write(42);
Serial.write(timing/1000);
// Serial.write(42);
}
if (multiplerounds==2)
{
digitalWrite(RTSPin,0);
delayMicroseconds(fillertime);
digitalWrite(RTSPin,1);
if (Serial.available()>18)
{
Serial.flush();
}
if (Serial.available()>17)//was 15
{
frontbyte=Serial.read();
for (int x=0; x<16; x++)
{
tempbyte[x]=Serial.read();
}
backbyte=Serial.read();
if (frontbyte==41 && backbyte==42)
{
for (int x=0; x<16; x++)
{
lightbyte[x]=tempbyte[x];
}
}
else
{
for (int x=0; x<16; x++)
{
lightbyte[x]=128;
}
//digitalWrite(RTSPin,0);
//delayMicroseconds(100);
//digitalWrite(RTSPin,1);
if (Serial.available()>0)
{
for (int x=0; x<=Serial.available(); x++)
{
int temp = Serial.read();
}
}
}
timecounter=0;
}
else
{
delayMicroseconds(noserialpause);
}
multiplerounds=0;
}
if (multiplerounds==1){
delayMicroseconds(fillertime+extrafillertime);
}
timing =(micros() - timeold);
timeold=micros();
rpmcount = 0;
for (byte linebytes=0; linebytes<16; linebytes=linebytes+2)
{
delayMicroseconds(mirrordelay[linebytes/2]);
for (x=startbyte; x!=endbyte; x=x+stepbyte)
{
digitalWrite(9, bitRead(lightbyte[linebytes+mirrored], x));
delayMicroseconds(ontime);
digitalWrite(9,0);
delayMicroseconds(offtime);
}
for (x=startbyte; x!=endbyte; x=x+stepbyte)
{
digitalWrite(9, bitRead(lightbyte[linebytes+mirrored+stepbyte], x));
delayMicroseconds(ontime);
digitalWrite(9,0);
delayMicroseconds(offtime);
}
}
}
}
void rpm_fun()
{
rpmcount++;
}
Pingback: Laser projector project, part 4: first code
Hi Bert,
Good to hear that my code is making itself useful for other people who are building similar projects. I would like to see the end result of your project, so please give me a ping when you’re finished!
Best regards,
Michiel