Trying to write an event listener with Teensyduino 2.0

Status
Not open for further replies.

farina

New member
I'm trying to listen to triggers from hall effect sensors on my teensyduino 2.0 board, and I'm having a lot of trouble coding this correctly. As it stands right now, my key press event isn't actually releasing. My thought is that there must be a way to actually trigger the board rather than run an infinite loop?

Here is the code I've written

Code:
int laneOnePin = 5;
int laneTwoPin = 6;
int laneThreePin = 7;
int laneFourPin = 8;

int redPin =  12;
int greenPin =  15;
int bluePin =  14;

int laneOneTrigger;
int laneTwoTrigger;
int laneThreeTrigger;
int laneFourTrigger;

void setup()   {                
  Serial.begin(38400);
  pinMode(laneOneTrigger, INPUT);
  pinMode(laneTwoTrigger, INPUT);
  pinMode(laneThreeTrigger, INPUT);
  pinMode(laneFourTrigger, INPUT);
  pinMode(redPin, OUTPUT);
  pinMode(greenPin, OUTPUT);
  pinMode(bluePin, OUTPUT);
  
   // initialize control over the keyboard:
  Keyboard.begin();
}

void loop()                     
{
  laneOneTrigger = digitalRead(laneOnePin);
  laneTwoTrigger = digitalRead(laneTwoPin);
  laneThreeTrigger = digitalRead(laneThreePin);
  laneFourTrigger = digitalRead(laneFourPin);
  
  Serial.print("magnet is: ");
  
  if (laneOneTrigger == 0) {  
    Serial.println("near lane one");
    // Turn USB red
    analogWrite(redPin, 255);
    analogWrite(greenPin, 0);
    analogWrite(bluePin, 0);
    Keyboard.press(KEY_LEFT_CTRL);
    Keyboard.press(KEY_LEFT_ALT);
    Keyboard.press(KEY_F1);
    delay(200);
    Keyboard.releaseAll();
  } else if (laneTwoTrigger == 0) {  
    Serial.println("near lane two");
    // Turn USB yellow
    analogWrite(redPin, 255);
    analogWrite(greenPin, 255);
    analogWrite(bluePin, 0);
    Keyboard.press(KEY_LEFT_CTRL);
    Keyboard.press(KEY_LEFT_ALT);
    Keyboard.press(KEY_F2);
    delay(200);
    Keyboard.releaseAll();
  } else if (laneThreeTrigger == 0) {  
    Serial.println("near lane three");
    // Turn USB green
    analogWrite(redPin, 0);
    analogWrite(greenPin, 255);
    analogWrite(bluePin, 0);
    Keyboard.press(KEY_LEFT_CTRL);
    Keyboard.press(KEY_LEFT_ALT);
    Keyboard.press(KEY_F3);
    delay(200);
    Keyboard.releaseAll();
  } else if (laneFourTrigger == 0) {  
    Serial.println("near lane four");
    // Turn USB blue
    analogWrite(redPin, 0);
    analogWrite(greenPin, 0);
    analogWrite(bluePin, 255);
    Keyboard.press(KEY_LEFT_CTRL);
    Keyboard.press(KEY_LEFT_ALT);
    Keyboard.press(KEY_F4);
    delay(200);
    Keyboard.releaseAll();
  } else {
    Serial.println("not near any lane");
    delay(200);
    Keyboard.releaseAll();
    analogWrite(redPin, 0);
    analogWrite(greenPin, 0);
    analogWrite(bluePin, 0);  
  }
    
  delay(10);
}

Any suggestions would be fantastic!! Thank you.
 
Code:
  pinMode(laneOneTrigger, INPUT);
  pinMode(laneTwoTrigger, INPUT);
  pinMode(laneThreeTrigger, INPUT);
  pinMode(laneFourTrigger, INPUT);

Those should be the pin numbers.

Pete
 
Good observation, I corrected that, however I'm still having issue with the program. It actually flashes the LED by itself between green and blue, but it seems totally random. Is there a way to do this without a loop and just have board listening for the trigger from the hall effect sensor?
 
Good news, I figured out that my issue was related to some of the pins not being connected yet. I used "INPUT_PULLUP" as the type, and now it works perfectly. I still feel like this is the sloppiest code I've ever written...but it does the job I guess :)
 
I highly recommend using the Bounce library for reading pushbuttons.

There's an example in File > Examples > Teensy > USB_Keyboard > Buttons.
 
Status
Not open for further replies.
Back
Top