tone() library calls fails on alternating tones - arduino

I have a Leonardo generating a square wave using tone(). I ran the output through an LC low pass filter and fed into into a recording jack. Everything works fine for one continuous tone. But if I try and vary the frequency, for example:
loop()
{
tone(8,1300);
delay(10);
tone(8,1500);
delay(10);
}
then every now and again (it varies, e.g. 14ms, 28ms, 71ms) the signal disappears for about 4ms. This happens even when no other code apart from the above is present, i.e. no serial port access etc.
How can I get the Leonardo to generate tones without these gaps appearing?

Related

Arduino LCD display does not display text

I'm using LCD for the first time and I'm unable to make it work. It lights the background but it does not show any text. I tried three times from the beginning, each time with a different tutorial, and none of the solutions worked. Is there any mistake in my wiring that I do not see?
Code:
#include <LiquidCrystal.h>
LiquidCrystal lcd(7, 8, 9, 10, 11, 12);
void setup() {
lcd.begin(16, 2);
lcd.print("hello, world!");
}
void loop() {
lcd.setCursor(0, 1);
lcd.print(millis()/1000);
}
Here are the links to the tutorials I used:
https://www.arduino.cc/en/Tutorial/HelloWorld
http://www.instructables.com/id/How-to-use-an-LCD-displays-Arduino-Tutorial/
https://learn.adafruit.com/adafruit-arduino-lesson-11-lcd-displays-1/breadboard-layout
I also attached a link to the picture with my wiring.
Maybe you just need to adjust contrast. I dont understand why you have tkat pot connected to + and - and center pin is left over. Also, get better wires. Connect that pot to contrast pins and try to adjust it.
It's quite difficult to look for each wiring as all your cables are bent and twisted. From here I can see that the big yellow cable is meant to lead to the slider. You left the end of it on the left-hand-side of the breadboard, which is NOT connected to the right-hand side.
This means: Connect your red cable on the lower left side (in the column "H") to your slider in column "D", between the other two cables.

Real time graph plotting using Processing

I have to plot a graph in processing by the feedback from encoder motors of the bot. so I have two variables basically left motor encoder and right motor encoder. I planned to vary on in x-axis and another in y-axis. While I went through some of the code on internet I found that, almost everyone has written the graph part code in serial event itself?
So my first doubt is why do they write it in serial event() function rather than void draw()? Another thing is when I tried to write my code for graph in void draw() it had a pseudo code something like this:
xpos1=0,ypos1=height;
void draw():
line(xpos1,ypos1,xpos,height-ypos);// obviously the data(xpos,ypos) is mapped with the width and height of the processing ide window.
xpos1=xpos;
ypos1=height-ypos;
if(xpos1>=width)
{
xpos1=0;
}
if(ypos1>=height)
{
ypos1=0;
}
So I get to see only a small dot traversing on processing ide window and I cannot see the older path that my line has travelled which in the case of the sites which I described when wrote the similar piece of code in serial event() they had a whole graph getting made on the processing window.
Where am I getting wrong? Also is there any alternative to plot the graph using void draw()? I want to vary both xpos as well as ypos as i get two feedbacks form left motor and right motor.
Screenshot of my attempted graph in different frames!
Image
Screenshot of one of the graphs made by somewhat the similar code displayed above but written in the serial event() available on the internet:
As stated in the comments, there are too many subquestions here.
Regarding the question relative to the code, there is one main line that is making the code much more complex than it has to be. You are trying to draw a line between each and every couple of numbers received by the two encoders. There is no need to do that. When plotting a graph, I personally use the point(x,y) function. It's much easier to implement for prototyping purposes, and adjusting the frameRate() at which the sketch is running, you won't notice the difference.
void draw() {
point(encoder1, encoder2);
if (encoder1 >= width) {
encoder1 = encoder1 - width;
}
if (encoder2 >= height) {
encoder2 = encoder2 - height;
}
}
A simple sketch like this one will do the job.
The other thing that is not quite clear is the initialisation of the variables. Usually you initialise a variable if it's continuously increasing, like time, but from your description you want to plot on the X axis one encoder, and on the Y axis the other encoder. So wouldn't it be better to map the values to start with in order not to have them go out of the canvas range?
Please edit the question so that the code is clear and concise, following these guidelines, and try to ask one question per post.

Arduino HC-SR04 pwm interfere

I have been struggling with arduino and ultrasonic sensor HC-SR04 and PWM.
I am using arduino pro mini. Also using NewPing library.
the newping library example works perfectly if I have just the sonar attached and I get about 170cm when pointing upwards towards the ceiling.
However if I add a pwm call on a different pin(pin 3 in this case) the sonar outputs maximum of 41cm, everything below that works, albeit bit noisily.
I have almost nothing connected to the pwm pin, there is only a capasitor and a resistor. I had a motor as well but removed it for debugging.
Illustration of my wiring:
code:
#include <NewPing.h>
#define TRIGGER_PIN 8 // Arduino pin tied to trigger pin on the ultrasonic sensor.
#define ECHO_PIN 7 // Arduino pin tied to echo pin on the ultrasonic sensor.
#define MAX_DISTANCE 200
NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE);
int p = 3;
void setup() {
// put your setup code here, to run once:
pinMode(p,OUTPUT);
Serial.begin(115200);
}
void loop() {
delay(50); // Wait 50ms between pings (about 20 pings/sec). 29ms should be the shortest delay between pings.
Serial.print("Ping: ");
int d = sonar.ping_cm();
Serial.print(d); // Send ping, get distance in cm and print result (0 = outside set distance range)
Serial.println("cm");
analogWrite(p,d);
}
The problem does disappear if I remove everything from the pin 3, but I don't see how this wiring can have any effect, especially with the motor missing.
Any ideas how to get the sonar and pwm working at the same time.
Check the pin color of your HC-SR04. Models with "brass" colored pins have a defect. They might and will return false readings. Models with "silver-y-ish" pins are more precise. The defect isn't there.
Also, try using pins 2 & 3 for the distance sensor and something else for the PWM.

Multiple Ultrasonic sensors with Arduino

I'm trying to create a robot using three HC-SR04 ultrasonic sensors and my Arduino Pro Mini but I've run into a few problems. In short the robot's function is as follows:
The robot is dual wheeled, with an H-bridge (SN754410) driving each wheel.
There's one HC-SR04 sensor on each side of the robot, the left one activates the left wheel motor when it detects a hand in front of it, vice versa for the right side.
i.e. To make the robot go forward, we place our hands near the left and right side of the robot, to make it turn right, we remove the right hand and keep the left one in place, vice versa for turning left, etc.
A third HC-SR04 is located the top of the robot, such that it activates a third motor when the user's hand is hovering above the robot.
My test code is as follows:
#include <NewPing.h>
#define SONAR_NUM 3 // Number of sensors.
#define MAX_DISTANCE 20 // Maximum distance (in cm) to ping.
NewPing sonar[SONAR_NUM] = { // Sensor object array.
NewPing(4, 5, MAX_DISTANCE), // Each sensor's trigger pin, echo pin, and max distance to ping.
NewPing(6, 7, MAX_DISTANCE),
NewPing(8, 9, MAX_DISTANCE)
};
#define ena1 10 //trigger for left motor H-bridge
//#define ena2 11 //trigger for right motor
//#define ena3 12 //for top motor
long sensors[3]; //array to store sensor distances
void setup() {
Serial.begin (115200);
pinMode(ena1, OUTPUT);
//pinMode(ena2, OUTPUT);
//pinMode(ena3, OUTPUT);
}
void loop() {
for (uint8_t i = 0; i < SONAR_NUM; i++) { // Loop through each sensor and display results.
delay(50); // Wait 50ms between pings (about 20 pings/sec). 29ms should be the shortest delay between pings.
sensors[i] = sonar[i].ping_cm();
}
Serial.println(sensors[0]);
if (sensors[0] > 0 && sensors[0] <= 20){
Serial.println("detected");
digitalWrite(ena1, HIGH);
}else{
Serial.println("NA");
digitalWrite(ena1,LOW);
}
}
As you can see, I'm using the NewPing.h library to collect the sensor data. After each iteration of the for loop, the distances detected by the sensors are stored in a sensor array. When a hand is placed about 15-20 cm away from a sensor, the arduino sends a digital "HIGH" trigger signal to the respective H-bridge, activating the respective motor (I only have one of these pins, "ena1", enabled in my code, the other two are commented for the test).
To test my code, I simply connected the H-bridge trigger pin "ena1" to an LED, this pin is activated by the sensor whose distance data is stored in variable "sensors[0]". However, after I compile and upload my code, I notice that the LED simply flickers faintly as I put my hand in front of the sensor. As if the LED is being turned on and off very fast.
The output from the serial monitor is as follows:
15
detected
0
NA
16
detected
0
NA
14
detected
As you can see, by putting my hand about ~15cm in front of the sensor, the sensor returns the correct distance and the "ena1" pin is set to high (as evidenced by "detected" being printed to the screen).
However, the sensor always returns a "0" value at the next iteration of the main loop (while my hand is still in front of the sensor), subsequently setting the "ena1" pin to LOW again, which might explain why the LED is being turned on and off so fast.
I'm not sure why this is happening... Interestingly, by removing the digitalWrite lines from the code, the sensor returns the correct values (i.e. no "0" value when my hand is in front of the sensor).
Any ideas on how I can fix this?
Thanks in advance!
This might be a hardware error. I've seen cases where, if the pins of the HC-SR04 were a brass (gold-ish) color, the sensor had a tendency to throw out a 0 for distance.
My suggestion is to get an other ultrasonic sensor, preferably with more silver-ish colored pins.
Your code looks good though!
Good luck!

Arduino UTFT graphing

I use the UTFT library together with the Onewire sketch to plot a temperature graph on an LCD (480 x 320 ):
for (int i=1; i<478; i++)
{
myGLCD.drawPixel((1+xi),119-(celsius) );
xi++;
delay(100);
}
This works, but I dont understand why is does not work when I remove the for loop. As far as I can read the code, the function should still draw the pixels without the for loop - it draws only one pixels without the loop -
Besides if I use the loop, the rest of the program is halted until the loop is done, which is bad, since I would like to plot the graph constantly.

Resources