IR Miniatur-Reflexlichttaster zusammen mit Arduino Mega 2560

Hallo Leute,

ich möchte folgendes Projekt realisieren. Ich habe einen Mega 2560 und 3 IR Miniatur Reflexlichttaster MRL 601 und eine Glücksradscheibe. Ich möchte den Arduino samt den Sensoren hinten an das Glücksrad anbringen und einige Werte von der Scheibe ablesen dur verschiedene Reflektoren. Das Glücksrad ist in 12 Zahlen aufgeteilt. Ich möchte mit den 3 Sensoren die Geschwindigkeit, Die Drehrichtung und die Position herrauslesen. Die Sensoren sind schon angeschlossen das hat ganz gut geklappt. Im moment habe ich aber nur 2 angeschlossen. Nun zu meinem Aktuellem Problem:

Ich möchte die Drehrichtung herrausfinden. Dazu möchte ich 2 der 3 Sensoren verwenden. Ich habe mir das so vorgestellt das auf der "Bahn" die der Sensor ja bei einer Umdrehung abtastet eine Stelle ist die das IR Licht reflektiert. die Reflektionsstellen sind etwas versetzt bei den beiden Bahnen. so das in der Theorie wenn der Sensor1 von der Bahn 1 zuerst ein Signal zurück gibt und danach Sensor2 ein Signal zurück gibt sich die Scheibe nach Links dreht und halt das ganze noch mal anderst herum. Ich habe mir gedacht ich programiere das eventuell mit Interrupts aber das will noch nicht so richtig funktionieren. Habt ihr vllt noch ein paar tips wie ich das umsetzen könnte wie ich die Interrupts richtig programmiere und die ich das ganze noch Debounce.

Vielen Dank

Mit freundlichem Gruß

Levent

Die reflektierenden Stellen sollten wie bei einem inkrementellen Rotations-Encoder angeordnet sein:

Bezüglich Programmierung: Bei einem Glücksrad sollten Interrupts nicht nötig sein, Pollen sollte mehr als genug schnell sein. Poste mal den Code, den Du im Moment hast, vielleicht finden wir ja den Fehler in Deinem Code.

also von dem Code hab ich noch nicht wirklich viel da ich bisher nur die Sensoren getestet habe. jetzt geht es dann hin zu dem richtigen programm. hat vllt schon mal jemand mit den sensoren gearbeitet und weis da etwas bescheid

Hallo,
"hat vllt schon mal jemand mit den sensoren gearbeitet und weis da etwas bescheid"

Eigentlich stellt man dafür ein Datenblatt zur Verfügung. Ich mache das mal:
http://www.produktinfo.conrad.com/datenblaetter/175000-199999/182230-da-01-de-Reflex_Lichtschranke_MRL_601.pdf

Dann brauchst Du noch Vorwiderstände für die Dioden und den Transistor zur Strombegrenzung.
Das Ding schaltet bei Reflexion durch, das heißt Du brauchst an Deinem Glücksrad so etwas wie eine StroboScheibe (hell/dunkel/hell us.w.)
Dann könntest Du damit schon einmal etwas probieren.
Gruß und Spaß dabei
Andreas

ok ich habe mich wahrscheinlich falsch ausgedrückt. Die Reflexlichttaster habe ich schon angeschlossen und erhalte auch ein Signal.
Ich habe 3 Sensoren. Der erste bekommen bei jeder Ziffer von der Reflexion etwas zurück. die anderen beiden bekommen nur an einer nummer was zurück zb der 2. Sensor an der 2 und der 3 Sensor an der 3. habe ein Bild angehängt. In der Theorie wenn also zuerst Sensor 2 ne EINS bekommt und dann Sensor 3 eine EINS bekommt dreht es sich im Uhrzeigersinn und wenn zuerst Sensor 3 und dann Sensor 2 eine bekommen dann gegen den Uhrzeigersinn. Dann möchte ich mit der dauer zwischen zwei EINSEN bei Sensor 2 herrausfinden wie schnell sie die Scheibe dreht. und dann noch die Position auf der sich das Rad beim Start befindet. Das möchte ich anhand mit dem 1. und 2. Sensor berechnen. Fehler - abload.de

Hallo,
so einfach wird das glaube ich nicht. Das Ding wird ja von Hand gedreht, d.h
verschiedene Geschwindigkeiten.
Sensor 1 für Geschwindigkeit
Wenn die Markierung an dem vorbei läuft, dann müßte ein "millis() Timer"
starten.
Im gleichen Moment müßten die Impuse gezählt werden.
Also es werden 5000 millis gezählt und in dieser Zeit werden 5 Impulse
ausgelöst.
Dann hast Du alle Sekunde einen Impuls.
Dafür brauchst aber einen Interrupt zum starten und stoppen- das kann ich nicht.

Drehrichtung:
Sensor 2 ist der erste
Sensor 3 ist der zweite

das müßte auch mit millis() gehen.

Sensor 2 erhält den Impuls und fängt an millis() zu zählen.
Jetzt könnte man:
Wenn millis() < 50 und Sensor 3 Pin = LOW dann Drehung links, sonst rechts.

Wenn also in einer bestimmten Zeit kein weitere Impuls kommt, dann dreht es
sich in eine bestimmte Richtung.
Die Zeit, Sensor 2 -> Sensor 3 ist immer kürzer als
die Zeit, Sensor 2 -> Sensor 1 -> Sensor 3.
So aus dem "steh" kann ich Dir das nicht programmieren, aber vielleicht hilft
es Dir auf die Sprünge.
Gruß und Spaß
Andreas

Wenn es nur um 12 Positionen geht, wieso fügst Du nicht einen Sensor hinzu und kodierst immer gleich die ganze Zahl binär? Dann weisst Du immer, wo Du bist und kannst trotzdem die Geschwindigkeit bestimmen.
Der Ansatz von SkobyMobil ist ziemlich genau das, was Du brauchst, allerdings glaube ich nicht, dass Interrupts nötig sind, das Rad wird kaum über 100 U/min rauskommen und das sind Geschwindigkeiten, die locker mittels Poll erfasst werden können.

Hallo,
"kodierst immer gleich die ganze Zahl binär?"
Das ist doch der Tip des Tages…

Du setzt 4 Sensoren senkrecht übereinander und für jede Zahl ein binäres Feld.
Da gibt es nie Probleme mit irgendwas.
Gruß und Spaß
Andreas
P.S. Bild: diese schwarz/weiß Felder auf jede Zahl- hier bis 10

Bildschirmfoto 2014-09-10 um 11.07.png

ja auf die idee mit den 4 Sensoren bin ich auch gekommen aber mein "Chef" also der für mich zuständig ist (Praxissemester) meinte wir sollten so wenig wie möglich hardware verwenden und da wir ja schon 3 haben und er meint das es auf jedenfall auch mit 3 geht soll ich es mit 3 Sensoren machen. Ich habe den Code von dieser Seite http://www.kriwanek.de/arduino/grundlagen/183-mehrere-pin-change-interrupts-verwenden.html benutzt und naja nur etwas angepasst das ich halt erstmal das ganze verständnis mit den Interrupts bekomme. Habe mal meinen aktuellen Code angefügt. Nicht schlagen falls es irgendwie schlimm aussieht^^

// Define values for the interrupt counter:
volatile int counter1    = 0;        // Counter incremented by pin change interrupt
volatile int counter2    = 0;        // Counter incremented by pin change interrupt
volatile int counter3    = 0;        // Counter incremented by pin change interrupt

volatile int bounceTime  = 20;       // Switch bouncing time in milliseconds

volatile unsigned long IRQ1PrevTime; // Last time in milliseconds IRQ1 arrived
volatile unsigned long IRQ2PrevTime; // Last time in milliseconds IRQ2 arrived
volatile unsigned long IRQ3PrevTime; // Last time in milliseconds IRQ3 arrived
volatile unsigned long TimeForOneRev; // Time for one Revolution
volatile unsigned long TimeForOneRev2; // Time for one Revolution
volatile unsigned long TimeForClockwise;
volatile unsigned long TimeForAntiClockwise;
int clockFlag = 0;
int antiClockFlag =0;

volatile int IRQ1PrevVal   = 0;      // Contact level last IRQ1
volatile int IRQ2PrevVal   = 0;      // Contact level last IRQ2
volatile int IRQ3PrevVal   = 0;      // Contact level last IRQ3

volatile int irqFlag = 0;           // 1=display counters after IRQ; 0=do nothing


// Setup and Main:
void setup(){
  Serial.begin(9600);                // Initialize serial interface with 9600 Baud
  Serial.write("Please start the ride...\n");

  // INT1 --> Make Arduino Pin 50 (PCINT3/Port B.3) an input and set pull up resistor:
  pinMode(50, INPUT);
//digitalWrite(11, HIGH);
  // INT2 --> Make Arduino Pin 11 (PCINT5/Port B.5) an input and set pull up resistor:
  pinMode(11, INPUT);
//digitalWrite(12, HIGH);
  // INT3 --> Make Arduino Pin 12 (PCINT6/Port B.6) an input and set pull up resistor:
  pinMode(12, INPUT);
//  digitalWrite(15, HIGH);

  // This is ATMEGA368 specific, see page 75 of long datasheet
  // PCICR: Pin Change Interrupt Control Register - enables interrupt vectors
  // Bit 0 = enable PC vector 0 (PCINT7..0)
  PCICR |= (1 << PCIE0);             // Set port bit in CICR for INT1 and INT2 and INT3

  // Pin change mask registers decide which pins are enabled as triggers:
  PCMSK0 |= (1<<PCINT3);             // Set pin interrupt for INT1
  PCMSK0 |= (1<<PCINT5);             // Set pin interrupt for INT2
  PCMSK0 |= (1<<PCINT6);             // Set pin interrupt for INT3

  IRQ1PrevTime=millis();             // Hold actual time
  IRQ2PrevTime=millis();             // Hold actual time
  IRQ3PrevTime=millis();             // Hold actual time
  interrupts();                      // Enable interrupts
}

void loop()
{
  // Place your main loop commands here (e.g. output to LCD)
  if (irqFlag==1)                   // Flag was set by IRQ routine
  {
    if (clockFlag==1) {
    
    Serial.write("IRQ rising edge, Counter1 = ");
    Serial.print(counter1);
    Serial.write(", Counter2 = ");
    Serial.print(counter2);
    Serial.write(", Counter3 = ");
    Serial.print(counter3);
    Serial.write(" , Clockwise");
    Serial.write(", Time for one Revolution = ");
    Serial.print(TimeForOneRev2);
    Serial.println("seconds");
    clockFlag=0;
    }
    else {
    
    Serial.write("IRQ rising edge, Counter1 = ");
    Serial.print(counter1);
    Serial.write(", Counter2 = ");
    Serial.print(counter2);
    Serial.write(", Counter3 = ");
    Serial.print(counter3);
    Serial.write(", Time for one Revolution = ");
    Serial.print(TimeForOneRev2);
    Serial.println(" seconds");

    irqFlag=0;                      // Reset IRQ flag
    }
    
    if (antiClockFlag==1) {
    
    Serial.write("IRQ rising edge, Counter1 = ");
    Serial.print(counter1);
    Serial.write(", Counter2 = ");
    Serial.print(counter2);
    Serial.write(", Counter3 = ");
    Serial.print(counter3);
    Serial.write(" , Antilockwise");
    Serial.write(", Time for one Revolution = ");
    Serial.print(TimeForOneRev2);
    Serial.println("seconds");
    antiClockFlag=0;
    }
    else {
    
    Serial.write("IRQ rising edge, Counter1 = ");
    Serial.print(counter1);
    Serial.write(", Counter2 = ");
    Serial.print(counter2);
    Serial.write(", Counter3 = ");
    Serial.print(counter3);
    Serial.write(", Time for one Revolution = ");
    Serial.print(TimeForOneRev2);
    Serial.println(" seconds");

    irqFlag=0;                      // Reset IRQ flag
    }
    
  }
}

//-----------------------------------------------------------------------------
// Subs and Functions:

ISR(PCINT0_vect)
{
  // You have to write your own interrupt handler. Don't change the name!
  // This code will be called anytime when PCINT356 switches high to low, 
  // or low to high. This is for INT1 and INT2 and INT3 (all Port B)
  byte PVal;                                   // Port value (8 Bits)
  byte IRQ1ActVal;                             // Actual IRQ1 value
  byte IRQ2ActVal;                             // Actual IRQ2 value
  byte IRQ3ActVal;                             // Actual IRQ3 value

  long unsigned IRQ1ActTime;
  long unsigned IRQ2ActTime;
  long unsigned IRQ3ActTime;
  
  PVal = PINB;                                 // Read port B (8 bit)
  IRQ1ActVal = PVal & (1<<PCINT3);             // Mask out all except IRQ1
  IRQ1ActVal = IRQ1ActVal >> PCINT3;           // shift to right for bit0 position
  IRQ2ActVal = PVal & (1<<PCINT5);             // Mask out all except IRQ2
  IRQ2ActVal = IRQ2ActVal >> PCINT5;           // shift to right for bit0 position
  IRQ3ActVal = PVal & (1<<PCINT6);             // Mask out all except IRQ3
  IRQ3ActVal = IRQ3ActVal >> PCINT6;           // shift to right for bit0 position

  IRQ1ActTime=millis();                        // Read actual millis time
  if(IRQ1ActTime - IRQ1PrevTime > bounceTime)  // No bouncing anymore:
  {  
    // No contact bouncing anymore:
    if(IRQ1PrevVal==0 && IRQ1ActVal==1)        // Transition 0-->1
    {
      // Place your command for rising signal here...
       TimeForOneRev=millis();
      counter1++;
      if(counter1>12) {
        counter1 = 1;
        TimeForOneRev2=TimeForOneRev/1000;
      }
      IRQ1PrevTime=IRQ1ActTime;  
      IRQ1PrevVal=IRQ1ActVal;   
      irqFlag=1;
      
    }
    if(IRQ1PrevVal==1 && IRQ1ActVal==0)        // Transition 1-->0
    {
      // Place your command for falling signal here... 

      IRQ1PrevVal=IRQ1ActVal;
    }
  }

  IRQ2ActTime=millis();                        // Read actual millis time
  if(IRQ2ActTime - IRQ2PrevTime > bounceTime)  // No bouncing anymore:
  {  
    // No contact bouncing anymore:
    if(IRQ2PrevVal==0 && IRQ2ActVal==1)        // Transition 0-->1
    {
      // Place your command for rising signal here...
     //old Code
      TimeForClockwise=millis();
      if(TimeForAntiClockwise < 50) antiClockFlag=1;

      counter2++;
      if(counter2>255) counter2 = 0;
      IRQ2PrevTime=IRQ2ActTime;  
      IRQ2PrevVal=IRQ2ActVal;  
      irqFlag=1;
      
  }
    if(IRQ2PrevVal==1 && IRQ2ActVal==0)        // Transition 1-->0
    {
      // Place your command for falling signal here... 
      IRQ2PrevVal=IRQ2ActVal;
    }
  }

    IRQ3ActTime=millis();                        // Read actual millis time
  if(IRQ3ActTime - IRQ3PrevTime > bounceTime)  // No bouncing anymore:
  {  
    // No contact bouncing anymore:
    if(IRQ3PrevVal==0 && IRQ3ActVal==1)        // Transition 0-->1
    {
      // Place your command for rising signal here...
      TimeForAntiClockwise=millis();
     
      counter3++;
      if(TimeForClockwise < 50){
        clockFlag = 1;
      }
      if(counter3>255) counter3 = 0;
      IRQ3PrevTime=IRQ3ActTime;  
      IRQ3PrevVal=IRQ3ActVal;   
      irqFlag=1;
    }
    if(IRQ3PrevVal==1 && IRQ3ActVal==0)        // Transition 1-->0
    {
      // Place your command for falling signal here... 
      
      IRQ3PrevVal=IRQ3ActVal;
    }
  }
}

Also wenn das ein sauberes Projekt werden soll, dann nimm zwei der drei Sensoren und ordne die wie im Wikipedia-Artikel über Rotations-Encoder beschrieben an. Damit erkennst Du dann zuverlässig wie weit und in welche Richtung sich das Rad bewegt. Den dritten kannst Du noch als Null-Durchgang verwenden, also die Home-Position anzeigen lassen. Damit weisst Du dann aber schon wenn das Rad zu drehen beginnt, in welche Richtung es sich dreht und kannst die Aktivierung des dritten Sensors abwarten, damit Du weisst, wo Du bist. Mit etwas geschickter Programmierung kannst Du somit sagen, wo gestartet wurde, wo es aufgehört hat zu drehen und ob es manuell beeinflusst wurde.

Also ich habe jetzt die Zeit für eine umdrehung bzw für jede umdrehung die anzahl der umdrehung und die startposition jetzt fehlt im grunde nur noch die richtung und da hackt es etwas

// Define values for the interrupt counter:
volatile int counter1    = 0;        // Counter incremented by pin change interrupt
volatile int counter2    = 0;        // Counter incremented by pin change interrupt
volatile int counter3    = 0;        // Counter incremented by pin change interrupt

volatile int bounceTime  = 20;       // Switch bouncing time in milliseconds

volatile unsigned long IRQ1PrevTime; // Last time in milliseconds IRQ1 arrived
volatile unsigned long IRQ2PrevTime; // Last time in milliseconds IRQ2 arrived
volatile unsigned long IRQ3PrevTime; // Last time in milliseconds IRQ3 arrived
volatile unsigned long timeForOneRev; // Time for one Revolution
volatile unsigned long timeForOneRev2; // Time for one Revolution
volatile unsigned long timeForClockwise;
volatile unsigned long timeForAntiClockwise;
volatile int clockFlag = 0;
volatile int antiClockFlag = 0;
volatile int revolutionsCounter = 0;
volatile int homeState = 7;
volatile int homeStateHelp = 12;
volatile int startState = 0;

volatile int IRQ1PrevVal   = 0;      // Contact level last IRQ1
volatile int IRQ2PrevVal   = 0;      // Contact level last IRQ2
volatile int IRQ3PrevVal   = 0;      // Contact level last IRQ3

volatile int irqFlag = 0;           // 1=display counters after IRQ; 0=do nothing


// Setup and Main:
void setup(){
  Serial.begin(9600);                // Initialize serial interface with 9600 Baud
  Serial.write("Please start the ride...\n");

  // INT1 --> Make Arduino Pin 10 (PCINT3/Port B.4) an input and set pull up resistor:
  pinMode(10, INPUT);
//digitalWrite(11, HIGH);
  // INT2 --> Make Arduino Pin 11 (PCINT5/Port B.5) an input and set pull up resistor:
  pinMode(11, INPUT);
//digitalWrite(12, HIGH);
  // INT3 --> Make Arduino Pin 12 (PCINT6/Port B.6) an input and set pull up resistor:
  pinMode(12, INPUT);
//  digitalWrite(15, HIGH);

  // This is ATMEGA368 specific, see page 75 of long datasheet
  // PCICR: Pin Change Interrupt Control Register - enables interrupt vectors
  // Bit 0 = enable PC vector 0 (PCINT7..0)
  PCICR |= (1 << PCIE0);             // Set port bit in CICR for INT1 and INT2 and INT3

  // Pin change mask registers decide which pins are enabled as triggers:
  PCMSK0 |= (1<<PCINT4);             // Set pin interrupt for INT1
  PCMSK0 |= (1<<PCINT5);             // Set pin interrupt for INT2
  PCMSK0 |= (1<<PCINT6);             // Set pin interrupt for INT3

  IRQ1PrevTime=millis();             // Hold actual time
  IRQ2PrevTime=millis();             // Hold actual time
  IRQ3PrevTime=millis();             // Hold actual time
  interrupts();                      // Enable interrupts
}

void loop()
{
  // Place your main loop commands here (e.g. output to LCD)
  if (irqFlag==1)                   // Flag was set by IRQ routine
  {
    if (startState==0){
      
    Serial.write("Counter1 = ");
    Serial.print(counter1);
    Serial.write(", Counter2 = ");
    Serial.print(counter2);
    Serial.write(", Counter3 = ");
    Serial.print(counter3);
    Serial.write(", Time for one Revolution = ");
    Serial.print(timeForOneRev2);
    Serial.print(" seconds");
    Serial.write(", Round no. ");
    Serial.println(revolutionsCounter); 
        irqFlag=0;   
    }
    else if (clockFlag==1) {
    
    Serial.write("Counter1 = ");
    Serial.print(counter1);
    Serial.write(", Counter2 = ");
    Serial.print(counter2);
    Serial.write(", Counter3 = ");
    Serial.print(counter3);
    Serial.write(" , Clockwise");
    Serial.write(", Time for one Revolution = ");
    Serial.print(timeForOneRev2);
    Serial.println("seconds");
    clockFlag=0;
    }
    else if (antiClockFlag==1) {
    
    Serial.write("Counter1 = ");
    Serial.print(counter1);
    Serial.write(", Counter2 = ");
    Serial.print(counter2);
    Serial.write(", Counter3 = ");
    Serial.print(counter3);
    Serial.write(" , Antilockwise");
    Serial.write(", Time for one Revolution = ");
    Serial.print(timeForOneRev2);
    Serial.println("seconds");
    antiClockFlag=0;
    }
    else {
    
    Serial.write("Counter1 = ");
    Serial.print(counter1);
    Serial.write(", Counter2 = ");
    Serial.print(counter2);
    Serial.write(", Counter3 = ");
    Serial.print(counter3);
    Serial.write(", Time for one Revolution = ");
    Serial.print(timeForOneRev2);
    Serial.print(" seconds");
    Serial.write(", Round no. ");
    Serial.print(revolutionsCounter); 
    Serial.write(", Start Position at: ");
    Serial.println(startState);

    irqFlag=0;                      // Reset IRQ flag
    }
    }

}

//-----------------------------------------------------------------------------
// Subs and Functions:

ISR(PCINT0_vect)
{
  // You have to write your own interrupt handler. Don't change the name!
  // This code will be called anytime when PCINT356 switches high to low, 
  // or low to high. This is for INT1 and INT2 and INT3 (all Port B)
  byte PVal;                                   // Port value (8 Bits)
  byte IRQ1ActVal;                             // Actual IRQ1 value
  byte IRQ2ActVal;                             // Actual IRQ2 value
  byte IRQ3ActVal;                             // Actual IRQ3 value

  long unsigned IRQ1ActTime;
  long unsigned IRQ2ActTime;
  long unsigned IRQ3ActTime;
  
  PVal = PINB;                                 // Read port B (8 bit)
  IRQ1ActVal = PVal & (1<<PCINT4);             // Mask out all except IRQ1
  IRQ1ActVal = IRQ1ActVal >> PCINT4;           // shift to right for bit0 position
  IRQ2ActVal = PVal & (1<<PCINT5);             // Mask out all except IRQ2
  IRQ2ActVal = IRQ2ActVal >> PCINT5;           // shift to right for bit0 position
  IRQ3ActVal = PVal & (1<<PCINT6);             // Mask out all except IRQ3
  IRQ3ActVal = IRQ3ActVal >> PCINT6;           // shift to right for bit0 position

  IRQ1ActTime=millis();                        // Read actual millis time
  if(IRQ1ActTime - IRQ1PrevTime > bounceTime)  // No bouncing anymore:
  {  
    // No contact bouncing anymore:
    if(IRQ1PrevVal==0 && IRQ1ActVal==1)        // Transition 0-->1
    {
      // Place your command for rising signal here...
      counter1++;
      timeForOneRev=millis();
      if(counter1>12) {
        revolutionsCounter++;
        counter1 = 1;
        timeForOneRev2=timeForOneRev/1000;
      }
      IRQ1PrevTime=IRQ1ActTime;  
      IRQ1PrevVal=IRQ1ActVal;   
      irqFlag=1;
      
    }
    if(IRQ1PrevVal==1 && IRQ1ActVal==0)        // Transition 1-->0
    {
      // Place your command for falling signal here... 

      IRQ1PrevVal=IRQ1ActVal;
    }
  }

  IRQ2ActTime=millis();                        // Read actual millis time
  if(IRQ2ActTime - IRQ2PrevTime > bounceTime)  // No bouncing anymore:
  {  
    // No contact bouncing anymore:
    if(IRQ2PrevVal==0 && IRQ2ActVal==1)        // Transition 0-->1
    {
      // Place your command for rising signal here...
     //old Code

      if(timeForAntiClockwise < 50) antiClockFlag=1;

      counter2++;
      
      if (counter2==1) {
        
        startState=homeState-counter1;
        
        if (startState < 0) {
          startState=homeStateHelp-startState;
        }
      }
        
      if(counter2>255) counter2 = 0;
      IRQ2PrevTime=IRQ2ActTime;  
      IRQ2PrevVal=IRQ2ActVal;  
      irqFlag=1;
      
  }
    if(IRQ2PrevVal==1 && IRQ2ActVal==0)        // Transition 1-->0
    {
      // Place your command for falling signal here... 
            timeForClockwise=millis();
      IRQ2PrevVal=IRQ2ActVal;
    }
  }

    IRQ3ActTime=millis();                        // Read actual millis time
  if(IRQ3ActTime - IRQ3PrevTime > bounceTime)  // No bouncing anymore:
  {  
    // No contact bouncing anymore:
    if(IRQ3PrevVal==0 && IRQ3ActVal==1)        // Transition 0-->1
    {
      // Place your command for rising signal here...

      counter3++;
      if(timeForClockwise < 50){
        clockFlag = 1;
      }
      if(counter3>255) counter3 = 0;
      IRQ3PrevTime=IRQ3ActTime;  
      IRQ3PrevVal=IRQ3ActVal;   
      irqFlag=1;
    }
    if(IRQ3PrevVal==1 && IRQ3ActVal==0)        // Transition 1-->0
    {
      // Place your command for falling signal here... 
            timeForAntiClockwise=millis();
      IRQ3PrevVal=IRQ3ActVal;
    }
  }
}

achja und ich würde gerne wenn das Rad zum stehen kommt also sagen wir wenn enige Sekunden lang kein Flag auf high gesetzt wurde soll der Code am besten von neuem starten da noch ne Twitter verbindung mit eingebaut werden soll und diese soll jedesmal einen tweet versenden kurz nachdem das rad startet. aber halt immer wieder mit neuen werten und unabhängig von den alten werten. der code für die twitter anbindung hat mein chef schon fertig

OK also mein Chef wollte das ich den Code abändere. Er wollte das ich die Bit-shift anweisungen im Interrupt rausnehme da für jedes ein CPU Cycle nötig ist und dafür eine Switch Case Anweisung reinmache. das sieht dann so aus

ISR(PCINT0_vect)
{
  // You have to write your own interrupt handler. Don't change the name!
  // This code will be called anytime when PCINT356 switches high to low, 
  // or low to high. This is for INT1 and INT2 and INT3 (all Port B)
  byte PVal;                                   // Port value (8 Bits)
  byte IRQ1ActVal;                             // Actual IRQ1 value
  byte IRQ2ActVal;                             // Actual IRQ2 value
  byte IRQ3ActVal;                             // Actual IRQ3 value

  long unsigned IRQ1ActTime;
  long unsigned IRQ2ActTime;
  long unsigned IRQ3ActTime;

  
  switch(PINB&0x70)
  {
    case(ROT):
    IRQ1ActVal=1;
     IRQ1ActTime=millis();                        // Read actual millis time
  if(IRQ1ActTime - IRQ1PrevTime > bounceTime)  // No bouncing anymore:
  {  
    // No contact bouncing anymore:
    if(IRQ1PrevVal==0 && IRQ1ActVal==1)        // Transition 0-->1
    {
      // Place your command for rising signal here...
      counter1++;
      timeForOneRev=millis();
      if(counter1>12) {
        revolutionsCounter++;
        counter1 = 1;
        timeForOneRev2=timeForOneRev/1000;
      }
      IRQ1PrevTime=IRQ1ActTime;  
      IRQ1PrevVal=IRQ1ActVal;   
      irqFlag=1;
      IRQ1ActVal=0;
      
    }
    if(IRQ1PrevVal==1 && IRQ1ActVal==0)        // Transition 1-->0
    {
      // Place your command for falling signal here... 

      IRQ1PrevVal=IRQ1ActVal;
    }
  }
    break;
    case(HOME):
    IRQ2ActVal=1;
    IRQ2ActTime=millis();                        // Read actual millis time
  if(IRQ2ActTime - IRQ2PrevTime > bounceTime)  // No bouncing anymore:
  {  
    // No contact bouncing anymore:
    if(IRQ2PrevVal==0 && IRQ2ActVal==1)        // Transition 0-->1
    {
      // Place your command for rising signal here...
     //old Code

      if(timeForAntiClockwise < 50) antiClockFlag=1;

      counter2++;
      
      if (counter2==1) {
        
        startState=homeState-counter1;
        
        if (startState < 0) {
          startState=homeStateHelp-startState;
        }
      }
        
      if(counter2>255) counter2 = 0;
      IRQ2PrevTime=IRQ2ActTime;  
      IRQ2PrevVal=IRQ2ActVal;  
      irqFlag=1;
      IRQ2ActVal=0;
      
  }
    if(IRQ2PrevVal==1 && IRQ2ActVal==0)        // Transition 1-->0
    {
      // Place your command for falling signal here... 
            timeForClockwise=millis();
      IRQ2PrevVal=IRQ2ActVal;
    }
  }

    break;
    case(DIRECTION):
    IRQ3ActVal=1;
        IRQ3ActTime=millis();                        // Read actual millis time
  if(IRQ3ActTime - IRQ3PrevTime > bounceTime)  // No bouncing anymore:
  {  
    // No contact bouncing anymore:
    if(IRQ3PrevVal==0 && IRQ3ActVal==1)        // Transition 0-->1
    {
      // Place your command for rising signal here...

      counter3++;
      if(timeForClockwise < 50){
        clockFlag = 1;
      }
      if(counter3>255) counter3 = 0;
      IRQ3PrevTime=IRQ3ActTime;  
      IRQ3PrevVal=IRQ3ActVal;   
      irqFlag=1;
      IRQ3ActVal=0;
    }
    if(IRQ3PrevVal==1 && IRQ3ActVal==0)        // Transition 1-->0
    {
      // Place your command for falling signal here... 
            timeForAntiClockwise=millis();
      IRQ3PrevVal=IRQ3ActVal;
    }
  }
    break;
  } 
}

Ganz oben habe ich dann noch

#define ROT 0x10
#define HOME 0x20
#define DIRECTION 0x40

definiert. Jetzt zu meinem Problem er bemekt bei schnellen impulsen nicht jedes HIGH. Also die ausgabe sieht dann manchmal so aus

Counter1 = 12, Counter2 = 1, Counter3 = 0, Time for one Revolution = 67 seconds, Round no. 6, Start Position at: 7
Counter1 = 3, Counter2 = 1, Counter3 = 0, Time for one Revolution = 67 seconds, Round no. 6, Start Position at: 7
Counter1 = 4, Counter2 = 1, Counter3 = 0, Time for one Revolution = 67 seconds, Round no. 6, Start Position at: 7
Counter1 = 5, Counter2 = 1, Counter3 = 0, Time for one Revolution = 67 seconds, Round no. 6, Start Position at: 7
Counter1 = 6, Counter2 = 1, Counter3 = 0, Time for one Revolution = 67 seconds, Round no. 6, Start Position at: 7
Counter1 = 7, Counter2 = 1, Counter3 = 0, Time for one Revolution = 67 seconds, Round no. 6, Start Position at: 7
Counter1 = 9, Counter2 = 1, Counter3 = 0, Time for one Revolution = 67 seconds, Round no. 6, Start Position at: 7
Counter1 = 10, Counter2 = 1, Counter3 = 0, Time for one Revolution = 67 seconds, Round no. 6, Start Position at: 7
Counter1 = 11, Counter2 = 1, Counter3 = 0, Time for one Revolution = 67 seconds, Round no. 6, Start Position at: 7
Counter1 = 2, Counter2 = 1, Counter3 = 0, Time for one Revolution = 68 seconds, Round no. 7, Start Position at: 7

Normalerweise sollte der Counter 1 immer von 1-12 zählen und dann auf 1 zurück gehen

Edit: hoffentlich war es nicht schlimm das ich einen Doppelpost gemacht habe

also da ja der Counter trotzdem hochzählt ist nur die Serial. ausgabe zu langsam. aber da am ende ja keine Seriele ausgabe gefordert ist ist es nicht so schlimm