Tone is not playing on Arduino Uno - arduino

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.

Related

FreeRTOS Arduino MEGA 2560 vTaskDelay() not working

I have an Arduino MEGA 2560 running a FreeRTOS sketch using Arduino_FreeRtos. Everything appears to be working correctly except for vTaskDelay functions. Below is one of the areas I am having an issue.
taskENTER_CRITICAL();
MOTORS[5].motor(1, 120);
MOTORS[6].motor(1, 127);
taskEXIT_CRITICAL();
vTaskDelay(pdMS_TO_TICKS(9000));
taskENTER_CRITICAL();
MOTORS[5].motor(1, 0);
MOTORS[6].motor(1, 0);
taskEXIT_CRITICAL();
The first task critical section starts moving my motor forward. There is supposed to be a 9 second delay and then the motor is supposed to stop in the second task critical section. What is actually happening is the motor moves forward for ~1 second and then stops.
I do have INCLUDE_vTaskDelay enabled in the FreeRTOSConfig.h file.
What else can I look at to try and find out why vTaskDelay is not working?
Below is the definition for the clock. Per comment that the clock may not be correct. I can't decipher if the clock is correct or not. Hopefully someone can point out if this setting may be causing the issue.
The I think the problem is that 128000 >> (portUSE_WDTO + 11) is probably 0 (unless portUSE_WDTO is negative).
In order to get the correct timings you need to check that the tick rate is as expected (30ms). This is unlikely to be the case at the moment.

Display time with push button interrupt Arduino

I need to write a code that contains an interval loop but contains a push button interrupt that displays RTC values when activated. I have found a way to individually do each task i.e change pushbutton state, loop in intervals, display RTC value but I cannot seem to combine them and create a working. If someone can provide links or an explanation on how to accomplish this I would be so grateful.
If you do not use delay(interval); in your main loop, you can run as many tasks in parallel as you want. Understand the BlinkWithoutDelay sample, and try to extend it to two leds blinking independently. Or read a button while blinking.
And push button and interrupt does not go together well, BTW.
You even might add a small delay(2); to slow down polling the button pin.
This is usually fine for the other parallel running tasks and implements a very simple debounce mechanism.

Erase and reset my arduino

By some reason, I wrote the code below and upload it into my arduino, and obviously, I forget a delay function in loop method.
So, every time I pin the arduino into my computer, the simulated keyboard is always input something and can not stop.
I want to erase or reset my arduino, but how can I work it?
Type of board is Leonardo Micro Mini.
void setup() {
// put your setup code here, to run once:
}
void loop() {
// put your main code here, to run repeatedly:
Keyboard.print("Hello.");
}
I think the problem here is that it's difficult for you to upload, for instance, the BareMinimum example because of the Arduino printing "Hello." on your keyboard.
Is this right? If so, I see three possible solutions.
unplug the arduino, load the BareMinimum example, push the "Load" button and, when the compilation starts, plug the arduino back. It should start printing "Hello" but, in the end, it should load the sketch on the leonardo.
keep the reset button pressed; hit the load button in the IDE, then, as soon as the IDE says "loading" (after the compilation) release the switch. An alternative is to just press the reset button at the right moment, but I think that keeping it pressed is less annoying
use a programmer (or even another arduino - google for Arduino as ISP for more infos) to load the bootloader again on the leonardo
Bye

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

Hard Drive POV clock

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.

Resources