servo ohne lib steuern

hi

ich habe das problem das die servo lib mit der altsoftserial lib in konflikt geraten. ich möchte über meinen touchscreen 3 buttons mit verschieden positionen anwählen und benötige für mein gps tacho aber die altsoftserial.

oder gibt es eine alternative lib zur servo.h?

gruß

rene

Hallo,

alternative wäre selbst ein PWM Signal zu erzeugen. Wenn Deine loop schnell genug ist kannste das in der loop machen. Wenn das zu ungenau bzw. unregelmäßig wird mußte Dir einen freien Timer suchen und den selbst programmieren. Welchen Arduino hast du?

Auf die USB Programmierung verzichten und mit ISP den Uno programmieren.

ich habe einen mega, mein loop ist sehr langsam durch das gps. so ca 1 sek

kann ich der servo.h einfach einen anderen timer zuweisen?

gruß

rene

mein loop ist sehr langsam durch das gps.

Das kann man doch ändern....

plath:
ich habe einen mega, mein loop ist sehr langsam durch das gps. so ca 1 sek

kann ich der servo.h einfach einen anderen timer zuweisen?

gruß

rene

Wieso nimmst Du dann die Softwareemulation. Der Mega hat 3 freie, unbenutzte serielle Schnittstellen.
Problem gelöst!
Uwe

Da muss ich erstmal checken ob die nicht vom tft oder Touch benutzt werden !
Dann noch rausfinden wie es geht

#include <UTFT_Buttons.h>
#include <AltSoftSerial.h>    // Pin 46 +48 !!!!!!!!!!!!!!!!!
#include <UTouch.h>
#include <UTouchCD.h>
#include <memorysaver.h>
#include <tinyFAT.h>
#include <UTFT_tinyFAT.h>
#include <UTFT.h>

#include <TinyGPS.h>
TinyGPS gps;

AltSoftSerial ss;

UTFT myGLCD(CTE70,38,39,40,41); 
UTouch        myTouch(6,5,4,3,2);
UTFT_tinyFAT myFiles(&myGLCD);
UTFT_Buttons  myButtons(&myGLCD, &myTouch);

#define TOUCH_ORIENTATION  PORTRAIT

// temp

#include <DHT.h>

#include <DallasTemperature.h>
#include <OneWire.h>

#define ONE_WIRE_BUS 8
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);

#define DHTPIN 9
#define DHTTYPE DHT11  
DHT dht(DHTPIN, DHTTYPE);

long previousMillis = 0;
long interval = 10000 ;
//long interval2 = 240000 ;


DeviceAddress motorThermometer = { 0x28, 0x04, 0x5A, 0x08, 0x06, 0x00, 0x00, 0x27 };
DeviceAddress outsideThermometer = { 0x28, 0x12, 0xE0, 0x0A, 0x06, 0x00, 0x00, 0x3B };
DeviceAddress oelThermometer = { 0x28, 0xEA, 0x5B, 0x08, 0x06, 0x00, 0x00, 0xD0 };
DeviceAddress rechteThermometer = { 0x28, 0x81, 0x85, 0x09, 0x06, 0x00, 0x00, 0x98 };
DeviceAddress linkeThermometer = { 0x28, 0xD9, 0x9B, 0x08, 0x06, 0x00, 0x00, 0x46 };


float tempmotorThermometer;
float tempoutsideThermometer;
float tempoelThermometer;
float temprechteThermometer;
float templinkeThermometer;
float tempdht22;
float humdht22;

//temp ende

//---Schriften einbinden---//

extern uint8_t BigFont[];
extern uint8_t seg180[];
extern uint8_t seg96[];
extern uint8_t SevenSegmentFull[];

// relais pins
int Pin1 = 42;
int Pin2 = 43;
int Pin3 = 45;
int Pin4 = 47;
int Pin5 = 49;
int Pin6 = 10;
int Pin7 = 11;
//int Pin8 = 12;

//analoge eingänge
int oeldruckeingang = A3;
float oeldruck2;
float oeldruck1;
float oeldruck;

int tankeingang = A2;
int tank = 0;
int trimeingang= A1;
int trim = 0;
int drehzahleingang = A4;
int drehzahl = 0;

////////////////////spannungeingang = A0;
int voltin = A0;
float vout = 0.0;
float vin = 0.0;
float R1 = 100000.0; // resistance of R1 (100K) -see text!
float R2 = 33000.0; // resistance of R2 (10K) - see text!
int value = 0;
float volt;

bool invert = false;  

void setup()
{
    file.initFAT();
   myGLCD.InitLCD(PORTRAIT);
  myGLCD.clrScr();
  myFiles.loadBitmap(65, 100, 350, 583, "rk.raw");
      delay(2000);
      myGLCD.clrScr();
      
  myTouch.InitTouch(PORTRAIT);
  myTouch.setPrecision(PREC_HI);
  
Serial.begin(9600);
ss.begin(9600); 
 texte();
   pinMode(Pin1, OUTPUT);
   digitalWrite(Pin1, HIGH);
   pinMode(Pin2, OUTPUT);
   digitalWrite(Pin2, HIGH);
   pinMode(Pin3, OUTPUT);
   digitalWrite(Pin3, HIGH);
   pinMode(Pin4, OUTPUT);
   digitalWrite(Pin4, HIGH);
   pinMode(Pin5, OUTPUT);
   digitalWrite(Pin5, HIGH);
   pinMode(Pin6, OUTPUT);
   digitalWrite(Pin6, HIGH);
   pinMode(Pin7, OUTPUT);
   digitalWrite(Pin7, HIGH);
  // pinMode(Pin8, OUTPUT);
   //digitalWrite(Pin8, HIGH);

sensors.begin();
  // set the resolution to 10 bit 
  sensors.setResolution(motorThermometer, 10);
  sensors.setResolution(outsideThermometer, 10);
  sensors.setResolution(oelThermometer, 10);
  sensors.setResolution(rechteThermometer, 10);
  sensors.setResolution(linkeThermometer, 10);
  
dht.begin();
  temps();
}

void texte(){
//Tank Fest  
 
     myGLCD.setFont(BigFont);
     if (invert==true){ myGLCD.setColor(VGA_BLACK);}
      else {myGLCD.setColor(VGA_WHITE);}
     myGLCD.drawRect(19, 49, 61, 301);
     myGLCD.drawRect(10 , 48, 71, 49);
     if (invert==true){ myGLCD.setColor(VGA_BLACK);}
      else {myGLCD.setColor(VGA_WHITE);}
     if (invert==true){ myGLCD.setBackColor(VGA_WHITE);}
      else {myGLCD.setBackColor(VGA_BLACK);}
     myGLCD.print("F" , 75, 40);
     myGLCD.drawRect(10 , 175, 71, 176);
     myGLCD.drawRect(10 , 300, 71, 301);
     myGLCD.print("E" , 75, 290);
     if (invert==true){ myGLCD.setColor(VGA_WHITE);}
      else {myGLCD.setColor(VGA_BLACK);}
     myGLCD.fillRect(20, 47, 60, 52);
    myFiles.loadBitmap(15, 305, 50, 50, "zapf1.raw");      
   
   //Trimm
     
     if (invert==true){ myGLCD.setColor(VGA_BLACK);}
      else {myGLCD.setColor(VGA_WHITE);}
     myGLCD.drawRect(419, 49, 461, 301);
     myGLCD.drawRect(410 , 48, 471, 49);
     myGLCD.print("+" , 385, 40);
     myGLCD.drawRect(410 , 145, 471, 146);
     myGLCD.drawRect(410 , 175, 471, 176);
     myGLCD.drawRect(410 , 205, 471, 206);
     myGLCD.drawRect(410 , 235, 471, 236);
     myGLCD.drawRect(410 , 265, 471, 266);
     myGLCD.drawRect(410 , 300, 471, 301);
     myGLCD.print("-" , 385, 290);
     if (invert==true){ myGLCD.setColor(VGA_WHITE);}
      else {myGLCD.setColor(VGA_BLACK);}
     myGLCD.fillRect(420, 47, 460, 52);
   myFiles.loadBitmap(415, 305, 50, 50, "pro.raw");


     //Texte
  if (invert==true){ myGLCD.setColor(VGA_BLACK);}
      else {myGLCD.setColor(VGA_WHITE);}
     if (invert==true){ myGLCD.setBackColor(VGA_WHITE);}
      else {myGLCD.setBackColor(VGA_BLACK);} 
  myGLCD.setFont(BigFont);
  //myGLCD.print("4.12.2015", LEFT, 2);
  //myGLCD.print("13:55", RIGHT, 2);
  myGLCD.print("KM/H", CENTER, 10);
  myGLCD.print("U/MIN", CENTER, 235);
  myGLCD.print("Motor", LEFT, 390);
  myGLCD.print("OelTemp", CENTER, 390);
  myGLCD.print("OelDruck", RIGHT, 390);
  myGLCD.print("Wasser", LEFT, 480);
  myGLCD.print("Linke", CENTER, 480);
  myGLCD.print("Rechte", RIGHT, 480);
  myGLCD.print("C", 45, 2);
  myGLCD.print("-", 68, 2);
  myGLCD.print("%", 135, 2);
  myGLCD.print("Volt", 410, 2);
}

void temps(){
  
  float h = dht.readHumidity();
  // Read temperature as Celsius
  float t = dht.readTemperature();
  t=t-0.4;
  h=h+2;
  
sensors.requestTemperatures();
 
  tempmotorThermometer = sensors.getTempC(motorThermometer);
  tempoutsideThermometer = sensors.getTempC(outsideThermometer);
  tempoelThermometer = sensors.getTempC(oelThermometer);
  temprechteThermometer = sensors.getTempC(rechteThermometer);
  templinkeThermometer = sensors.getTempC(linkeThermometer);
  
 if (invert==true){ myGLCD.setColor(VGA_BLACK);}
else {myGLCD.setColor(VGA_WHITE);}
if (invert==true){ myGLCD.setBackColor(VGA_WHITE);}
else {myGLCD.setBackColor(VGA_BLACK);}
 
  myGLCD.setFont(SevenSegmentFull);
  myGLCD.printNumI(tempmotorThermometer, LEFT, 415);
  myGLCD.printNumI(tempoelThermometer, CENTER, 415);
  myGLCD.printNumI(templinkeThermometer, CENTER, 505);
  myGLCD.printNumI(temprechteThermometer, RIGHT, 505);
  myGLCD.printNumI(tempoutsideThermometer, LEFT, 505);
  myGLCD.setFont(BigFont);
  myGLCD.printNumI(t, 10, 2);
  myGLCD.printNumI(h, 90, 2);
  
}
}

void tanken(){

    tank = analogRead(tankeingang);
    tank = constrain(tank, 170, 665);
    tank = map(tank, 170, 665, 50, 300);
    
    
    if (invert==true){myGLCD.setColor(VGA_BLACK);}
       else {myGLCD.setColor(VGA_WHITE);}
    if (tank > 260) {myGLCD.setColor(VGA_RED);}
      
            myGLCD.fillRect(20, 300, 60, tank);
            
       if (invert==true){myGLCD.setColor(VGA_WHITE);}
       else {myGLCD.setColor(VGA_BLACK);}
            myGLCD.fillRect(20, tank-2, 60, 50);

}


void trimmen(){   
  
  trim = analogRead(trimeingang);
  trim = constrain(trim, 10, 536);
  trim = map(trim, 10, 536, 300, 50);
    
    if (invert==true){myGLCD.setColor(VGA_BLACK);}
       else {myGLCD.setColor(VGA_WHITE);}
     if (trim < 170) {myGLCD.setColor(VGA_RED);}
       myGLCD.fillRect(420, 300, 460, trim);
      
     if (invert==true){myGLCD.setColor(VGA_WHITE);}
       else {myGLCD.setColor(VGA_BLACK);}
        myGLCD.fillRect(420, trim-2, 460, 50);


//spannung
   value = analogRead(voltin);
   vout = (value * 5.0) / 1023.0;
   vin = vout / (R2/(R1+R2));
   volt = volt * 0.9 + vin * 0.1;
   
   myGLCD.setFont(BigFont);
   if (invert==true){ myGLCD.setColor(VGA_BLACK);}
else {myGLCD.setColor(VGA_WHITE);}
if (invert==true){ myGLCD.setBackColor(VGA_WHITE);}
else {myGLCD.setBackColor(VGA_BLACK);}
   myGLCD.printNumF(vin,1 , 340, 2);
  
  }

void drehzahlmessung(){
  
    drehzahl = analogRead(drehzahleingang);
    drehzahl = map(drehzahl, 420, 500, 1000, 3000);
    drehzahl = constrain(drehzahl, 450, 7000);
    
   int schwelle = (drehzahl<1000)?10:((drehzahl<2000)?50:100); 
   int gerundet = (drehzahl / schwelle) * schwelle;
  
     myGLCD.setFont(seg96);
  
  if (drehzahl < 500 ) gerundet = 0;
  if (gerundet == 0) myGLCD.print("000", 176, 260); 
  if (gerundet < 1000) {
    myGLCD.print("0", 112, 260);
    myGLCD.printNumI(gerundet, 176, 260);
  }
  else{
  myGLCD.printNumI(gerundet, CENTER, 260);
  }
  
  //Serial.println(drehzahl);
  //myGLCD.printNumI(drehzahl, CENTER, 260);
}
  

/*
void oeldruckmessung(){      // neuer sensor mit 2 anzeigen!

    
    oeldruck1 = analogRead(oeldruckeingang);
    oeldruck1 = map(oeldruck1, 650, 160, 18, 0);
    oeldruck1 = constrain(oeldruck1, 0, 50);
    oeldruck2 = oeldruck1/10;
   // Serial.println(oeldruck1);

    if (invert==true){myGLCD.setColor(VGA_BLACK);}
       else {myGLCD.setColor(VGA_WHITE);}
          
          myGLCD.setFont(SevenSegmentFull);
      if (oeldruck2 < 0.1) oeldruck2 = 0.0;
          myGLCD.printNumF(oeldruck2,1 , RIGHT, 415);
   }

*/


void oeldruckmessung(){      //alter sensor arduino alleine

    
    oeldruck1 = analogRead(oeldruckeingang);
    oeldruck1 = map(oeldruck1, 365, 620, 50, 0);
    oeldruck1 = constrain(oeldruck1, 0, 60);
    oeldruck2 = oeldruck1/10;
   // Serial.println(oeldruck1);
        
    if (invert==true){myGLCD.setColor(VGA_BLACK);}
       else {myGLCD.setColor(VGA_WHITE);}
          
          myGLCD.setFont(SevenSegmentFull);
      if (oeldruck2 < 0.1) oeldruck2 = 0.0;
          myGLCD.printNumF(oeldruck2,1 , RIGHT, 415);
   }


void loop()
{
  unsigned long currentMillis = millis ( ) ;
  
  temps();
  tanken();
  trimmen();
  oeldruckmessung();
  drehzahlmessung();
   int but1, but2, but3, but4, but5, but6, but7, but8, pressed_button;
  myButtons.setTextFont(BigFont);
  but1 = myButtons.addButton( 0, 590, 115,  100, "Licht");
  but2 = myButtons.addButton( 120, 590, 115,  100, "Blower");
  but3 = myButtons.addButton( 240, 590, 115,  100, "Bilge");
  but4 = myButtons.addButton( 360, 590, 115,  100, "Radio");
  but5 = myButtons.addButton( 0, 699, 115,  100, "Sub");
  but6 = myButtons.addButton( 120, 699, 115,  100, "Auspuff");
  but7 = myButtons.addButton( 240, 699, 115,  100, "LED");
  but8 = myButtons.addButton( 360, 699, 115,  100, "Hell");
  myButtons.drawButtons();
  
    
  while(1)
  {

    
    
 //   myGLCD.printNumF(spannung, 1, 300, 2);
   trimmen();
   oeldruckmessung();
   drehzahlmessung();
   
  unsigned long currentMillis = millis ( ) ;

      if ( currentMillis - previousMillis > interval ) {  previousMillis = currentMillis ; 
        temps();
        tanken();
    
}

  bool newData = false;
  unsigned long chars;
  unsigned short sentences, failed;

  // For one second we parse GPS data and report some key values
  for (unsigned long start = millis(); millis() - start < 500;)
 {
    if (ss.available()) // test mit if   original while
    {
      char c = ss.read();
      // Serial.write(c); // uncomment this line if you want to see the GPS data flowing
      if (gps.encode(c)) // Did a new valid sentence come in?
        newData = true;
    }
  }

 if (newData)
  {
    int fkmph = (gps.f_speed_kmph()); 
    //int falt = (gps.f_altitude());
    //int sat = (gps.satellites());
    
    //Serial.print(" sateliten=");
    //Serial.print(sat);
    //Serial.print(" kmh=");
    //Serial.print(fkmph);
    //Serial.print(" hoehe=");
    //Serial.println(falt);
    
    if (invert==true){ myGLCD.setColor(VGA_BLACK);}
  else {myGLCD.setColor(VGA_WHITE);}
    if (invert==true){ myGLCD.setBackColor(VGA_WHITE);}
  else {myGLCD.setBackColor(VGA_BLACK);}
    myGLCD.setFont(seg180);
    if (fkmph < 10) {
      myGLCD.print("0", 104, 40);
      myGLCD.printNumI(fkmph, 240, 40);
  }
  else{
  myGLCD.printNumI(fkmph, CENTER, 40);
   }
 }
  
if (myTouch.dataAvailable() == true)
    {
      pressed_button = myButtons.checkButtons();

      if (pressed_button==but1)
      {             
               if (digitalRead(Pin1) == HIGH){
                    digitalWrite(Pin1, LOW);
                      myButtons.setButtonColors(VGA_WHITE, VGA_GRAY, VGA_WHITE, VGA_RED, VGA_RED);
                      myButtons.drawButton(but1);}
               else {
                  digitalWrite(Pin1, HIGH);
                  myButtons.setButtonColors(VGA_WHITE, VGA_GRAY, VGA_WHITE, VGA_RED, VGA_BLUE);
                  myButtons.drawButton(but1);
           }}
   
      if (pressed_button==but2)
      
      {           
                  if (digitalRead(Pin2) == HIGH) {     
                  digitalWrite(Pin2, LOW);
                  myButtons.setButtonColors(VGA_WHITE, VGA_GRAY, VGA_WHITE, VGA_RED, VGA_RED);
                  myButtons.drawButton(but2);}
               
           else { 
                  digitalWrite(Pin2, HIGH);
                  myButtons.setButtonColors(VGA_WHITE, VGA_GRAY, VGA_WHITE, VGA_RED, VGA_BLUE);
                  myButtons.drawButton(but2);
             
           }}

    if (pressed_button==but3)
      {             
               if (digitalRead(Pin3) == HIGH){
                    digitalWrite(Pin3, LOW);
                      myButtons.setButtonColors(VGA_WHITE, VGA_GRAY, VGA_WHITE, VGA_RED, VGA_RED);
                      myButtons.drawButton(but3);}
               
           else {
                  digitalWrite(Pin3, HIGH);
                  myButtons.setButtonColors(VGA_WHITE, VGA_GRAY, VGA_WHITE, VGA_RED, VGA_BLUE);
                  myButtons.drawButton(but3);
           }}
   
      if (pressed_button==but4)
      
      {
              
                  if (digitalRead(Pin4) == HIGH) {     
                  digitalWrite(Pin4, LOW);
                  myButtons.setButtonColors(VGA_WHITE, VGA_GRAY, VGA_WHITE, VGA_RED, VGA_RED);
                  myButtons.drawButton(but4);}
               
           else { 
                  digitalWrite(Pin4, HIGH);
                  myButtons.setButtonColors(VGA_WHITE, VGA_GRAY, VGA_WHITE, VGA_RED, VGA_BLUE);
                  myButtons.drawButton(but4);

Wieso hast du auf dem Mega einen Konflikt mit den Timern? Auf dem UNO wäre das der Fall. Aber auf dem Mega läuft die Servo Lib standardmäßig auf Timer5. Nicht auf Timer1

Die anderen Hardware Schnittstellen sind einfach Serial1, Serial2, etc. statt Serial.

hmmm ich kann es dir nicht genau sagen. bin gerade unterwegs aber irgendwas mit vector 47 kam als fehler zwischen altsoftserial und servo

Mhh, gerade nochmal angesehen. Das sieht so aus als ob die Servo Lib die ISRs definiert auch wenn sie nicht verwendet werden. Das kann dann in der Tat ein Problem geben.

Gehe nach x:\Arduino\libraries\Servo\src\avr\ServoTimers.h

Dann im Atmega2560 Abschnitt, kommentiere mindestens das Define für Timer1 aus:
#define _useTimer1
-->
//#define _useTimer1

Aber wie gesagt, nimm lieber eine der Hardware Serial Schnittstellen. Das geht viel besser.

Ja das will ich gerne tun wenn du mir sagst wie ! ich finde leider nichts zum Thema servo an serielle Schnittstelle

Ich benötige ja nicht wirklich was aus der servo.h denke ich

Ich dachte an If Button 1 digital.write servo Position 1 oder sowas Es soll ja den Rest vom Sketch nicht behindern

Zur not muss ich einen 2. Mega mit einem Poti zum servo steuern nehmen Aber das ist ja die letzte Option :o

Gruß

Rene

plath:
Ja das will ich gerne tun wenn du mir sagst wie ! ich finde leider nichts zum Thema servo an serielle Schnittstelle

Es geht darum, dass du eine Hardware Schnittstelle für dein GPS Modul verwendest! Nicht für den Servo

Statt z.B. das:

softserial.begin(9600);
..
softserial.read();

So:

Serial2.begin(9600);
..
Serial2.read();

Du hast auf dem Mega 4 serielle Schnittstellen in Hardware! Serial: 0 + 1, Serial1: 18 + 19, Serial2: 16 + 17, Serial3: 14 + 15

Serial ist die USB Schnittstelle. Da kannst du sonst nichts anderes anschließen. Aber die 3 anderen Schnittstellen kannst du an andere Geräte hängen. Die sind unabhängig voneinander und brauchen keine Timer.

altsoftserial verwendet pin 46 und 47, wie muss ich das verstehen?

de servo spreche ich dann an mit z.b serial2.write =??

gruß

rene

Nicht für den Servo!!

Du hast doch das geschrieben:

benötige für mein gps tacho aber die altsoftserial.

Also da statt AltSoftSerial entweder Serial1, Serial2 oder Serial3 verwenden. Und dann die Servo Lib für den Servo.

Oder wenn es unbedingt AltSoftSerial sein muss, in der Servo Lib das Timer1 Define auskommentieren.

axooo

ok da haben wir aneinander vorbei geschrieben. das problem ist das ich da momentan nichts zum testen habe weil alles im boot eingebaut ist. ich habe eine klappensteuerung am auspuff die ich noch ansprechen muss nur kann ich nicht deswegen alles umstricken weil das boot 100 km weit weg ist.

eine datei servoTimers habe ich nicht, in der servo.h habe ich timer1 auskommentiert aber das bringt leider nichts.

also muss ich mir was anderes einfallen lassen.

gruß

rene

plath:
eine datei servoTimers habe ich nicht,

Da ist noch ein /avr Verzeichnis

Und da steht folgendes:

#if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__)
#define _useTimer5
#define _useTimer1   //<---- diese Zeile auskommentieren !
#define _useTimer3
#define _useTimer4
typedef enum { _timer5, _timer1, _timer3, _timer4, _Nbr_16timers } timer16_Sequence_t;

Das _timer1 sollte man vielleicht auch entfernen. Das spielt aber glaube erst eine Rolle wenn man so viele Servos am Laufen hat dass man mehrere Timer braucht. Aber man kann auch gleich auf Nummer sicher gehen.

ne kein avr verzeichniss

Nichts leichter als das!

Voraussetzung: Verwende digitale Servos! Diese "merken" sich die zuletzt vorgegebene Impulsbreite (und damit die Position) und halten diese Position auch, selbst wenn weitere Impulse ausbleiben oder nur unregelmäßig kommen.

Ansonsten ist das simpel: Irgend einen Pin (an dem der Dateneingang des Servos angeschlossen ist) auf HIGH, dann eine vorgegebene Zeit warten (delay micro seconds per Variable gesteuert), diesen PIN auf LOW, fertig.