Arduino for dummies

mastakilla
Messaggi: 72
Iscritto il: 29/12/2015, 0:48

Re: Arduino for dummies

Messaggioda mastakilla » 11/07/2016, 9:17

ciao, per mappature lineari puoi usare la comoda funzione map

https://www.arduino.cc/en/Reference/Map

nel tuo caso sarebbe

Codice: Seleziona tutto

potValue2 = map( analogRead(potPin), 0, 1024, 0, 99);

Avatar utente
Tony Evo
Messaggi: 6139
Iscritto il: 02/06/2011, 6:51
ECU: MS2 V3.0
Località: Vicino Roma

Re: Arduino for dummies

Messaggioda Tony Evo » 11/07/2016, 21:16

Siate un po piu elementari con me,ho provato sia

Codice: Seleziona tutto

/* hey, this is a simple code to make your arduino read
the value of a potentiometer and display it in percentage form on
a 16 X 2 LCD screen. I am pretty new at this so sorry if this code
is terrible or if i have no idea what i'm talking about in the
comments.

the circuit(pasted from examples):

* LCD RS pin to digital pin 12
* LCD Enable pin to digital pin 11
* LCD D4 pin to digital pin 5
* LCD D5 pin to digital pin 4
* LCD D6 pin to digital pin 3
* LCD D7 pin to digital pin 2
* LCD R/W pin to ground
* 10K resistor:
* ends to +5V and ground
* wiper to LCD VO pin (pin 3)
* Potentiometer attached to analog input 0

* center pin of the potentiometer to the analog pin
* one side pin (either one) to ground
* the other side pin to +5V

*/
#include <LiquidCrystal.h> // include the LCD library
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

int potPin = A0; //Potentiometer input pin
int potValue2 = 0; // final display variable
void setup() {
    lcd.begin(16, 2); // lcd rows and columns
    lcd.print("Potentiometer"); // title of sorts
}

void loop() {
// read then divide the input(max 1020 in this case) by 10
// divide by 1.02 to get percentage
    potValue2=analogRead(potPin)/10.24;
// set cursor to second row, first column
    lcd.setCursor(0, 1);
//display final percentage
    lcd.print(potValue2);
//print the percent symbol at the end
    lcd.print("%");
//wait 0.1 seconds
    delay(100);
//wipe the extra characters
    lcd.print(" ");
    delay(1);
}


che

Codice: Seleziona tutto

/* hey, this is a simple code to make your arduino read
the value of a potentiometer and display it in percentage form on
a 16 X 2 LCD screen. I am pretty new at this so sorry if this code
is terrible or if i have no idea what i'm talking about in the
comments.

the circuit(pasted from examples):

* LCD RS pin to digital pin 12
* LCD Enable pin to digital pin 11
* LCD D4 pin to digital pin 5
* LCD D5 pin to digital pin 4
* LCD D6 pin to digital pin 3
* LCD D7 pin to digital pin 2
* LCD R/W pin to ground
* 10K resistor:
* ends to +5V and ground
* wiper to LCD VO pin (pin 3)
* Potentiometer attached to analog input 0

* center pin of the potentiometer to the analog pin
* one side pin (either one) to ground
* the other side pin to +5V

*/
#include <LiquidCrystal.h> // include the LCD library
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

int potPin = A0; //Potentiometer input pin
int potValue2 = 0; // final display variable
void setup() {
    lcd.begin(16, 2); // lcd rows and columns
    lcd.print("Potentiometer"); // title of sorts
}

void loop() {
// read then divide the input(max 1020 in this case) by 10
// divide by 1.02 to get percentage
    potValue2 = map( analogRead(potPin), 0, 1024, 0, 99);
// set cursor to second row, first column
    lcd.setCursor(0, 1);
//display final percentage
    lcd.print(potValue2);
//print the percent symbol at the end
    lcd.print("%");
//wait 0.1 seconds
    delay(100);
//wipe the extra characters
    lcd.print(" ");
    delay(1);
}


probabilmente sbaglio a mettere i vostri comandi,perchè sono alle prime armi e spesso faccio le cose un po a pappagallo perchè non ho delle buone basi di programmazione

mastakilla
Messaggi: 72
Iscritto il: 29/12/2015, 0:48

Re: Arduino for dummies

Messaggioda mastakilla » 11/07/2016, 21:36

un consiglio, fai una cosa per volta. Prima assicurati di avere il valore che vuoi visualizzare usando il log seriale (Serial.println..), POI ti dedichi a stampare stringhe sul display LCD, oppure fai il contrario ma fai una cosa per volta e procedi solo quando sei certo che funziona. Regola n.1 del codice ;)

Avatar utente
Tony Evo
Messaggi: 6139
Iscritto il: 02/06/2011, 6:51
ECU: MS2 V3.0
Località: Vicino Roma

Re: Arduino for dummies

Messaggioda Tony Evo » 11/07/2016, 21:42

ma se vogliamo visualizzare il valore AFR e abbiamo detto che 0v = 10AFR e 5v = 20AFR

la riga di codice non dovrebbe essere

Codice: Seleziona tutto

potValue2 = map( analogRead(potPin), 0, 1024, 10, 20);


?

nell'lcd funziona tutto come dovrebbe,ma non riesco ad avere il valore voluto

mastakilla
Messaggi: 72
Iscritto il: 29/12/2015, 0:48

Re: Arduino for dummies

Messaggioda mastakilla » 11/07/2016, 22:07

se l'output è lineare tra 10 e 20, si.
Siccome la funzione map ritorna un intero, puoi usare

Codice: Seleziona tutto

float afr = map( analogRead(potPin), 0, 1024, 100, 200)/10;


Per avere un valore decimale.

Avatar utente
Tony Evo
Messaggi: 6139
Iscritto il: 02/06/2011, 6:51
ECU: MS2 V3.0
Località: Vicino Roma

Re: Arduino for dummies

Messaggioda Tony Evo » 11/07/2016, 22:31

Ho usato questo codice,la virgola è comparsa,ma non cammina,è solo il numero intero a camminare

Codice: Seleziona tutto

float potValue2 = map( analogRead(potPin), 0, 1024, 100, 200)/10;

mastakilla
Messaggi: 72
Iscritto il: 29/12/2015, 0:48

Re: Arduino for dummies

Messaggioda mastakilla » 11/07/2016, 23:16

prova questo codice:

Codice: Seleziona tutto

void setup() {
  Serial.begin(57600);
}

float afr = 0;
char tempString[100];
char floatToStringBuffer[15];

void loop() {
 
 
  for(int i=0; i<=1024; i++) {
    afr = map( i, 0, 1024, 100, 200) / (float)10;
    dtostrf(afr, 4, 2, floatToStringBuffer);
    snprintf( tempString, 100, "i: %d -> afr: %s", i, floatToStringBuffer);
    Serial.println(tempString);
  }
 
  delay(100);
}


ho dovuto aggiungere un cast a float della costante 10 perchè altrimenti fa una divisione intera e ti ritorna un intero (sono i piaceri del C)
Inoltre ho usato la funzione dtostrf per convertire float e double in stringhe, perchè non sono nativamente supportati da sprintf e compagnia bella sull'implementazione di Arduino (e questi sono i piaceri dei microcontroller con 2k di RAM) :D

Avatar utente
Tony Evo
Messaggi: 6139
Iscritto il: 02/06/2011, 6:51
ECU: MS2 V3.0
Località: Vicino Roma

Re: Arduino for dummies

Messaggioda Tony Evo » 12/07/2016, 8:08

Ho provato il tuo codice e ho abilitato il monitor seriale e sembra come impazzito visualizzando caratteri incomprensibili.
alcune funzioni me le immaginavo piu semplicie,a prescindere dalla scarsa conoscienza del linguaggio di programmazione

mastakilla
Messaggi: 72
Iscritto il: 29/12/2015, 0:48

Re: Arduino for dummies

Messaggioda mastakilla » 12/07/2016, 8:18

Devi impostare il monitor seriale sui 57600 baud o vedi dolo testo malformato

Avatar utente
Tony Evo
Messaggi: 6139
Iscritto il: 02/06/2011, 6:51
ECU: MS2 V3.0
Località: Vicino Roma

Re: Arduino for dummies

Messaggioda Tony Evo » 12/07/2016, 8:24

l'ho impostato e vedo i caratteri bene,ma è come se fosse impazzito,parte da 10 e arriva a 20 ma in continuazione.. non sembra pilotato dal potenziometro


Torna a “Elettronica generale”

Chi c’è in linea

Visitano il forum: Nessuno e 6 ospiti