int main(int argc, char **argv, char **envp)
{
	if (argc != 4) {										// check for any arguments, if none, display messages
		printf("Error during execution:\n");
		printf("USAGE: <filename> <gpio-pin> <gpio-pin> <gpio-pin>\n\n");
		exit(-1);
	} else {											// if received arguments, display message
		printf("Program Started ... \nPlease press <enter> to exit this program.\n");
	}

	setup_pins();

	struct gpio_struct args;
	args.gpio_1 = atoi(argv[1]);							// get the first argument
	args.gpio_2 = atoi(argv[2]);							// get the first argument
	args.gpio_3 = atoi(argv[3]);							// get the first argument

	pthread_t water_sensor, interrupt_service;

	/* Thread 1 to collect water data from water sensor */
	if( pthread_create(&water_sensor, NULL, &get_water_sensor, NULL) != 0){
		printf("Error: unable to set water sensor thread\n");
		return -1;
	}

	/* Thread 2 responsible for interrupt service */
	if( pthread_create(&interrupt_service, NULL, &interrupt_func, (void *)&args) != 0){
		printf("Error: unable to set interrupt service thread\n");
		return -1;
	}

	pthread_join(interrupt_service, NULL);
	return 0;
}
Example #2
0
int main(int argc, char *argv[]){
    check4running(PIDFILE, NULL);
    initial_setup();
    Global_parameters = parse_args(argc, argv);
    assert(Global_parameters);
    if(!get_shm_block(&sdat, ClientSide) || !check_shm_block(&sdat)){
        ERRX("Can't get SHM block!");
    }
    #ifndef EBUG
    PRINT(_("Test multicast connection\n"));
    double last = M_time;
    WAIT_EVENT((fabs(M_time - last) > 0.02), 5.);
    if(tmout && fabs(M_time - last) < 4.)
        ERRX(_("Multicasts stale!"));
    #endif

    signal(SIGTERM, signals); // kill (-15)
    signal(SIGHUP, SIG_IGN);  // hup - daemon
    signal(SIGINT, signals);  // ctrl+C
    signal(SIGQUIT, signals); // ctrl+\   .
    signal(SIGTSTP, SIG_IGN); // ctrl+Z
    setbuf(stdout, NULL);

    setup_pins();
    if(Global_parameters->absmove && gotozero()) ERRX(_("Can't locate zero-endswitch"));

    if(Global_parameters->gotoangle > -360. && Global_parameters->gotoangle < 360.){
        if(Global_parameters->absmove) Global_parameters->gotoangle += PA_ZEROVAL;
        if(gotoangle(Global_parameters->gotoangle)) ERRX(_("Can't move for given angle"));
    }

    stepper_process();
    return 0;
}
Example #3
0
int main(void) {
	m_clockdivide(0);
	setup_pins();
	if ( RF_debug) {setupUSB();}
	setup_timer_1();
	setup_timer_3();
	m_bus_init();
	m_rf_open(chan,RX_add,p_length);

	int timer_3_cnt = 0;

	//sei();
	set_motors(0,0);
	while (1){
		if (check(TIFR3,OCF3A)){
			set(TIFR3, OCF3A);
			timer_3_cnt++;
			if ( timer_3_cnt % 10 ==0 ) {
				timer_3_cnt=0;
				m_green(TOGGLE);
			}
			if ( timer_3_cnt == 1000){ timer_3_cnt=0;}
		}
		if(new){
			switch ( receive_buffer[11] ) {
				case Single_Joy: single_joystick(); break;
				case Double_Joy: double_joystick(); break;
				case Tank_Mode: tank_driving(); break;
				case Mario_Kart: Mario_Drive(); break;
				default : set_motors(0,0); m_green(2);
			}
			if(RF_debug){ debug_rf(); }
		}
	}
}
Example #4
0
int main(void) { 

	m_disableJTAG();
	m_clockdivide(2);
	setup_pins();
	if (debug_fire|| RF_debug) {setupUSB();}
	setup_timer_1();
	setup_timer_3();
	m_bus_init();
	m_rf_open(chan,RX_add,p_length);

	int timer_3_cnt = 0;

	//sei();
	set_motors(0,0);
	while (1){
		if (check(TIFR3,OCF3A)){
			set(TIFR3, OCF3A);
			timer_3_cnt++;
			
			if(fired){
				since_fired++;
				if (debug_fire){
					m_usb_tx_string(" its been\t");
					m_usb_tx_int(since_fired);
					m_usb_tx_string(" milisec\n\r");
				}
			}
			if (since_fired>10){ 
				clear(PORTF,5);
				since_fired=0;
				fired = false;
				if (debug_fire){m_usb_tx_string(" its been 100 sec\n\r");}
			} 
			if ( fire && check(PINB,3)){ 
				fire=false; 
				fired=true; 
				since_fired=0;
				if (debug_fire){m_usb_tx_string(" portb 3 is high\n\r");}
			}
			
//			m_rf_open(chan,RX_add,p_length);
			m_green(TOGGLE);
			m_rf_init();
			if ( timer_3_cnt == 10){
				m_red(2);
				timer_3_cnt=0;
				m_rf_open(chan,RX_add,p_length);
			}
		}
		if(new){ turretDrive();
			if(RF_debug){ debug_rf(); }
			if ((receive_buffer[0] == 1 || receive_buffer[1]==1) && !fire){FIRE();}
		}
		//TODO fill in timer code for the firing mechanism
	}
}
Example #5
0
int main(void)
{
    setup_pins();
    while(1) {
        PORTB = 0xff;
        _delay_us(600);
        PORTB = 0;
        _delay_us(1200);
    }
}
int main (int argc, char *argv[])
{

    if (setup_pins(49) != 0)    /* Set up the pin routing on the Galileo board */
        exit (-1);

    aio0();                    /* Run the ADC program */

    (void)gpio_dealloc(49);    /* Unreserve the GPIO pin */

    exit (0);
}
Example #7
0
/**
 * Main set up routine.
 */
void
setup(void) {
  setup_processor_clocks();
  setup_pins();
  setup_usart();
  setup_dma();

  // Enable low and medium level interrupts.
  PMIC.CTRL = PMIC_LOLVLEN_bm | PMIC_MEDLVLEN_bm;

  /* Enable interrupts. */
  sei();
}
Example #8
0
void
setup(void) {
  setup_clocks();
  setup_pins();

  // Initialize the USB device.
  USB_Init();

  // Turn on all low level interrupts.
  PMIC.CTRL |= PMIC_LOLVLEN_bm;

  // Turn on interrupts.
  sei();
}
Example #9
0
void _startup( void )
{
  int i;
#if NDEVS && SERIAL1
    struct uart_csreg *uart1_regptr;
#endif

    /*
     * We need to cheat here and run some code to ensure that
     * kprintf() can work before calling nulluser().
     */
    setup_pins();

    /* SERIAL1 is the bluetooth device our kprintf() uses. */
#if NDEVS && SERIAL1
    /*
     * Platform initialization of bluetooth for our kprintf() over
     * bluetooth to work (over bluetooth).
     */

    /* setup Pin Function Select Register (Pin Connect Block) make
     * sure old values of Bits 0-4 are masked out and set them
     * according to UART1-Pin-Selection.
     */
    //PINSEL0 = (PINSEL0 & ~UART1_PINMASK) | UART1_PINSEL;
    /* turn on CTS */
    //PINSEL0 = (PINSEL0 & ~UART1_CTS_PINMASK) | UART1_CTS_PINSEL;

    /* Actually configure the UART1's registers */
    //uart1_regptr = devtab[SERIAL1].csr;
   // fluke_uart_reg_init(uart1_regptr, 38400);

    /*
     * kprintf() needs to be informed of the CSR, it uses uarttab
     * instead of devtab to get it for unknown reason.
     */
    //uarttab[devtab[SERIAL1].minor].csr = uart1_regptr;

  // usleep(1000);
#endif /* NDEVS && SERIAL1 */

    nulluser();
}
Example #10
0
void gen_setup(void){
	usb_conn_test = Matlab_pos_tracking ||  go_to_location_test || debug_com || debug_goto || debug_find_puck; 
	if (usb_conn_test ){
		m_usb_init();
		m_green(ON);
		while (!m_usb_isconnected());
		m_green(OFF);
	} 
	m_bus_init();
	m_wii_open();	
	// m_port_init(PORT_ADD);

	setup_pins();

	setup_puckfinding();		//Puck Finding Intialization

	setup_timer_1();
	setup_timer_3();
	setup_ADC();


}
Example #11
0
int main(void)
{
    setup_pins();
    setup_comparator();
    setup_timer0();

    ledsetup();
    uint8_t message_buffer[12] = {0};
    Controller *controller = (Controller*)message_buffer;
    machine_init();

    Animation anim;
    clear_animation(&anim);
    bool prev_machine_toggle = true;

    delay_ms(1000);

    while(1)
    {
        // Wait until we have a valid message
        while(!getMessage(message_buffer)) {}

        // Interpret the input
        process_input(controller, &anim); 

        if((prev_machine_toggle && !machine_toggle) ||
            machine_toggle)
        { 
            show_animation(&anim);
        }
        clear_animation(&anim);
        prev_machine_toggle = machine_toggle;

        // Wait for the 2nd paired request to pass
        _delay_us(2000);
    }
}
Example #12
0
int main(void) {
	uint8_t pad_up, pad_down, pad_left, pad_right, pad_y, pad_b, pad_x, pad_a, pad_black,
	pad_white, pad_start, pad_select, pad_l3, pad_r3, pad_l, pad_r, pad_left_analog_x,
	pad_left_analog_y, pad_right_analog_x, pad_right_analog_y;

	// Set clock @ 16Mhz
	CPU_PRESCALE(0);

	// Disable JTAG
	bit_set(MCUCR, 1 << JTD);
	bit_set(MCUCR, 1 << JTD);

	// Setup pins
	setup_pins();

	// Init XBOX pad emulation
	xbox_init(true);

	// Pins polling and gamepad status updates
	for (;;) {
		xbox_reset_watchdog();

		pad_up = !bit_check(PINC, 7);
		pad_down = !bit_check(PINB, 2);
		pad_left = !bit_check(PINB, 0);
		pad_right = !bit_check(PIND, 3);
		pad_y = !bit_check(PIND, 2);
		pad_b = !bit_check(PIND, 1);
		pad_x = !bit_check(PIND, 0);
		pad_a = !bit_check(PIND, 4);
		pad_black =  !bit_check(PINC, 6);
		pad_white =  !bit_check(PIND, 7);
		pad_start =  !bit_check(PINE, 6);
		pad_select =  !bit_check(PINB, 4);
		pad_l3 =  !bit_check(PINB, 5);
		pad_r3 =  !bit_check(PINB, 6);
		pad_l = !bit_check(PINB, 7);
		pad_r = !bit_check(PIND, 6);

		pad_left_analog_x = pad_left_analog_y = pad_right_analog_x = pad_right_analog_y = 0x7F;

		if(!bit_check(PINB, 1)) {
			pad_left_analog_x = 0x00;
		} else if(!bit_check(PINB, 3)) {
			pad_left_analog_x = 0xFF;
		}

		if(!bit_check(PINF, 0)) {
			pad_left_analog_y = 0x00;
		} else if(!bit_check(PINF, 1)) {
			pad_left_analog_y = 0xFF;
		}

		if(!bit_check(PINF, 4)) {
			pad_right_analog_x = 0x00;
		} else if(!bit_check(PINF, 5)) {
			pad_right_analog_x = 0xFF;
		}

		if(!bit_check(PINF, 6)) {
			pad_right_analog_y = 0x00;
		} else if(!bit_check(PINF, 7)) {
			pad_right_analog_y = 0xFF;
		}

		pad_up    ? bit_set(gamepad_state.digital_buttons_1, XBOX_DPAD_UP)    : bit_clear(gamepad_state.digital_buttons_1, XBOX_DPAD_UP);
		pad_down  ? bit_set(gamepad_state.digital_buttons_1, XBOX_DPAD_DOWN)  : bit_clear(gamepad_state.digital_buttons_1, XBOX_DPAD_DOWN);
		pad_left  ? bit_set(gamepad_state.digital_buttons_1, XBOX_DPAD_LEFT)  : bit_clear(gamepad_state.digital_buttons_1, XBOX_DPAD_LEFT);
		pad_right ? bit_set(gamepad_state.digital_buttons_1, XBOX_DPAD_RIGHT) : bit_clear(gamepad_state.digital_buttons_1, XBOX_DPAD_RIGHT);

		pad_start  ? bit_set(gamepad_state.digital_buttons_1, XBOX_START)       : bit_clear(gamepad_state.digital_buttons_1, XBOX_START);
		pad_select ? bit_set(gamepad_state.digital_buttons_1, XBOX_BACK)        : bit_clear(gamepad_state.digital_buttons_1, XBOX_BACK);
		pad_l3     ? bit_set(gamepad_state.digital_buttons_1, XBOX_LEFT_STICK)  : bit_clear(gamepad_state.digital_buttons_1, XBOX_LEFT_STICK);
		pad_r3     ? bit_set(gamepad_state.digital_buttons_1, XBOX_RIGHT_STICK) : bit_clear(gamepad_state.digital_buttons_1, XBOX_RIGHT_STICK);

		pad_a ? bit_set(gamepad_state.digital_buttons_2, XBOX_A)    : bit_clear(gamepad_state.digital_buttons_2, XBOX_A);
		pad_b ? bit_set(gamepad_state.digital_buttons_2, XBOX_B)  : bit_clear(gamepad_state.digital_buttons_2, XBOX_B);
		pad_x ? bit_set(gamepad_state.digital_buttons_2, XBOX_X)  : bit_clear(gamepad_state.digital_buttons_2, XBOX_X);
		pad_y ? bit_set(gamepad_state.digital_buttons_2, XBOX_Y) : bit_clear(gamepad_state.digital_buttons_2, XBOX_Y);

		pad_black ? bit_set(gamepad_state.digital_buttons_2, XBOX_LB)    : bit_clear(gamepad_state.digital_buttons_2, XBOX_LB);
		pad_white ? bit_set(gamepad_state.digital_buttons_2, XBOX_RB)    : bit_clear(gamepad_state.digital_buttons_2, XBOX_RB);

		if(pad_start && pad_select) {
			bit_set(gamepad_state.digital_buttons_2, XBOX_HOME);
		} else {
			bit_clear(gamepad_state.digital_buttons_2, XBOX_HOME);
		}

		gamepad_state.l_x = pad_left_analog_x * 257 + -32768;
		gamepad_state.l_y = pad_left_analog_y * -257 + 32767;
		gamepad_state.r_x = pad_right_analog_x * 257 + -32768;
		gamepad_state.r_y = pad_right_analog_y * -257 + 32767;

		gamepad_state.lt = pad_l * 0xFF;
		gamepad_state.rt = pad_r * 0xFF;

		xbox_send_pad_state();
	}
}
Example #13
0
int main (void) 
{
  int ch = 0;
  int i = 0;
  
  // are we ready to relay scribbler output
  // by default we wait until we get a scribbler request from
  // the user before relaying scribbler messages via bluetooth
  int relay_scrib_messages = 0;

  // is the current command a scribbler command
  int scribbler_cmd = 0;

  // are we processing a get_info packet
  int  get_info = 0;


  // setup the directionality of the pins
  setup_pins();
  
  // setup the PLL - 60 MHz
  setup_pll();

  // setup the instruction prefetch
  setup_mam();
  
  // give some time to let everything settle
  msleep(100);
  
  // The scribbler UART(0) is 38400 8N1
  uart0Init(B38400, UART_8N1, UART_FIFO_8); // setup UART 0
  
  // The CM-5 UART (0) is 57600 8N1
  //  uart0Init(B57600, UART_8N1, UART_FIFO_8); // setup UART 0
  // The Robonova UART (0) is 9600 or 115200 8N1
  //  uart0Init(B115200, UART_8N1, UART_FIFO_8); // setup UART 0

  
  // The bluetooth UART(1)
#if (VERSION_OF_BOARD == 3)
  uart1Init(B57600, UART_8N1, UART_FIFO_8); // setup UART 1
#else
  uart1Init(B460800, UART_8N1, UART_FIFO_8); // setup UART 1
#endif
  
  // synchronize with autobaud
  for (i = 1; i < 512; i++)
    {
      uart1Putch(i);
    }  

  // Set up VIC and enable interrupts
  vic_init();
  
  //set timer0 prescalaer to around 1 usec
  TIMER0_PR = 0x39;
  
  // Start timer0 for benchmarking
  TIMER0_TCR = 1;

  // Set up the transmit queue
  uart1_queue_init();

  // setup interrupts for receiving IR messages
  ir_rx_init();

  // set back configurable LED off
  set_led(0);

  // set front LED off
  led_off();

  // turn IR power to 135
  set_ir(135);

  // set external bluetooth to max; internal amplifier is throttled via pskeys
  set_bluetooth(255);
  
  // slow camera down 
  write_camera(OV_CLKRC, 1);
  
  // turn off auto white balance
  //write_camera(OV_COMA, 0x14 & ~(1<<2));
  
  // turn off auto gain and auto exposure
  //write_camera(OV_COMB, 0xA3 & ~(0x3));

  //flush out any old characters
  for (i = 0; i < 200; i++)  
    {
      getch();
    }
  

  /* read in scribbler orientation 
   *
   * - scribbler forward is stored as 0xDF in serial memory to avoid
   *   coincidental settings
   */

  read_mem(0, 0, &scribbler_orientation, 1);  
  if (scribbler_orientation == 0xDF)
    {
      scribbler_orientation = 0;
    }
  else
    {
      scribbler_orientation = 1;
    }
  
  while (1)
    {
      //led_on();
      ch = getch();     
      //led_off();
     	  
      // body of the firmware - process the byte code sent over bluetooth
      if (ch != -1)
	{
	  cur_rqst[rqst_idx] = ch;

	  // process the cmd if its intended for the fluke
	  if (rqst_idx == 0)
	    {
	      scribbler_cmd = 0; 	      
	      if      (ch == SET_WINDOW)           serve_set_window();
	      else if (ch == SET_RLE)              serve_set_rle();
	      else if (ch == GET_RLE)	           serve_rle();
	      else if (ch == GET_BLOB)		   serve_blob();
	      else if (ch == GET_BLOB_WINDOW)	   serve_blob_window();
	      else if (ch == GET_IMAGE)		   serve_image();
	      else if (ch == GET_WINDOW)	   serve_image_window();
	      else if (ch == GET_WINDOW_LIGHT)	   serve_image_window_sum();
	      else if (ch == GET_BATTERY)	   serve_battery();
	      else if (ch == SET_DONGLE_LED_ON)	   led_on();
	      else if (ch == SET_DONGLE_LED_OFF)   led_off();
	      else if (ch == SET_DIMMER_LED)	   serve_back_led();
	      else if (ch == SET_DONGLE_IR)	   serve_ir_power();
	      else if (ch == SET_RESET_SCRIBBLER)  serve_reset_scribbler();
	      else if (ch == SET_RESET_SCRIBBLER2) serve_reset_scribbler2();
	      else if (ch == GET_SERIAL_MEM)	   serve_get_serial_mem();
	      else if (ch == SET_SERIAL_MEM)	   serve_set_serial_mem();
	      else if (ch == SET_SERIAL_ERASE)	   serve_erase_serial_mem();
	      else if (ch == GET_DONGLE_R_IR)	   serve_ir(0x1);
	      else if (ch == GET_DONGLE_L_IR)	   serve_ir(0x2);
	      else if (ch == GET_DONGLE_C_IR)	   serve_ir(0x4);
	      else if (ch == SET_SCRIB_PROGRAM)	   serve_set_scrib_program();
	      else if (ch == SET_SCRIB_BATCH)	   serve_set_scrib_program_batch();
	      else if (ch == GET_SCRIB_PROGRAM)	   serve_get_scrib_program();
	      else if (ch == SET_START_PROGRAM)	   serve_start_scrib_program();
	      else if (ch == SET_START_PROGRAM2)   serve_start_scrib2_program();
	      else if (ch == SET_UART0)		   serve_set_uart0();
	      else if (ch == SET_PASS_BYTE)	   serve_send_byte();
	      else if (ch == SET_PASSTHROUGH)	   serve_set_passthrough();
	      else if (ch == SET_PASS_N_BYTES)	   serve_set_pass_n_bytes();
	      else if (ch == SET_PASSTHROUGH_ON)   relay_scrib_messages = 1;
	      else if (ch == SET_PASSTHROUGH_OFF)  relay_scrib_messages = 0;
	      else if (ch == GET_PASS_N_BYTES)	   serve_get_pass_n_bytes();
	      else if (ch == GET_PASS_BYTES_UNTIL) serve_get_pass_bytes_until();
	      else if (ch == SET_FORWARDNESS)	   serve_set_forwardness();
	      else if (ch == SET_WHITE_BALANCE)	   serve_set_wb();
	      else if (ch == SET_NO_WHITE_BALANCE) serve_unset_wb();
	      else if (ch == GET_CAM_PARAM)	   serve_read_camera();	      
	      else if (ch == SET_CAM_PARAM)	   serve_write_camera();
	      else if (ch == SAVE_EEPROM)	   serve_save_eeprom();
	      else if (ch == RESTORE_EEPROM)	   serve_restore_eeprom(); 
	      else if (ch == UPDATE_FIRMWARE)	   serve_update_firmware();
	      else if (ch == WATCHDOG_RESET)	   serve_fluke_reset();
	      else if (ch == GET_JPEG_GRAY_HEADER) serve_jpeg_gray_header();
	      else if (ch == GET_JPEG_COLOR_HEADER)serve_jpeg_color_header();
	      else if (ch == GET_JPEG_GRAY_SCAN)   serve_jpeg_gray_scan();
	      else if (ch == GET_JPEG_COLOR_SCAN)  serve_jpeg_color_scan();
	      else if (ch == GET_VERSION)          serve_version();
	      else if (ch == GET_IR_MESSAGE)       serve_get_ir_message();
	      else if (ch == SEND_IR_MESSAGE)      serve_send_ir_message();
	      else if (ch == SET_IR_EMITTERS)      serve_set_ir_emitters();
	      else if (ch == GET_ROBOT_ID)         serve_identify_robot();
	      else scribbler_cmd = 1;
	    }
	  
	  if (scribbler_cmd)	    
	    {	      
	      // Flush if this is the first time we have  talked to the scribbler 
	      if (relay_scrib_messages == 0)
		{
		  for (i = 0; i < 100; i++)
		    {
		      uart0Getch();
		    }
		}

	      relay_scrib_messages = 1;
	      
	      // we just got a GET_INFO request; pass it on to the scribbler
	      if ((rqst_idx == 0) && (ch == GET_INFO))
		{
		  get_info = 1;
		}
	      // scribbler should now have the whole GET_INFO request
	      else if ((rqst_idx == 8) && get_info == 1)
		{
		  get_info = 2;
		}
	      
	      // flip motors forward to backwards
	      if ((cur_rqst[0] == SET_MOTORS) && scribbler_orientation && rqst_idx < 3)
		{
		  // now send the packet with left/right swapped
		  if ((rqst_idx == 2))
		    {
		      uart0Putch(cur_rqst[0]);
		      //msleep(10);
		      
		      uart0Putch(200-cur_rqst[2]);
		      //msleep(10);
		      
		      uart0Putch(200-cur_rqst[1]);
		    }
		}
	      // pass on character to the scribbler
	      else
		{
		  uart0Putch(ch);
		}
	     
	      // take care of scribbler packet indexing
	      rqst_idx ++;
	      if (rqst_idx > 8)
		{
		  rqst_idx = 0;
		}
	    }
	} /* end if it received a request */
      
      /*
       * if we are talking to the scribbler see if it has anything
       * for us to relay over the bluetooth link
       */
      if (relay_scrib_messages)
	{
	  ch = uart0Getch();
	  if (ch != -1)
	    {
	      // Process GET_INFO request we got the last character
	      if (get_info > 500 && ch == 0x0A)
		{
		  get_info = 0;
		}

	      // Process GET_INFO we just sent our GET_INFO string
	      if (get_info >= 500 && get_info < 50000)
		{
		  putch(',');
		  get_info = 50000;
		}
	      putch(ch);	      
	    }
	}
      
      
      /* 
       * send our GET_INFO information 
       *
       * the GET_INFO handling is pretty hideous to preserve backward
       * compatibility with other bluetooth serial and serial links.
       */
      
      if (get_info > 1)
	{
	  get_info ++;
	  
	  if (get_info == 400)
	    {
	      putstr(VERSION);
	      get_info = 500;
	    }
	  
	  if (get_info == 100000)
	    {
	      putch(0x0A);
	      get_info = 0;
	    }	  	  
	}

      update_low_battery_light();
    }
}
Example #14
0
int main(void) {
	m_clockdivide(0);
	m_bus_init();
	setup_timer_3();
	m_rf_open(chan,TX_add,p_length);
	setup_pins();
	if (debug_ADC){ setupUSB();}

	long VLeftSum=0;
	long HLeftSum=0;
	long VRightSum=0;
	long HRightSum=0;
//	int  VLeftSum=0;
//	int  HLeftSum=0;
//	int  VRightSum=0;
//	int  HRightSum=0;


	int cnt_RB = 0;
	int cnt_LB = 0;


	/////////////////////////////////////////////////////////////////////////////////
	//
	// give me a half second to go from switching on the m2 to holding the config buttons.
	//
	//////////////////////////////////////////////////////////////////////////////////

	m_green(1);
	int timer_3_cnt=0;
	int Calibration = 50;
	for (timer_3_cnt=0 ; timer_3_cnt< Calibration ; ){
		if(check(TIFR3,OCF3A)){		 ///// timer 3 runs at 100Hz
			set(TIFR3,OCF3A );
			timer_3_cnt++;
		}
	}

	m_red(ON);

	for (timer_3_cnt=0 ; timer_3_cnt<Calibration ; ){
		if(check(TIFR3,OCF3A)){		 ///// timer 3 runs at 100Hz
			set(TIFR3,OCF3A );
			timer_3_cnt++;


		update_ADC(0,0,0,0);

		check_buttons();
		if (send_buffer[1]==1){ cnt_RB++ ;}
		if (send_buffer[0]==1){ cnt_LB++ ;}

		VLeftSum  += *(int*)(&send_buffer[2]);
		HLeftSum  += *(int*)(&send_buffer[6]);
		VRightSum += *(int*)(&send_buffer[4]);
		HRightSum += *(int*)(&send_buffer[8]);

//		if (debug_ADC){while(!m_usb_rx_available()); m_usb_rx_flush();}
		if (debug_ADC){debug_ADC_sums( timer_3_cnt,  VRightSum, VLeftSum,  HLeftSum,  HRightSum );}
		}
	}

	// if (debug_ADC){while(!m_usb_rx_available()); m_usb_rx_flush();}

	int VLeftOffset = (int)(VLeftSum/Calibration);
	int HLeftOffset = (int)(HLeftSum/Calibration);
	int VRightOffset= (int)(VRightSum/Calibration);
	int HRightOffset= (int)(HRightSum/Calibration);

	int VLeftOffset = 512;//(int)(VLeftSum/Calibration);
	int HLeftOffset = 512;//(int)(HLeftSum/Calibration);
	int VRightOffset= 512;//(int)(VRightSum/Calibration);
	int HRightOffset= 512;//(int)(HRightSum/Calibration);

	bool L_bump  = ( cnt_LB > Calibration/2 );
	bool R_bump = ( cnt_RB > Calibration/2 );


	if (debug_ADC){while(!m_usb_rx_available()); m_usb_rx_flush();}
	if (debug_ADC){debug_ADC_vals( Calibration,   VRightSum, VLeftSum,  HLeftSum,  HRightSum );}
	m_green(0);
	m_red(OFF);

	set_drive_mode( L_bump , R_bump );

	while (1){
		check_buttons();
		update_ADC( VLeftOffset,HLeftOffset,VRightOffset,HRightOffset );
		TX_comm();
		if (debug_ADC){deal_with_new();}
	}
}