Hard Drive POV clock - arduino

I am building a hard drive POV clock. (google it, they are pretty cool)
I am working on the code for it, right now all i want to do is get the hang of making it do simple patterns with the RGB leds. I am wondering if anyone has any ideas on how to do something simple like make a red line rotate around the platter.
right now what i have is an interrupt that triggers a function.
int gLED = 8; // pins for RGB led strip
int rLED = 9;
int bLED = 10;
attachInterrupt(0, ledPattern, FALLING);
void ledPattern(){
digitalWrite(gLED, HIGH); // This will make a stable image of slice of the
delayMicroseconds(500); // platter, but it does not move.
digitalWrite(gLED, LOW);
}
That is the main part of the code (obviously I cut some stuff out that arduino requires)
What I am trying to figure out is how can make that slice rotate around the platter.
Eventually I will make the pattern more interesting by adding in other colors.
Any Ideas?

Try adding a delay at the start of ledPattern before turning the LEDs on, and increasing that delay each time the interrupt is called. To display a line at a particular angle, you will need to measure the time between subsequent interrupts (the time taken for one whole revolution of the LEDs) and make all the delays in the ledPattern routine proportional to this time.
Be aware that, if too much delay is added, the ledPattern routine won't have returned by the time the next interrupt is called; you might need to mitigate this, or at least work out exactly what the arduino environment is doing in this situation.

Related

Arduino servo without library

As education exercise on sending digital signals, I'm trying to code the pulse train for a servo without using the servo.h library.
The servo is a 9g micro servo. Hardware is correct, as many examples using servo.h work fine.
I have the following code. The problem is that the servo jerks around for 3 seconds rather than moving and holding still.
void loop() {
movePulse_1000();
delay(3000);
}
void movePulse_1000(){
Serial.print("Start movePulse_1000()\t\t");
for (int pulseCounter=0; pulseCounter<=150; pulseCounter++){
digitalWrite(pinServo,LOW);
delay(20); // between pulses
digitalWrite(pinServo,HIGH);
delayMicroseconds(1000);
}
Serial.println("End movePulse_1000()");
}
Using an analog servo, the average pulse width must be 1.5ms apart, and the duty cycle changes based on the desired position. To keep the servo where you want it, you must constantly refresh the servo data. This isn't a super simple task and that servo library is quite optimized. There's few reason not to use it.
It creates hardware timers and uses them to refresh the servos. This allows your regular code to appear to continue regularly even though it's being interrupted by the servo library to service the servos. Duty cycle, pwm frequency, and refresh rates all come into play. You'll have to look at the data sheet of the servo you are using to get the full details. But its not as simple you think and those delay/delaymicros functions you're using aren't always precise enough. That's why you would use times and overflow interrupts. Though most servos aren't too picky and you can get away with a ton of slop.
The library servo.h constantly sends pulses which means servo growling = battery consumption.
I changed your function to only rotate the servo without a timer from 0 to 180 degrees.
del = (7 * x) +500; - for my servo-pulse 500 to 1260us (calculated, not measured)
void movePulse(int x){
int del=(7*x)+500;
for (int pulseCounter=0; pulseCounter<=50; pulseCounter++){
digitalWrite(pinServo,HIGH);
delayMicroseconds(del);
digitalWrite(pinServo,LOW);
delay(20); // between pulses
}
}

Tone is not playing on Arduino Uno

I am trying to make a bot that follows a line with IR sensors and I also have a IR sensor to prevent collisions. In the if statement that stops movement, I also have it play a tone, but the problem is the tone doesn't play. I know that the if statement is being executed as the servos do stop as intended and resume after the object is removed. Also the speaker setup is confirmed working as other code with tones are working fine. I did hear it beep a few times when I was troubleshooting which is strange.
Here is the statement with the issue;
if (irDetect == 0) // Object detected
{
servoLeft.writeMicroseconds(1500); // Stop left servo
servoRight.writeMicroseconds(1500); // Stop right servo
tone(5, 4000, 100);
delay(100);
}
I cannot figure out the problem so any help is appreciated.
Tone and the ir library you are using are both using timer2. So you have a timer conflict. Both can't simultaneously have control over timer2. You'll either need to find a new library for one function or the other or modify one to use a different timer.

Arduino: Using analogRead() on Photoresistor to read LED with PWM

I have been working on a project with Arduino and have come across something that I find fascinating/confusing. So, I had to test something before constructing this project. I built a simple circuit that consists of just an LED and photoresistor. What I had to test was whether the photoresistor was capable of determining the brightness of an LED that was being dimmed through PWM. My initial expectation was that this would not work (the photoresistor would either read 1023 or 0 because PWM is achieved digitally). To my surprise, the photoresistor was able to accurately read the brightness of the LED (accurately to an extent -- this is simply based off of comparing the apparent brightness of the PWM LED with an LED placed in series with a certain resistor)! This is exactly what I wanted, but I am just curious as to why this works. I am not sure if my original doubt was due to a misunderstanding of photoresistors or PWM. Any help would be much appreciated. Thank you!
Here is the code I am running (I am not using the analogWrite() function because the project I am working on requires me to have a certain level of control over the PWM):
const int LED_PIN = 9;
const int PHOTO_PIN = 0;
//These values have been altered and tested
const int HIGH_TIME = 250;
const int LOW_TIME = 2750;
void setup()
{
pinMode(LED_PIN, OUTPUT);
pinMode(PHOTO_PIN, INPUT);
Serial.begin(9600);
}
void loop()
{
digitalWrite(LED_PIN, HIGH);
delayMicroseconds(HIGH_TIME);
digitalWrite(LED_PIN, LOW);
delayMicroseconds(LOW_TIME);
Serial.println(analogRead(PHOTO_PIN));
}
A "photoresistor" is a variable resistor. That is the simplest way to say it.
Just imagine your potentiometer, you can control its resistance by turning the little knob and then analogRead it. The photoresistor on the other side, changes it resistance depending on the light intensity. Because of that, the resistance will go up and down depending on your LED.
For "HOW" it actually works, see here.
Now, there are a couple of factors to consider:
1 - The ambient light of your room.
2 - The distance between your LED
So hope I helped you learn a little more about photoresistors!
The response time of the photo resistor is much slower than the PWM frequencies you are using. So it averages the on and off times of the LED and gives a resistance proportional to the average light. If you were using a photodiode with a fast response time, it would be able to "see" the LED go on and off.
I suggest that you don't try to write to the Serial port every time through the loop since it will quickly fall behind at 9600 baud. Perhaps write every 500 times through the loop.

How can I change the frequency and amplitude at the same time?

For example, if I need to set the frequency of 100 Hertz, I used the tone function to set the frequency using PWM. Now I need to vary the amplitude between 0 - 5 Volts using PWM. Is it possible to use tone and analogWrite together to fix frequency i.e 100 Hertz and analogWrite to change the amplitude?
Let me know if there is any another alternative because I tried the above commands tone and analog write on the same pin , it is not working.
You cannot do that using the tone function. Look into https://en.wikipedia.org/wiki/Pulse-width_modulation to get an idea of how a "Frequency" is generated using PWM. This image might be helpful:
If you really need to have different voltages, you will either need to add external components on the output pin (a simple voltage divider at least) or you generate the signal using solely analogWrite. Be aware that in the latter case timing issues may arise depending on the complexity of your code.
You can
use analogWrite and throw away the tone function, mimicing the behavior of the tone function;
use an external circuit.
The code that can suit the first solution is
#define TONE_PERIOD 10 /* Period in milliseconds */
unsigned long lastPulseStart;
byte volume;
void setup()
{
volume = ...;
lastPulseChange = millis();
}
void loop()
{
if (lastPulseStart - millis() < TONE_PERIOD/2)
analogWrite(pin, volume);
else if (lastPulseStart - millis() < TONE_PERIOD)
analogWrite(pin, 0);
else
lastPulseStart += TONE_PERIOD;
}
For the second solution you can use two pins, then use something to mix the outputs. For instance
an AND port (tone goes to an input, analogWrite to the other; the and port should be before the filter);
a buffer with enable input (tone goes to the enable, analogWrite to the line; this can be either before or after the filter);
if you are using an amplifier and it has an enable pin, use it (of course tone goes to the enable, analogWrite to the line);
an external ADC (in this case you will use the tone as the voltage reference, the volume should be passed as value for the ADC);
if you have attached a speaker directly to the output, you can send the analogWrite output to the speaker and put an NMOS on the other pin of the speaker, driven by the tone output.
If you need some schematics just ask..
Edit
Have you considered an digital potentiometer? After reading its description, it pretty much matches you need.
[...] controlling the volume on your stereo [...]
It functions quite like a normal potentiometer and I think it should work for you.
Original post
The answer is no, because tone will output pulses at a set frequency, but then analogWrite will ask the pin to output something else. The microcontroller might get "confused" and will probably not do what do you want it to do.
An more mechanical way to get over this problem is to use an potentiometer. It is pretty much a variable resistor that will change its resistance depending on the position of its knob. Why does this work is that no pulses are involved and the only thing you do is to lower the strengh of the signal while not modifying it.

Arduino Uno - Light Switch

The function of the program I am writing is to stream incoming analog data from a sensor to a program on my computer across the USB port. For a little fun, I've decided to add a button to the program that will turn on/off a lamp. The lamp will be connected to a relay, which is connected to the arduino. I know how to go about programming it, but I want to know if this will interrupt the sensor data transfer?
I will get the current state of the light (HIGH(1) or LOW(0)) from the arduino when the button is pressed, then write to the arduino (HIGH(1) or LOW(0)) depending on the current state. I have a 5 second delay between each loop of the arduino program, for reasons related to the sensor output; however, I think I'm going to have to change this so that when the button is pressed, it is not missed by the arduino loop, or is that not possible?
I thought I read somewhere that you can't transmit/receive streaming data on the same serial line...in which case, I will need the Mega.
You have to remember and think of the Arduino as a single threaded device. While it is doing anything it is not able to do anything else. Period! In regard to the serial port however, the buffer will still accept incoming data on RX, however if an overflow situation occurs whilst blocked, management is impossible.
See the following taken directly from the Arduino reference
Certain things do go on while the delay() function is controlling the Atmega chip however, because the delay function does not disable interrupts. Serial communication that appears at the RX pin is recorded, PWM (analogWrite) values and pin states are maintained, and interrupts will work as they should. Reference
Now in saying that when you are setting the delay to 5 seconds between loops ( delay(5000) ) you are essentially blocking it from doing anything else almost full stop.
The Arduino framework exposes a a counter ( millis() ) that basically runs from the moment of boot for roughly 50 days in increments of one (1) millisecond. See Arduino - millis()
In your application you would define (remember) what loop you were due to run & when the said loop had finished so to not allow the other loop to run until the millis() counter was a defined amount more than your count. (Remember to define the count as a long)
Then what you do is move your loops out into separate functions that will only execute if the if statement return true...
for example...
long interval = 5000; // Define interval outside of the main loop
long previousCount = 0; // Used to store milli count when finished
int loopPosition = 1;
void loop()
{
if ((long)millis() - previousCount >= 5000 )
// This if statement will only return true every 5 seconds (5000ms)
{
if (loopPosition == 1)
{
function_One();
previousCount = millis(); // Redefine previousCount to now
loopPosition++; // Increment loop position
}
else if (loopPosition == 2)
{
function_Two();
previousCount = millis();
loopPosition--; // Decrement loop position
}
}
// Do Anything Here You Want
// - While ever the if statement above returns false
// the loop will move to this without hesitation so
// you can do things like monitor a pin low / high scenario.
}
void function_One()
{
// Do Your First Loop
}
void function_Two()
{
// Do Your Second Loop
}
The above will stop any delay you are using from blocking awesomeness, and to more of a point, almost make delay obsolete if implemented correctly under the right scenarios.
In regard to your serial data comment, like i said at the top of this article, the Arduino can only do one thing at a time. Transmitting and receiving at exactly the same time is impossible even with the Mega. In saying that a board like the 'Uno' for example is only capable of one serial interface, where as the 'Mega' is capable of four.
Best of luck....
NB- For a beginner reading this, the following tutorial / example covers what i have above in fairly simple terms and is a great building block for further awesomeness! Arduino - Blink Without Delay

Resources