Buongiorno a tutti,
Sono un neofita di Arduino, sto facendo un progetto basato sul concetto del sensore capacitivo, l'utilizzo del monitor seriale da quello che ho capito è fondamentale... So che deve dare risultati in numeri, ma a me sta dando valori molto strani e non capisco come mai.
Vi allego lo screenshot del problema.
Grazie nid69ita,
Non sono molto pratico dei forum
vi allego lo sketch
int freq = 1000;
int speakerOut = 13;
int LEDPin = 10;
int capSensePin = 3;
int touchedCutoff = 80;
int sensor;
void setup(){
Serial.begin (9600);
pinMode(capSensePin, INPUT);
pinMode(speakerOut, OUTPUT);
pinMode(LEDPin, OUTPUT);
digitalWrite(freq, LOW);
}
void loop(){
sensor = readCapacitivePin(capSensePin);
if (sensor > touchedCutoff) {
Serial.println("capSense: " + sensor);
tone(speakerOut, freq + sensor/2);
}
else {
noTone(speakerOut);
}
if (sensor > touchedCutoff) {
Serial.println("capSense: " + sensor);
tone(LEDPin, freq + sensor/2);
}
else {
noTone(LEDPin);
}
if ( (millis() % 100) == 0){
Serial.print("Capacitive Sensor on Pin 3 reads: ");
Serial.println (sensor);
}
}
// readCapacitivePin
// Input: Arduino pin number
// Output: A number, from 0 to 17 expressing
// how much capacitance is on the pin
// When you touch the pin, or whatever you have
// attached to it, the number will get higher
// In order for this to work now,
// The pin should have a 1+Megaohm resistor pulling
// it up to +5v.
uint8_t readCapacitivePin(int pinToMeasure){
// This is how you declare a variable which
// will hold the PORT, PIN, and DDR registers
// on an AVR
volatile uint8_t* port;
volatile uint8_t* ddr;
volatile uint8_t* pin;
// Here we translate the input pin number from
// Arduino pin number to the AVR PORT, PIN, DDR,
// and which bit of those registers we care about.
// Now see how long the pin to get pulled up. This manual unrolling of the loop
// decreases the number of hardware cycles between each read of the pin,
// thus increasing sensitivity.
byte bitmask;
if ((pinToMeasure >= 0) && (pinToMeasure <= 7)){
port = &PORTD;
ddr = &DDRD;
bitmask = 1 << pinToMeasure;
pin = &PIND;
}
if ((pinToMeasure > 7) && (pinToMeasure <= 13)){
port = &PORTB;
ddr = &DDRB;
bitmask = 1 << (pinToMeasure - 8);
pin = &PINB;
}
if ((pinToMeasure > 13) && (pinToMeasure <= 19)){
port = &PORTC;
ddr = &DDRC;
bitmask = 1 << (pinToMeasure - 13);
pin = &PINC;
}
// Discharge the pin first by setting it low and output
*port &= ~(bitmask);
*ddr |= bitmask;
delay(1);
// Make the pin an input WITHOUT the internal pull-up on
*ddr &= ~(bitmask);
// Now see how long the pin to get pulled up
int cycles = 16000;
for(int i = 0; i < cycles; i++){
if (*pin & bitmask){
cycles = i;
break;
}
}
// Discharge the pin again by setting it low and output
// It's important to leave the pins low if you want to
// be able to touch more than 1 sensor at a time - if
// the sensor is left pulled high, when you touch
// two sensors, your body will transfer the charge between
// sensors.
*port &= ~(bitmask);
*ddr |= bitmask;
return cycles;
}
Nota che il monitor seriale visualizza rappresentazioni alfanumeriche di codici ASCII, per cui se il monitor seriale riceve 65 visualizza A, se riceve un codice ASCII che non ha una rappresentazione alfanumerica corrispondente visualizzerà nulla o simboli geroglifici incomprensibili.
Per cui se tu anziché voler stampare A vuoi stampare 65 devi convertire 65 in una stringa contenente due caratteri, cioè il 6 e 5 corrispondenti ai codici ASCII 54 e 53 che se spediti da arduino board saranno visualizzati dal monitor seriale come 65.
MauroTec:
Nota che il monitor seriale visualizza rappresentazioni alfanumeriche di codici ASCII, per cui se il monitor seriale riceve 65 visualizza A, se riceve un codice ASCII che non ha una rappresentazione alfanumerica corrispondente visualizzerà nulla o simboli geroglifici incomprensibili.
Per cui se tu anziché voler stampare A vuoi stampare 65 devi convertire 65 in una stringa contenente due caratteri, cioè il 6 e 5 corrispondenti ai codici ASCII 54 e 53 che se spediti da arduino board saranno visualizzati dal monitor seriale come 65.