Hi all! Its my first post so please forgive me any mistakes.
Ive been playing with Arduinos since a year now in my spare time, Ive learned some basics by copying and merging some sketches found on the the internet. Now I am kind of able to write my own stuff, still basic though.
Ive done some small projects, and now Im working on a RGB laser spirograph, and I'm stuck. Here's the concept:
I am shooting an RGB laser on to a single motor (to begin with), with a fitted mirror at a little angle, projecting a circle (see attached pic).
The basic idea is to be able to split the circle in to a dfferent color sections, starting with basic 7 (Red, Green, Blue, Cyan, Magenta, Yellow and White) and the last section to be black (all lasers off).
I have fitted a 3144 hall effect sensor and a little magnet on the mirror to detect the revolution and measure RPM.
I am using arduino nano.
The laser is a cheap chinese 500mw RGB setup with TTL which is working fine when hooked up to digital outputs.
The motor is currently powered from an external regulated supply, to be implemented as PWM later.
So I figured three ways of making it work:
-
Measure the RPM or RPS, divide by the measuring time and then divide it by 8 and use the result as the delay time between switching colors.
-
Reset a loop everytime the hall sensor gets high.
-
Measure the time of one revolution, divide it by 8 and use the result as the delay time between switching colors.
I tried first two ways but failed totally as I am not that good with loops and the RPM/RPS did not work properly.
So I am currently working on the third way, and got some results, but only with low RPM, making the whole thing flicker and shake, and the idea is to keep it stable, regardless of the motor speed.
So here is my question - am I doing something wrong here? How to optimize the code?
I am sure what I want to achieve is probably one of the easiest thigs to do with arduino but I'm just not that good with it yet, so any other suggestions are welcome.
The code is a mixture of some other codes I found with my own additions.
Any help would be appreciated, thanks, Greg.
int Hallpin = 3;
int Redpin = 9;
int Greenpin = 10;
int Bluepin = 11;
unsigned long StartTime;
unsigned long EndTime;
unsigned long Duration;
unsigned long One8th;
byte TimerRunning;
void setup(){
pinMode (Hallpin, INPUT_PULLUP);
pinMode (Redpin, OUTPUT);
pinMode (Greenpin, OUTPUT);
pinMode (Bluepin, OUTPUT);
Serial.begin(9600);
}
void loop(){
if (TimerRunning == 0 && digitalRead(Hallpin) == LOW){ // button pressed & timer not running already
StartTime = micros();
TimerRunning = 1;
}
if (TimerRunning == 1 && digitalRead(Hallpin) == HIGH){ // timer running, button released
EndTime = micros();
TimerRunning = 0;
Duration = EndTime - StartTime;
One8th = Duration / 8 * 10;
Serial.print ("Revolution time in microseconds: ");
Serial.println (Duration);
Serial.println (One8th);
// color ring
// RED
digitalWrite(Redpin, HIGH);
delayMicroseconds(One8th);
digitalWrite(Redpin, LOW);
//GREEN
digitalWrite(Greenpin, HIGH);
delayMicroseconds(One8th);
digitalWrite(Greenpin, LOW);
//BLUE
digitalWrite(Bluepin, HIGH);
delayMicroseconds(One8th);
digitalWrite(Bluepin, LOW);
//CYAN
digitalWrite(Greenpin, HIGH);
digitalWrite(Bluepin, HIGH);
delayMicroseconds(One8th);
digitalWrite(Greenpin, LOW);
digitalWrite(Bluepin, LOW);
//MAGENTA
digitalWrite(Redpin, HIGH);
digitalWrite(Bluepin, HIGH);
delayMicroseconds(One8th);
digitalWrite(Redpin, LOW);
digitalWrite(Bluepin, LOW);
//YELLOW
digitalWrite(Redpin, HIGH);
digitalWrite(Greenpin, HIGH);
delayMicroseconds(One8th);
digitalWrite(Redpin, LOW);
digitalWrite(Greenpin, LOW);
//WHITE
digitalWrite(Redpin, HIGH);
digitalWrite(Greenpin, HIGH);
digitalWrite(Bluepin, HIGH);
delayMicroseconds(One8th);
digitalWrite(Redpin, LOW);
digitalWrite(Greenpin, LOW);
digitalWrite(Bluepin, LOW);
//BLACK
digitalWrite(Redpin, LOW);
digitalWrite(Greenpin, LOW);
digitalWrite(Bluepin, LOW);
delayMicroseconds(One8th);
digitalWrite(Redpin, LOW);
digitalWrite(Greenpin, LOW);
digitalWrite(Bluepin, LOW);
}
}