示例#1
0
static void button_readState() {
  /* middle button - pause */
  int button_press;
  int button_release;
  button_press = get_single_debounced_button_press(ANY_BUTTON);
  button_release = get_single_debounced_button_release(ANY_BUTTON);
  switch (g_middle_button.state) {
  case BUTTON_STATE_RELEASED:
    if (button_press & MIDDLE_BUTTON) {
      g_middle_button.state = BUTTON_STATE_PRESSED;
      if (g_motor_state.enabled) {
	g_motor_state.enabled = false;
	g_motor_state.direction = DIRECTION_FORWARD;
	play_note(A(5), 200, 12);
      } else {
	g_motor_state.enabled = true;
	play_note(A(5), 200, 12);
      }
    }
    break;
  case BUTTON_STATE_PRESSED:
    if (button_release & MIDDLE_BUTTON) {
      g_middle_button.state = BUTTON_STATE_RELEASED;
    }
    break;
  }

  /* bottom button - speed down */
  switch (g_bottom_button.state) {
  case BUTTON_STATE_RELEASED:
    if (button_press & BOTTOM_BUTTON) {
      g_bottom_button.state = BUTTON_STATE_PRESSED;
      g_motor_state.speed = MIN(g_motor_state.speed + 5, 255);
    }
    break;
  case BUTTON_STATE_PRESSED:
    if (button_release & BOTTOM_BUTTON) {
      g_bottom_button.state = BUTTON_STATE_RELEASED;
    }
    break;
  }

  /* top button - speed up */
  switch (g_top_button.state) {
  case BUTTON_STATE_RELEASED:
    if (button_press & TOP_BUTTON) {
      g_top_button.state = BUTTON_STATE_PRESSED;
      g_motor_state.speed = MAX(g_motor_state.speed - 5, 0);
    }
    break;
  case BUTTON_STATE_PRESSED:
    if (button_release & TOP_BUTTON) {
      g_top_button.state = BUTTON_STATE_RELEASED;
    }
    break;
  }
  
}
示例#2
0
int main()
{
	lcd_init_printf();
	clear();	// clear the LCD
	printf("Time: ");
	
	while (1)
	{
		unsigned char button = get_single_debounced_button_press(ANY_BUTTON);
		switch (button)
		{
			case BUTTON_A:
				play_note(A(4), 50, 10);
				break;
			case BUTTON_B:
				play_note(B(4), 50, 10);
				break;
			case BUTTON_C:
				play_note(C(5), 50, 10);
		}

		button = get_single_debounced_button_release(ANY_BUTTON);
		switch (button)
		{
			case BUTTON_A:
				play_note(A(5), 50, 10);
				break;
			case BUTTON_B:
				play_note(B(5), 50, 10);
				break;
			case BUTTON_C:
				play_note(C(6), 50, 10);
		}

		unsigned long ms = get_ms();	// get elapsed milliseconds
		// convert to the current time in minutes, seconds, and hundredths of seconds
		unsigned char centiseconds = (ms / 10) % 100;
		unsigned char seconds = (ms / 1000) % 60;
		unsigned char minutes = (ms / 60000) % 60;

		lcd_goto_xy(0, 1);				// go to the start of the second LCD row
		// print as [m]m:ss.cc (m = minute, s = second, c = hundredth of second)
		printf("%2u:%02u.%02u", minutes, seconds, centiseconds);
	}

}
int main()
{
  // Initialize the encoders
  encoders_init(IO_D2, IO_D3, IO_A3, IO_A2);
  float count = 0;
  char forward = TRUE;
  int motor_speed = 100;

  //set_motors(100, 255);
  while(1)
  {
    clear();
    lcd_goto_xy(0,0);

    //Calculate the count
    count = encoders_get_counts_m1() / ENCODER_COUNT_PER_REVOLUTION;

    // Increment and decrement speed if top or bottom buttons were pressed.
    unsigned char button = get_single_debounced_button_press(ANY_BUTTON);

    if ((button & TOP_BUTTON) && motor_speed < 250) // if top button pressed
      motor_speed = motor_speed + 5;
    if ((button & BOTTOM_BUTTON) && motor_speed > 80) // if bottom button pressed
      motor_speed = motor_speed - 5;

    //If the user is holding the middle button we want to stop the motor.
    if(button_is_pressed(MIDDLE_BUTTON) & MIDDLE_BUTTON)
    {
      print("Paused ");
      set_motors(0,0);
    }
    else
    {
      // Set motor speed dependent on direction.
      if(forward)
      {
        set_motors(motor_speed, 0);
        print("Forward ");
      }
      else
      {
        set_motors(-motor_speed, 0);
        print("Reverse ");
      }

      // At 0 and 2 we need to switch directions.
      if(count >= 2 && forward)
        forward = FALSE;

      if(count <= 0 && !forward)
        forward = TRUE;
    }

    // Print the motor speed and revolutions
    print_long(motor_speed); 
    print_count(count); 

    //Delay so LCD doesn't flicker too much
    delay_ms(50);
  }
}
示例#4
0
int main()
{
	DDRC = 0;                              // all inputs
	PORTC = (1 << PORTC4) | (1 << PORTC5); // enable pull-ups on SDA and SCL, respectively

	TWSR = 0;  // clear bit-rate prescale bits
	TWBR = 17; // produces an SCL frequency of 400 kHz with a 20 MHz CPU clock speed

	clear();  

	//enable accelerometer
	i2c_start(); 
	i2c_write_byte(0x30); // write acc
	i2c_write_byte(0x20); // CTRL_REG1_A
	i2c_write_byte(0x27); // normal power mode, 50 Hz data rate, all axes enabled
	i2c_stop();

	//enable magnetometer
	i2c_start(); 
	i2c_write_byte(0x3C); // write mag
	i2c_write_byte(0x02); // MR_REG_M
	i2c_write_byte(0x00); // continuous conversion mode
	i2c_stop();

	vector a, m;
	char ribbon_segment[8];
	unsigned char button;
	enum calibration_mode mode = CAL_NONE;
	vector cal_m_max = {0, 0, 0};
	vector cal_m_min = {0, 0, 0};

  	while(1)
	{
		// see if a button was pressed to enable calibration mode
		// each button displays max and min measurements for one axis:
		// top = X, middle = Y, bottom = Z
		// if any button is pressed a second time, return to normal mode
		button = get_single_debounced_button_press(ANY_BUTTON);

		if (button & TOP_BUTTON)
		{
			if (mode != CAL_X)
				mode = CAL_X;
			else
				mode = CAL_NONE;
		}
		else if (button & MIDDLE_BUTTON)
		{
			if (mode != CAL_Y)
				mode = CAL_Y;
			else
				mode = CAL_NONE;
		}
		else if (button & BOTTOM_BUTTON)
		{
			if (mode != CAL_Z)
				mode = CAL_Z;
			else
				mode = CAL_NONE;
		}

		
		if (mode == CAL_NONE) // normal mode - display heading and compass ribbon
		{
			vector a_avg = {0,0,0}, m_avg = {0,0,0};
		
			// take 5 acceleration and magnetic readings and average them
			for(int i = 0; i < 5; i++)
			{
				read_data(&a, &m);

				a_avg.x += a.x;
				a_avg.y += a.y;
				a_avg.z += a.z;
				m_avg.x += m.x;
				m_avg.y += m.y;
				m_avg.z += m.z;
			}
			a_avg.x /= 5;
			a_avg.y /= 5;
			a_avg.z /= 5;
			m_avg.x /= 5;
			m_avg.y /= 5;
			m_avg.z /= 5;

			int heading = get_heading(&a_avg, &m_avg, &p);

			// select the portion of the ribbon to display
			strlcpy(ribbon_segment, &ribbon[(heading + 5) / 10], 8);

			clear();
			print_long(heading);
			lcd_goto_xy(4, 0);
			print_character('v');  // center indicator
			lcd_goto_xy(1, 1);
			print(ribbon_segment); // ribbon segment
		}
		else // calibration mode - record and display max/min measurements
		{
			read_data_raw(&a, &m);
			if (m.x < cal_m_min.x) cal_m_min.x = m.x;
			if (m.x > cal_m_max.x) cal_m_max.x = m.x;
			if (m.y < cal_m_min.y) cal_m_min.y = m.y;
			if (m.y > cal_m_max.y) cal_m_max.y = m.y;
			if (m.z < cal_m_min.z) cal_m_min.z = m.z;
			if (m.z > cal_m_max.z) cal_m_max.z = m.z;

			clear();

			switch (mode)
			{
				case CAL_X:
					print("Xmax:");
					print_long(cal_m_max.x);
					lcd_goto_xy(0, 1);
					print("min:");
					print_long(cal_m_min.x);
					break;
				case CAL_Y:
					print("Ymax:");
					print_long(cal_m_max.y);
					lcd_goto_xy(0, 1);
					print("min:");
					print_long(cal_m_min.y);
					break;
				default:
					print("Zmax:");
					print_long(cal_m_max.z);
					lcd_goto_xy(0, 1);
					print("min:");
					print_long(cal_m_min.z);
					break;
			}
		}

		delay_ms(100);
	}
}