Hallo,
ich habe eine dumme Frage und finde keine Antwort.
Ich möchte 4 Werte (2 würden reichen) von einem Arduino zu einem anderen Arduino über i2C übertragen, die Übertragung klappt auch schon ganz gut. Nur scheitere ich daran, die am Master empfangenen Werte wieder in Integer umzuwandeln.
Mein Slave misst und sendet in der Loop Schleife die als Integer definierten 1-3 stelligen Werte r,t,v,s (bitte Ignoriert die Seriellen befehle):
//gekürzt für maximale Beitragsgröße im Forum
#include <Servo.h>
#include <OneWire.h>
#include <Wire.h>
#include <DallasTemperature.h>
#define ONE_WIRE_BUS 2
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
Servo myservo;
Servo myservo2;
int potpin = 0; // analog pin used to connect the potentiometer
int val;
void setup() {
sensors.begin(); // put your setup code here, to run once:
myservo.attach(9);
myservo2.attach(10);
Wire.begin();
Serial.begin(9600);
attachInterrupt(digitalPinToInterrupt(3), Pulse_Event, RISING); // Enable interruption pin 2 when going from LOW to HIGH.
pinMode (3, INPUT_PULLUP);
delay(1000);
}
void loop() {
// put your main code here, to run repeatedly:
LastTimeCycleMeasure = LastTimeWeMeasured; // Store the LastTimeWeMeasured in a variable.
CurrentMicros = micros(); // Store the micros() in a variable.
// CurrentMicros should always be higher than LastTimeWeMeasured, but in rare occasions that's not true.
// I'm not sure why this happens, but my solution is to compare both and if CurrentMicros is lower than
// LastTimeCycleMeasure I set it as the CurrentMicros.
// The need of fixing this is that we later use this information to see if pulses stopped.
if(CurrentMicros < LastTimeCycleMeasure)
{
LastTimeCycleMeasure = CurrentMicros;
}
// Calculate the frequency:
FrequencyRaw = 10000000000 / PeriodAverage; // Calculate the frequency using the period between pulses.
// Detect if pulses stopped or frequency is too low, so we can show 0 Frequency:
if(PeriodBetweenPulses > ZeroTimeout - ZeroDebouncingExtra || CurrentMicros - LastTimeCycleMeasure > ZeroTimeout - ZeroDebouncingExtra)
{ // If the pulses are too far apart that we reached the timeout for zero:
FrequencyRaw = 0; // Set frequency as 0.
ZeroDebouncingExtra = 2000; // Change the threshold a little so it doesn't bounce.
}
else
{
ZeroDebouncingExtra = 0; // Reset the threshold to the normal value so it doesn't bounce.
}
FrequencyReal = FrequencyRaw / 10000; // Get frequency without decimals.
// This is not used to calculate RPM but we remove the decimals just in case
// you want to print it.
// Calculate the RPM:
RPM = FrequencyRaw / PulsesPerRevolution * 60; // Frequency divided by amount of pulses per revolution multiply by
// 60 seconds to get minutes.
RPM = RPM / 10000; // Remove the decimals.
// Smoothing RPM:
total = total - readings[readIndex]; // Advance to the next position in the array.
readings[readIndex] = RPM; // Takes the value that we are going to smooth.
total = total + readings[readIndex]; // Add the reading to the total.
readIndex = readIndex + 1; // Advance to the next position in the array.
if (readIndex >= numReadings) // If we're at the end of the array:
{
readIndex = 0; // Reset array index.
}
// Calculate the average:
average = total / numReadings; // The average value it's the smoothed result.
// Print information on the serial monitor:
// Comment this section if you have a display and you don't need to monitor the values on the serial monitor.
// This is because disabling this section would make the loop run faster.
int r = 164; // Lückenfüller
int s = map (RPM, 0, 1404, 0, 180);
sensors.requestTemperatures();
int t = map (sensors.getTempCByIndex(0), -55, 125, 0, 180);
int v = 136; // Lückenfüller
myservo.write (t); // Temperatur
myservo2.write (r); // Geschwindigkeit
delay(30);
Wire.beginTransmission(2);
Serial.print("rpm.val=");
Wire.print ("r,");
Serial.print(r);
Wire.println(r);
Serial.write(0xff);
Serial.write(0xff);
Serial.write(0xff);
Serial.print("temp.val=");
Wire.print("t,");
Serial.print(t);
Wire.println(t);
Serial.write(0xff);
Serial.write(0xff);
Serial.write(0xff);
Serial.print("volt.val=");
Wire.print("v,");
Serial.print(v);
Wire.println(v);
Serial.write(0xff);
Serial.write(0xff);
Serial.write(0xff);
Serial.print("speed.val=");
Wire.print("s,");
Serial.print(r);
Wire.println(160);
Serial.write(0xff);
Serial.write(0xff);
Serial.write(0xff);
Wire.endTransmission();
}
am Master
#include <Wire.h>
void setup() {
Wire.begin(2);
Wire.onReceive(empfangen);
Serial.begin(9600);
}
void loop() {
// put your main code here, to run repeatedly:
delay (100);
}
void empfangen(int anzahl)
{
while(1<Wire.available())
{
char buchstabe = Wire.read();
Serial.print(buchstabe);
}
char buchstabe = Wire.read();
Serial.println(buchstabe);
}
bekomme ich dann auch folgendes im Seriellen Monitor ausgespuckt:
r,164
t,-72
v,136
s,160
r,164
t,-72
v,136
s,160
r,164
t,-72
v,136
s,160
Nun meine Frage:
wie bekomme ich am Master die Werte r,t,v,s wieder zum Integer gewandelt, zur Weiterverarbeitung im sketch?
Vermutlich mache ich es mir schon beim Senden zu leicht.
Funktionsbeschreibung:
Der Slave erfasst eine Temperatur mit einem DS18B20 Sensor und eine sehr tiefe Frequenz mit Hilfe der micros Funktion um eine Geschwindigkeit zu messen.
Beide Werte werden über i2C zum Master übertragen
Der Master erfasst eine mittlere Frequenz zur Drehzahlbestimmung , eine Spannung (Bordspannung), empfängt die Temperatur und die Geschwindigkeit über I2C und bereitet alles in einem Seriellen Protokoll für ein, an der Seriellen Schnittstelle angeschlossenes, NEXTION Display auf.