Virtuell Cykling med VLC media player och Arduino Leonardo

By Hans Näsström Stockholm
This is a Virtual Cycling VIDEO-CONTROLER connected to reedswith on training/spinning -bike.
The program can control video playback speed on a VLC media player by
emulating Hot-key´s for the player, works only on ARDUINO LEONARDO systems.
It controlls speed from 40% to 200% speed and stops and pause if you stop pedaling.
For this to work properly ---
1: Start the video and set it in pause mode (Key U).
2: Start (connect) the ARDUINO LEONARDI and wait 10sec for it to setup.
3: And away you go, start pedaling!

Set VLC-Hotkeys as follows,---- Tools--Preferences--Hotkeys-----
[Pause/Play]: Dubble click, set and save both Hotkey and Global, set to: the letter u
[Slower (fine)]: Dubble click, set and save both Hotkey and Global, set to: numpad 4
[Faster (fine)]: Dubble click, set and save both Hotkey and Global, set to: numpad 6
Save these settings and close the player for it to take effect.
A large number of virtuel cycling videos can be downloaded from YouTube, this is my favorit;

Ladda ner den från denna adress, (notera dubbel ss före youtube).
[http://www.ssyoutube.com/watch?v=FTmPk8qpxQA&playnext=1&list=PLoV-bLQ4xWjLKiiecjbmvODo8Uwu0vmKr&feature=results_main]

You will need this hardware fixes:
Set a magnet on and a magnetic reed-switch by the trainer-bike´s wheel,
closing ONES every wheelturn, (Magnet north pole facing the reed switch).
Attached the reed-switch cables to Digital pin3 and Ground.
Set a pull up resistor 100kOhm between +5V and Digital pin3
This software is free to use but on your one risk.
Beware it will send keystrokes u,4,6 now and then.
*/

int videostep=1;
int steplevel=1;
int pause_run=0; // run = 1   pause = 0
int Rot = 0;  // Wheel rotation counter
int bike_wheel_rev = 0;
unsigned long time;
float cruise=61;      // ATTENTION!! Set this value to the number of wheelturns on YOUR bike during
                           // 4 sek and normal cruising speed (the video will go at 100% speed at this count)
void setup() 
{ 

  time=millis(); 
  while ((time+5000)>millis()); {  } // time for system to settle, setting up Com port etc
  
  Serial.begin(9600); 
  
  attachInterrupt(0, rpm, FALLING); // Set a pull up 10K resistor from 5V to Digital pin 3 (interupt 0).
  cli();		            // Disable interrupts for now

  // initialize control over the keyboard:
  Keyboard.begin();
   
 //Step down speed from 100% to 40%    
  int var = 0;
  while(var < 6){
  Keyboard.press('4'); delay(100); Keyboard.releaseAll();delay(100); 
  var++;
}

} 

void loop ()
{

  int Rot=0;  
  bike_wheel_rev = 0;	//Set bike_wheel counter to 0
  sei();		//Enables interrupts
  delay (4000);	        //count # bike wheel rev during 4 sec
  cli();		//Disable interrupts
  Rot = (bike_wheel_rev);   //Rot = revolutions during 4 sec

     if (Rot<20 && pause_run == 1)    {
     pause_run = 0; 
     Keyboard.press('u'); delay(100); Keyboard.releaseAll(); delay(100);
  }
  if (Rot>19 && pause_run == 0)    {
    pause_run = 1;   
    Keyboard.press('u'); delay(100); Keyboard.releaseAll(); delay(100);
  }
  if (Rot<(cruise*0.4))   { steplevel=1;}  // Serial.println("1-#");     
  if (Rot>(cruise*0.5))   { steplevel=2;}  // Serial.println("2-##");         
  if (Rot>(cruise*0.6))   { steplevel=3;}  // Serial.println("3-###"); wheel speed at 50% video speed
  if (Rot>(cruise*0.7))   { steplevel=4;}  // Serial.println("4-####");  
  if (Rot>(cruise*0.8))   { steplevel=5;}  // Serial.println("5-#####"); 
  if (Rot>(cruise*0.9))   { steplevel=6;}  // Serial.println("6-######");  
  if (Rot>cruise)         { steplevel=7;}   // Serial.println("7-#######");  Wheel speed at 100% video speed
  if (Rot>(cruise*1.1))   { steplevel=8;}  // Serial.println("8-########"); 
  if (Rot>(cruise*1.2))   { steplevel=9;}  // Serial.println("9-#########");  
  if (Rot>(cruise*1.3))   { steplevel=10;} // Serial.println("10-##########");
  if (Rot>(cruise*1.4))   { steplevel=11;} // Serial.println("11-###########");
  if (Rot>(cruise*1.5))   { steplevel=12;} // Serial.println("12-############");
  if (Rot>(cruise*1.6))   { steplevel=13;} // Serial.println("13-#############");
  if (Rot>(cruise*1.7))   { steplevel=14;} // Serial.println("14-##############");
  if (Rot>(cruise*1.8))   { steplevel=15;} // Serial.println("15-###############");
  if (Rot>(cruise*1.9))   { steplevel=16;} // Serial.println("16-################");
  if (Rot>(cruise*2))     { steplevel=17;} // Serial.println("16-################"); Wheel speed at 200% video speed

  while (videostep<steplevel && steplevel<17) // adjust video speed by stepping controler to the right, (step up speed)                      
  { 
    videostep = videostep +1; 
    Keyboard.press('6'); delay(100); Keyboard.releaseAll(); delay(100);
  }

  while (videostep>steplevel&&steplevel>0)// adjust video speed by stepping controler to the left, (step down speed)
  { 
    videostep = videostep -1; 
    Keyboard.press('4'); delay(100); Keyboard.releaseAll(); delay(100);
  }                          
   
}

void rpm ()      //This is the function that the interupt calls 
{ 
  bike_wheel_rev++; 
  delay(100);
}