// Allow the user to calibrate the scales
void calibrateScales()
{
  short calibrateWeight = 0;

  eraseDisplay();
  displayCenteredTextLine(0, "GlideWheel-AS");
  displayCenteredTextLine(2, "Place the object");
  displayCenteredTextLine(3, "on the scales");
  displayCenteredTextLine(4, "and press");
  displayCenteredTextLine(5, "[enter]");
  displayCenteredTextLine(6, "to calibrate");
  while (nNxtButtonPressed != kEnterButton) sleep(1);
  debounce();
  eraseDisplay();
  calibrateWeight = weighObject();
  displayCenteredTextLine(0, "GlideWheel-AS");
  displayCenteredTextLine(2, "Enter the weight");
  displayCenteredTextLine(3, "in grams");
  displayCenteredTextLine(7, "-     OK     +");
  while (true)
  {
    displayCenteredBigTextLine(5, "%d", calibrateWeight);
    switch(nNxtButtonPressed)
    {
      case kLeftButton: playTone(500,10); calibrateWeight--; calibrateWeight = max2(0, calibrateWeight); break;
      case kRightButton: playTone(1000,10); calibrateWeight++; break;
      case kEnterButton: playTone(1500,10);gramsPerUnit = (float)calibrateWeight / (float)MSANGreadRaw(MSANG); eraseDisplay(); return;
    }
    sleep(20);
    debounce();
  }
}
task main () {
  nNxtButtonTask  = -2;
  displayCenteredTextLine(0, "Mindsensors");
  displayCenteredBigTextLine(1, "Angle");
  displayCenteredTextLine(3, "Test 2");
  displayCenteredTextLine(5, "Connect sensor");
  displayCenteredTextLine(6, "to S1");
  MSANGresetAngle(MSANG);
  sleep(2000);

  eraseDisplay();

  while (true) {
    // Reset the angle to 0
    if (nNxtButtonPressed == kEnterButton)
    {
      debounce();
      calibrateScales();
      debounce();
    }
    displayCenteredTextLine(0, "GlideWheel-AS");
    displayCenteredTextLine(1, "Weighing scale");
    displayTextLine(7, "[enter] to calib.");

    displayCenteredBigTextLine(4, "%d g", weighObject());

    sleep(20);
  }
}
void shutdown(void)
{
        INTCON=0b00001000;      //interrupt on pin change only
        PWM1DCH=0;      //zero output
        PWM1CON=0;      //turn off pwm
        LATA=0;         //ensure pin is low
        FVRCON=0;       //fvr off
        ADCON=0;        //adc off
        
        while(1)        //make this a loop so we stay here until sure the switch went down
        {
                do{
                        debounce();
                        delayms(1);     
                }while(pressed);        //ensure switch is up
                
                IOCAP=0; 
                IOCAN=0b00001000;       //interrupt on fall of ra3
                IOCAF=0;        //clear flags
        
                SLEEP();
                
                pressed=0; switch_count=10;
                for(char i=0; i<40; i++){       //watch for up to 40ms for a solid press
                        debounce();
                        delayms(1);
                        if(pressed) break;      //if pressed break out of for loop
                }
                if(pressed) break;      //if pressed break out of sleep loop
        }               
        
        configure();    //set up hardware for operation
        GIE=1;  //turn on interrupts
        
}       
Esempio n. 4
0
void TestServo4(){
    MoveServo4_Degrees(0);
    debounce(100);
    MoveServo4_Degrees(180);
    debounce(100);
    MoveServo4_Degrees(90);
    
}
Esempio n. 5
0
int main(void) {
    WDTCTL = WDTPW | WDTHOLD;	// Stop watchdog timer

    initSPI();

    LCDinit();

    LCDclear();

    unsigned char player = initPlayer();

    init_timer();
    init_buttons();
    __enable_interrupt();
   	printPlayer(player);
    while(1)
    {

    	player = movementCheck(player);
    	if(LOSE == 1){
    		LCDclear();
    		print("GAME");
    		secondLine();
    		print("OVER");
    		firstLine();
    		GAMEOVER = 1;
    		waitForP1ButtonRelease(BIT1|BIT2|BIT3|BIT4);
    		debounce();
    	}
    	if(didPlayerWin(player)){
    		LCDclear();
    		print("YOU");
    		secondLine();
    		print("WON");
    		firstLine();
    		GAMEOVER = 1;
    		waitForP1ButtonRelease(BIT1|BIT2|BIT3|BIT4);
    		debounce();
    	}
    	if(GAMEOVER){
    		char buttonsToPoll[4] = {BIT1, BIT2, BIT3, BIT4};
    		while(!pollP1Buttons(buttonsToPoll, 4)){
    			//poll until something is pressed
    		}
    		TAR = 0;
    		LOSE = 0;
    		TIMER = 0;
    		GAMEOVER = 0;
    		LCDclear();
    		player = initPlayer();
    		printPlayer(player);
    	}
    }

    return 0;
}
Esempio n. 6
0
int main(void) {

	init_buttons();
	init_leds();
	init_display(intensity);
	init_uart();

	uart_interrupt_enable();

	sei();

	// main loop
	while(1) {

		// while button 1 is pressed, increment display brightness (mod 16)
		uint8_t button_status = debounce(&BUTTON0_PIN, BUTTON0);
		if(button_status == 1) {
			intensity = (intensity + 1) % 16;
			set_register_b(REG_INTENSITY, intensity);
		} else if(button_status == 2) {
			LIGHT_PORT ^= (1<<LIGHT);
		}

		// if timer is stopped (but not yet resetted) ...
		if(state == 'S') {
			
			// ... and button 0 has been pressed (and released),
			// save the current time in the times array (only once!)
			if(debounce(&BUTTON1_PIN, BUTTON1) && !time_already_saved) {
				uart_interrupt_disable();
				append(times, TIMES, last_correct_decoded_time);
				led_blink(2);
				time_already_saved = 1;
				uart_interrupt_enable();
			}

		}

		// if timer has been stopped or reset ...
		if(state == 'I' || state == 'S') {

			// ... and button 0 is pressed, show the current average
			if(!(BUTTON2_PIN & (1<<BUTTON2))) {
				uart_interrupt_disable();
				display_uint16_time(average());
				my_delay_ms(1500);
				while(!(BUTTON2_PIN & (1<<BUTTON2)));
				uart_interrupt_enable();
			}

		}
	}
}
Esempio n. 7
0
__interrupt void Port_1_ISR(void) {
	if (BIT0 & P1IFG) {
		if (BIT0 & P1IES) {
			button = BIT0;
			timer = 0;
		} else {
			debounce();
		}

		P1IES ^= BIT0;
		P1IFG &= ~BIT0;
	}

	if (BIT1 & P1IFG) {
		if (BIT1 & P1IES) {
			button = BIT1;
			timer = 0;
		} else {
			debounce();
		}

		P1IES ^= BIT1;
		P1IFG &= ~BIT1;
	}

	if (BIT2 & P1IFG) {
		if (BIT2 & P1IES) {
			button = BIT2;
			timer = 0;
		} else {
			debounce();
		}

		P1IES ^= BIT2;
		P1IFG &= ~BIT2;
	}

	if (BIT3 & P1IFG) {
		if (BIT3 & P1IES) {
			button = BIT3;
			timer = 0;
		} else {
			debounce();
		}

		P1IES ^= BIT3;
		P1IFG &= ~BIT3;
	}
}
Esempio n. 8
0
void TestRange(){
    while(1){
    OC1R = 0xDA;                //0 degree value 
    while(OC1R <= 0x490){          //180 degree value
          OC1R += 0x10; 
          debounce(1);
    }
    
    
    while(OC1R >= 0xE0){        //180 degree value 
        OC1R -= 0x10;
        debounce(1);
    }
    }
}
Esempio n. 9
0
void SetTo90(){
                      ///Actually sets to vertically straight not 90 for each servo 
    OC1R = 0xE0;       
    debounce(2);
    OC4R = 0x2F6;
    debounce(2);
    OC2R = 0x1FF;
    debounce(2);
    OC3R = 0x320;
    debounce(2);
    OC2R = 0x320;
    debounce(5);
     
 
}
Esempio n. 10
0
void TestServo5(){
    OC5R = 0x110;  
    while(OC5R <= 0x465){
        OC5R += 0x10; 
        debounce(1); 
    }
}
Esempio n. 11
0
bool Switch::process()
{ deglitch();
  debounce();
  calcClick();
  calcLongPress();
  return _switched;
}
Esempio n. 12
0
//------------------------------------------------------------------------------
// An adaptation of the loop in main() in the original Babuino code.
// This is intended to be called from within the Arduino loop() function.
//------------------------------------------------------------------------------
void 
Babuino::loop()
{
	debounce();
	
	switch (_states.getMachineState())
		{
		case READY:
			if (serialAvailable() > 0)
			{
				_states.setMachineState(COMM);
			}
			else if (_states.getRunRequest() == RUNNING)
			{
				_states.setMachineState(RUN);
			}
			break;

		case COMM:
			doComm();
			_states.setMachineState(READY);
			break;

		case RUN:
			_regs.pc.set(_storage.getStartAddress());
			initStack(_stack);
			_states.setMachineState(RUN);
			code_exec();
			_motors.off();
			_states.setMachineState(READY);
			break;
		}
}
Esempio n. 13
0
//Function used to determine which button has been pushed without polling
void testAndRespondToButtonPush(char buttonToTest) {
	if (buttonToTest & P1IFG) {

		if (buttonToTest & P1IES) {

			if (flag == 5) {
				LCDclr();
				location = initPlayer();
				printPlayer(location);
				flag = 0;
			}

			else {
				//This code is used to prevent the player (*) from moving outside of the game bounds.
				int mod = 0;
				mod = movePlayerInResponseToButtonPush(buttonToTest);
				clearPlayer(location);
				location += mod;
				location = movePlayer(location, mod);
				clearTimer();
			}

			//Debounces the button
		} else {
			debounce();
		}

		//Toggles between reading rising and falling edge
		P1IES ^= buttonToTest;
		P1IFG &= ~buttonToTest;
	}

}
Esempio n. 14
0
bool is_encoder_pressed() {
  if (debounce(PIND, PD4)) {
    return true;
  } else {
    return false;
  }
}
Esempio n. 15
0
int read_LCD_buttons() {
	adc_key_in = debounce(adc_key_in);
	if (adc_key_in < V2) return btnUP;
 	if (adc_key_in < V3) return btnDOWN;
 	if (adc_key_in < V4) return btnLEFT;
 	if (adc_key_in < V5) return btnSELECT;
 	return btnNONE;
}
uint8_t matrix_scan(void) {
  bool changed = custom_matrix_scan(raw_matrix);

  debounce(raw_matrix, matrix, MATRIX_ROWS, changed);

  matrix_scan_quantum();
  return 1;
}
Esempio n. 17
0
void TestServo6(){
    
    OC6R = 0x400;
    while(OC6R >= 0x275){
        OC6R -= 0x10; 
        debounce(1); 
    }
}
Esempio n. 18
0
void MakeSelection(S1){
    
    switch(S1){
        case 1:  TestServo1();
                 debounce(1); 
                 break;
        case 2:  TestServo2();
                 debounce(1);
                 break;
        case 3:  TestServo3();
                 debounce(1);
                  break;   
        case 4:  TestServo4();
                 debounce(1);
                 break;
        case 5:  TestServo5();
                 debounce(1);
                 break;
        case 6:  TestServo6();
                 debounce(1);
                 break;
        case 7:  SetTo90();
                 debounce(1);
                 break;
        case 8:  GrabBlock();
                 break;
        case 9:  Jog();
                 break;
                 
        
    }
}
Esempio n. 19
0
uint8_t cButtonBase::readButtons() {
  uint32_t ms = millis();
  if( ms >= nextCheck ) {
    debounce();
    nextCheck = ms + PERIOD;
    return changed;
  }
  else return 0;  // if a new value was not read, than nothing can change
}
void interrupt isr(void)
{

        if(TMR0IF){     //fires at 1kHz
                TMR0IF=0;
                debounce();
                if(++v_timer==voltage_rate){v_timer=0; v_sample=1;}
        }
}
Esempio n. 21
0
void Jog(){
    
    SetTo90();
    
    OC5R = 0x465;
    debounce(100);
    OC5R = 0x150;
    debounce(100);
    OC1R = 0xE0;
    MoveServo5_Degrees(0);
    MoveServo5_Degrees(180);
    OC1R = 0x485; 
    
    
    
    
    
}
Esempio n. 22
0
uint8_t Pushbutton::readDebounced(void)
{
  if (_pin!=PIN_NOT_SET)
  {
    while (debounce()==0);
    return read();
  }
  return (uint8_t)-1;
}
Esempio n. 23
0
void encoder_ButtonHandler(void)
{
	debounce();
	uint32_t button = IO002_ReadPin(IO002_Handle2);
	if(!button)
	{
		input = BUTTON_PUSH	;
	}
}
// Determine whether the falling signal endge has been detected
boolean SwitchableButton::fallingEdgeDetected()
{
	// Save current pin state
	boolean previousState = state();
	// Call debouncing method
	setState(debounce(previousState));
	
	// Return the result
	return state() == LOW && previousState != HIGH;
}
// Determine whether the button has been released
boolean SwitchableButton::released()
{
	// Save current pin state
	boolean previousState = state();
	// Call debouncing method
	setState(debounce(previousState));
	
	// Return the result
	return state() == initialState() && previousState != initialState();
}
Esempio n. 26
0
void keyscanner_main(void)
{
    /* TODO: low power mode:
     *   When all keys reported up:
     *     DDR_PP = 0x11; PORT_PP = 0x00;
     *     Guarantee wake on TWI / any PORT_OD pin FALLING
     *     Sleep
     */
    for (uint8_t pp = 0; pp < 8; ++pp) {
        uint8_t pp_bitmask = _BV(pp);

        _delay_ms(0.5);

        DDR_PP = 0x00 ^ pp_bitmask;
        PORT_PP = 0xFF ^ pp_bitmask;

        _delay_ms(0.5);

        uint8_t od_bits = PIN_OD;
        /*
         * Rollover conditions exist if:
         *  * Multiple OD pins are pulled low AND
         *  * Multiple PP pins are pulled low
         */
        uint8_t nPp = popCount(~PIN_PP);
        uint8_t nOd = popCount(~od_bits);
        // Most of the time the keyboard will not be a rollover state
        if (__builtin_expect(nPp > 1 && nOd > 1, 0)) {
            continue;
        }

        // Debounce key state
        uint8_t changes = debounce(od_bits, db + pp);
        // Most of the time there will be no new key events
        if (__builtin_expect(changes == 0, 1)) {
            continue;
        }

        DISABLE_INTERRUPTS({
            key_t key;
            key.dataNumber = 0; // Set by I²C code (ringbuf.count != 0)
            key.pp = pp;

            for (int8_t od = 0; od < 8; od++) {
                // Fewer than half the keys are expected to be down for each scanline
                if (__builtin_expect(bit_is_set(changes, od), 0)) {
                    key.keyState = bit_is_clear(db[pp].state, od);
                    key.od = od;
                    ringbuf_append(key.val);
                }
            }

            SET_INT(0);
        });
    }
Esempio n. 27
0
void tic_timer0(void) {
    static unsigned char debounce_tic = DebounceSet;
    if (mux_tout)mux_tout--;
    if (bot_tout)bot_tout--;
    if (led_tout)led_tout--;
    //llama a la funcion debounce si han pasado 
    if (debounce_tic)debounce_tic--;
    else {
        debounce_tic = DebounceSet;
        EstadoDeLosBotones = ((debounce(PORTD))^0xF0)&0xF0;
    }
}
Esempio n. 28
0
/*
 * Get the button's status.
 */
char AnaButtons::getStatus() {
    // if we have raised one status and the interval is not over just ignore
    word tdiff = millis() - lastMillis;
    if ((lastPressed != NOBUTTON) & (tdiff < intervalMillis)) {
        return NOBUTTON;
    }

    // read the analog pin
    analogReference(DEFAULT);
    word value = analogRead(pin);

    // get the delta averaged
    amove = lastValue;
    amove -= value;
    amove = word(abs(amove));
    tdiff = ave(amove);

    // is the value stable?
    if (tdiff > THRESHOLD) {
        // no, it's not
        lastValue = value;

        // just return no change
        return debounce(NOBUTTON);
    } else {
        // it's stable, parse the defined values
        if ((value > 430) & (value < 674))
            return debounce(ABUTTON1_PRESS);

        if ((value > 256) & (value < 421))
            return debounce(ABUTTON2_PRESS);

        if ((value > 140) & (value < 237))
            return debounce(ABUTTON3_PRESS);

        if ((value > 75) & (value < 122))
            return debounce(ABUTTON4_PRESS);

        if ((value > 36) & (value < 60))
            return debounce(ABUTTON5_PRESS);

        if (value < 10)
            return debounce(ABUTTON6_PRESS);

        // if you get here, you are on a undefined range,
        // so you get a "no button pressed at all"
        return debounce(NOBUTTON);
    }
}
Esempio n. 29
0
void testAndRespondToButtonPush(char buttonToTest){
        if (buttonToTest & P2IFG){
                if(buttonToTest & P2IES){
                        movingPlayer(buttonToTest);
                        clearTimer();
                }
                else{
                        debounce();
                }
                P2IES ^=buttonToTest;
                P2IFG &= ~buttonToTest;
        }
}
Esempio n. 30
0
int main(void) {
  //GIMSK = 0;
  
  bit_clear(BUTTON_DDR, BUTTONS);
  bit_set(BUTTON_PORT, BUTTONS);

  LED_DDR = B8(1);

  // set up an interrupt that goes off at 20Hz - by default
  TCCR0A = BIT(WGM01);
  TCCR0B = B8(101); // 1M/1024 = ~977
  // REM: prescaler reset? PSR10 = 1
  TIMER_THRESH = DEFAULT_TIMER_THRESH; // 977 / 49 = ~20
  TIMSK0 = BIT(OCIE0A); // turn on the interrupt
  sei(); // turn on interrupts

  TIMER_THRESH_DDR = B8(11111111);
  TIMER_THRESH_PORT = TIMER_THRESH;
  
  LED_PORT = 0;

  uint8_t count = 0;
  uint8_t wasBoth = 0;  // ENHANCEME: working out how to do this more like a real state machine would rock

  while (1) {
    uint8_t changes = debounce(~(BUTTON_PIN & BUTTONS));  // invert button state so 1 means "pressed"

    if (changes) {
      if (bit_read(debouncedState, BUTTON_INC) && bit_read(debouncedState, BUTTON_DEC)) {
	// both buttons were pressed
	TIMER_THRESH = DEFAULT_TIMER_THRESH;
	wasBoth = 1;
      } else if ( wasBoth && ! bit_read(debouncedState, BUTTONS)) {
	// both buttons were released
	wasBoth = 0;
      }	else if ( ! wasBoth && bit_read(changes, BUTTON_INC) && bit_read(debouncedState, BUTTON_INC)) {
	// increment button was pressed
	TIMER_THRESH -= TIMER_THRESH_STEP;  // reducing the timer threshold makes it fire faster
      }	else if ( ! wasBoth && bit_read(changes, BUTTON_DEC) && bit_read(debouncedState, BUTTON_DEC)) {
	// decrement button was pressed
	TIMER_THRESH += TIMER_THRESH_STEP;
      }
    }

    //LED_PORT = count;  // invert count since the LEDs are being sink-driven
    TIMER_THRESH_PORT = TIMER_THRESH;
    _delay_ms(1);
  }
}