Hello world!
I'm new in this forum and I don't know if this topic is the right position, however I'm here to ask if someone could verify if the following code for Arduino IDE is correct. The project must to measure distance in centimetres with the help of a pushbutton and a LCD display. The code is commented in italian.
1 //Misuratore di distanza con sensore ad ultrasuoni SRF05 azionato da un pulsante
2 //che permette di visualizzare il valore misurato attraverso un LCD 16x2 in 4-bit
3
4 #include <liquidcrystal.h> //includo la libreria dell'LCD
5
6 const int buttonPin = 6; //numero del pin del pulsante
7 const int ledPin = 13; //numero del pin del Led
8
9 int durata; //durata dell'impulso
10 int distanza; //distanza dell'oggetto
11 int pin_segnale = 8; //pin Arduino a cui è collegato il sensore SRF05
12 int val=0; //val verrà utilizzato per memorizzare lo stato del pin
13 int old_val=0;
14 int buttonState= 0; //variabile che legge lo stato del pulsante
15
16 LiquidCrystal lcd(12, 11, 5, 4, 3, 2); //inizializzo la libreria con i pin utilizzati
17
18 void setup() {
19 pinMode(ledPin, OUTPUT); //inizializzo il pin del Led come output
20 pinMode(buttonPin, INPUT); //inizializzo il pin del pulsante come input
21 lcd.begin(16, 2); //set up dei numeri e delle colonne dell'LCD
22 lcd.setCursor(0, 0); //sposto il cursore sulla prima riga e colonna
23 lcd.print(" La distanza è:"); //stampo il messaggio 'La distanza è:' sulla
//prima riga
24 }
25
26 void loop() {
27 val = digitalRead(buttonPin); //legge lo stato del pulsante
28 if ((val == HIGH) && (old_val == LOW)) { //controlla se il pulsante è HIGH
29 buttonState = 1 - buttonState;
30 delay(10);
31 }
32 old_val = val;
33 if (buttonState == 1) {
34 digitalWrite(ledPin, HIGH); //si accende il Led
35 pinMode(pin_segnale, OUTPUT);
36 digitalWrite(pin_segnale, LOW); //viene posto a LOW pin, per 2
37 //microsecondi prima di inviare un
//breve impulso di trigger
38 delayMicroseconds(2);
39 digitalWrite(pin_segnale, HIGH); //invia un impulso di trigger
40 delayMicroseconds(10); //di 10 microsecondi
41 digitalWrite(pin_segnale, LOW); //pone il pin al LOW in attesa che
42 //l'impulso torni indietro
43 pinMode(pin_segnale, INPUT);
44 durata = pulseIn(pin_segnale, HIGH); //legge l'eco dell'impulso in
//microsecondi
45 distanza = durata/58; //divide la durata per 58 per ottenere
46 //la distanza in cm
47 lcd.setCursor(0, 1); //sposto il cursore sulla prima riga e
48 //sulla prima colonna del display LCD
49 lcd.print(distanza); //visualizzo sul display LCD la distanza
50 lcd.print(" "); //visualizzo uno spazio
51 lcd.print("cm."); //visualizzo l'unità di misura "cm."
52 delay(100); //attende 100 millisecondi prima di
53 //incominciare una nuova misura
54 }
55 else {
56 digitalWrite(ledPin, LOW); //si spegne il Led
57 }
58 }</liquidcrystal.h>
PaulS:
What does that code ACTUALLY do? What do you expect it to do? What is the problem?
Hi PaulS,
it's my first code so I asked to check if is it correct. I need to know if sintax is right and if as is compiled the program could be able to do what it was written for i.e. measure distance and show it on a LCD display after had pushed a button.
Thanks.
Linuslizard:
and if as is compiled the program could be able to do what it was written for i.e. measure distance and show it on a LCD display after had pushed a button.
The normal way to answer that question is to try it and see what happens.
If it does not do what you want tell us what happens, or what error messages you get.
A big part of learning to program is the business of learning to figure out and fix the problem when (not IF) the program does not work. Don't expect to write code and have it work first time unless you are a genius.
Neither should you expect that an expert will be able to help you write code that works first time.
For syntax errors, click "verify" or whatever it is, next to the upload button. That will compile the code (or try to), and if it fails, you have syntax errors, and it will tell you where they are (or at least where it thinks they are - sometimes a single mistake can result in multiple errors later on)
Following that, upload it, and see if it does what you think it will, and if you have problems you can't figure out, then ask here.
It's very hard to read over code and say with certainty that it will work, because there are so many little things you could miss without a problem to point the way - that's why testing is critical.