Break Beam with a Teensy 3.2

Status
Not open for further replies.

Iceman

New member
Hi all,

I upgraded from Teensy 2.0 to a Teensy 3.2
I cannot seem to get this to work now.
Do I have it on the correct pins??
I am using the 3.3V for the Break Beam power
See below for code...

Please Help...
Thank you


I have a Windows 10 Pro OS.
Arduino programming HUD ver. 1.8.9
TeensyduinoInstall ver: 1.52
Board: Teensy 3.2 /3.1
USB Type: "Serial + Keyboard + Mouse + Joystick"
CPU Speed: "24 MHz"
Optimize: "Faster"
Port: Com 3


Code:
#define SENSORPIN 10
#define LEDPIN 13
  // Pin 13: Arduino has an LED connected on pin 13
  // Pin 11: Teensy 2.0 has the LED on pin 11
  // Pin  6: Teensy++ 2.0 has the LED on pin 6
  // Pin 13: Teensy 3.0 has the LED on pin 13

// add for Piezo
int piezo = 9;
int duty_cycle = 2.1;
// end add in for Piezo


int keyAllowed = 1;
int key1 = MODIFIERKEY_ALT;
int key2 = KEY_P;
// variables will change:
int sensorState = 0, lastState=0;         

void setup() {
  // initialize the LED pin as an output:
  pinMode(LEDPIN, OUTPUT);      
  // initialize the sensor pin as an input
  digitalWrite(SENSORPIN, HIGH); // turn on the pullup  HIGH
  // add for Piezo
  digitalWrite(piezo, HIGH);
  digitalWrite(piezo, LOW);
  // end of Piezo
  
  Serial.begin(9600);
  
  // add in for Piezo
  pinMode(piezo, OUTPUT);
  // end add in for Piezo
}

// added for Piezo
void beep(float frequency, float time) {
 for(int i = 0; i < (time / (1000 / frequency)); i ++) {
  digitalWrite(piezo, HIGH);
   delayMicroseconds(1000000) ; // / (frequency * duty_cycle);
  digitalWrite(piezo, LOW);
   delayMicroseconds(1000000) ; // / frequency * (1.0 - duty_cycle));
}
}
// end of Piezo
void loop()
{
  // read the state of the breakbeam
  sensorState = digitalRead(SENSORPIN);
  // check if the sensor beam is broken
  // if it is, the sensorState is LOW:
  if (sensorState == LOW && keyAllowed > 0) {     
    // turn LED on:
    digitalWrite(LEDPIN, HIGH);  
   // digitalWrite(LEDPIN, LOW);
    Keyboard.press(key1);
    Keyboard.press(key2);    
    //add in for Piezo
  beep(440, 100);
  beep(880,200);
    //end add in for Piezo
    
    delay(10);
    Keyboard.release(key2);
    Keyboard.release(key1);
    keyAllowed = 0;
    //delay(500); Default Light Curtain Delay 500 ms
    // Light Curtain Delay set to 10 seconds (10000 ms) per QA
    // Set only on the Thermatrons
    delay(500);
  } 
  if (sensorState == HIGH) {
    // turn LED off:
    digitalWrite(LEDPIN, LOW);  
   // digitalWrite(LEDPIN, HIGH); 
    keyAllowed = 1;
    delay(10);      
  }
}
 
Last edited by a moderator:
Forgot to mention...
When the unit connected via USB, the LED light comes on and stays on.
It is supposed to stay off until something breaks the beam, then come on (digitalWrite(LEDPIN, HIGH), send a couple of keyboard presses, beep.... then after the delay(500) basically reset (LED off, Sound Off) and be ready for next pass through...

Thanks
 
In setup, remove this statement:
Code:
digitalWrite(SENSORPIN, HIGH); // turn on the pullup HIGH
and replace it with this:
Code:
  pinMode(SENSORPIN,INPUT_PULLUP);
It should now wait for the SENSORPIN to go LOW. When it is LOW, it turns on the LED and starts sending the keyboard codes but it sends them repeatedly even after the SENSORPIN is HIGH again.
If the beam isn't broken very rapidly, I would use the bounce library to handle the debouncing.

Pete
P.S. before you post code, use the Auto Format in the IDE Tools menu and then post your code in code tags (the # icon generates them for you).
 
Thank you el_supremo,

That was it... I tweaked the code a lot more and it works perfectly now.

Thank you...
 
Status
Not open for further replies.
Back
Top