Ive made a pretty simple noisebox, which displays the 3 values on the LCD, (Frequency, Delay And On or Off), when one variable gets to a certain point (Delay which is controlled by a pot) the whole screen goes mad and i have to reset the arduino.
its just a buzzer which plays a tone dependant on variables, 1 pot and 1 ldr
do i need some caps to stabalise the signals or something?
Here is the code:
#include <Tone.h>
#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
Tone musicplayer;
int val = 0;//LDR variable
int val2 = 0;//POT variable
const int button = 8;
const int led = 13;
int buttonstate = 0;
void setup() {
pinMode(button, INPUT);
pinMode(led, OUTPUT);
pinMode(6, OUTPUT);
lcd.begin(16, 2);
lcd.print("FreQ Delay");
musicplayer.begin(6);
}
void loop(){
lcdscreen();
buttonstate = digitalRead(button);
//If button is pressed play music
if (buttonstate == HIGH) {
music();
}
}
void lcdscreen() {
val = analogRead(2);//Value from LDR
val2 = analogRead(0);//Value from POT
lcd.setCursor(12, 0);
buttonstate = digitalRead(button);
if (buttonstate == HIGH){
lcd.print("ON ");
digitalWrite(led, HIGH);
}
else {
lcd.print("OFF");
digitalWrite(led, LOW);
}
lcd.setCursor(0, 1);
lcd.print(val);
lcd.setCursor(5, 1);
lcd.print(val2);
}
void music() {
musicplayer.play(val, val2);
delay(val2);
}
For example, you need to set up 2 buffers for your sensors.
Above setup put these two lines:
char buffer[4]; // buffer may need to be bigger/smaller
char buffer1[4];// same thing
Then, to turn it into something the lcd can understand:
val = analogRead(2);//Value from LDR
val2 = analogRead(0);//Value from POT
itoa(val, buffer, 10); // turns the Integer from Val to a Char(text)
itoa(val2, buffer1, 10);
Now, instead of printing val and val2, use buffer instead:
The LCD controller does not understand letters and numbers as we know them. It understands that when it receives a certain pattern of '1's and '0's that it should display a certain pattern of dots on the screen that we will recognize as a letter or number. Each letter and number has a corresponding combination of '1's and '0's as defined by the American Standard Code for Information Exchange (ASCII).
When you want to display a letter on the LCD you can't actually send the letter itself to the LCD controller. Instead you must send the ASCII code representing the letter. Likewise when you want to display a number you can't send the number itself, but instead you must send the ASCII code representing that number. The itoa function (itoa --> Integer to ASCII) converts the number that you want to display to the ASCII code for that number.
I knew I forgot something.. lol, thanks Floresta:)
You can look up ASCII on Google and it will give you a list of what you'll get if you're using "Serial.write" or trying to print values directly, like you did.
For example
Serial.print(97);
Serial.print("a");
Serial.write(97); // both will put a Lowercase 'a'
The itoa function (itoa --> Integer to ASCII) converts the number that you want to display to the ASCII code for that number.
That should be ASCII codes. Pass a number like 243 to itoa, and you get back a string containing '2', '4', '3', and NULL. The print function that accepts strings wants them NULL terminated.
You can get some more interesting information about ASCII by using the ASCII chart link at http://web.alfredstate.edu/weimandn. Don't forget to scroll down to see the information below the chart.
Quote:
The itoa function (itoa --> Integer to ASCII) converts the number that you want to display to the ASCII code for that number.
That should be ASCII codes. Pass a number like 243 to itoa, and you get back a string containing '2', '4', '3', and NULL. The print function that accepts strings wants them NULL terminated.
You are correct! I originally had it worded as "... converts the integer that ..." and somehow decided to change 'integer' to 'number'.