mirror of
				https://github.com/KevinMidboe/Arduino.git
				synced 2025-10-29 17:40:11 +00:00 
			
		
		
		
	Init commit with many years of arduino sketches and projects. I dont know if the esp8266 includes much, but there are also libraries. I hope they dont have crazy automatic versioning through the Arduino IDE.
This commit is contained in:
		
							
								
								
									
										63
									
								
								Projects/_3DInterface/_3DInterface.ino
									
									
									
									
									
										Executable file
									
								
							
							
						
						
									
										63
									
								
								Projects/_3DInterface/_3DInterface.ino
									
									
									
									
									
										Executable file
									
								
							@@ -0,0 +1,63 @@
 | 
			
		||||
//
 | 
			
		||||
// By Kyle McDonald
 | 
			
		||||
// From the instructables project at:
 | 
			
		||||
// http://www.instructables.com/id/DIY-3D-Controller/
 | 
			
		||||
 | 
			
		||||
#define resolution 8
 | 
			
		||||
#define mains 50 // 60: north america, japan; 50: most other places
 | 
			
		||||
 | 
			
		||||
#define refresh 2 * 1000000 / mains
 | 
			
		||||
 | 
			
		||||
void setup() {
 | 
			
		||||
  Serial.begin(115200);
 | 
			
		||||
 | 
			
		||||
  // unused pins are fairly insignificant,
 | 
			
		||||
  // but pulled low to reduce unknown variables
 | 
			
		||||
  for(int i = 2; i < 14; i++) {
 | 
			
		||||
    pinMode(i, OUTPUT);
 | 
			
		||||
    digitalWrite(i, LOW);
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  for(int i = 8; i < 11; i++)
 | 
			
		||||
    pinMode(i, INPUT);
 | 
			
		||||
 | 
			
		||||
  startTimer();
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void loop() {  
 | 
			
		||||
  Serial.print(time(8, B00000001), DEC);
 | 
			
		||||
  Serial.print(" ");
 | 
			
		||||
  Serial.print(time(9, B00000010), DEC);
 | 
			
		||||
  Serial.print(" ");
 | 
			
		||||
  Serial.println(time(10, B00000100), DEC);
 | 
			
		||||
 | 
			
		||||
} 
 | 
			
		||||
 | 
			
		||||
long time(int pin, byte mask) {
 | 
			
		||||
  unsigned long count = 0, total = 0;
 | 
			
		||||
  while(checkTimer() < refresh) {
 | 
			
		||||
    // pinMode is about 6 times slower than assigning
 | 
			
		||||
    // DDRB directly, but that pause is important
 | 
			
		||||
    pinMode(pin, OUTPUT);
 | 
			
		||||
    PORTB = 0;
 | 
			
		||||
    pinMode(pin, INPUT);
 | 
			
		||||
    while((PINB & mask) == 0)
 | 
			
		||||
      count++;
 | 
			
		||||
    total++;
 | 
			
		||||
  }
 | 
			
		||||
  startTimer();
 | 
			
		||||
  return (count << resolution) / total;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
extern volatile unsigned long timer0_overflow_count;
 | 
			
		||||
 | 
			
		||||
void startTimer() {
 | 
			
		||||
  timer0_overflow_count = 0;
 | 
			
		||||
  TCNT0 = 0;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
unsigned long checkTimer() {
 | 
			
		||||
  return ((timer0_overflow_count << 8) + TCNT0) << 2;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
		Reference in New Issue
	
	Block a user