/**************************************************************** * Author: Ittichote Chuckpaiwong * Date: August 2, 2009 * Filename: ex8_1.pde * CPU: AVR ATmega168 * Oscillator: HT 19.6608MHz * Compiler: Arduino * Hardware Config: * - Connect the IDC10/14 connector between port Digital[0..7] and "LCD" connector * - Connect the IDC10/14 connector between port Digital[8..13] and "DC-MOTOR" connector * - Push the switch "CTRL/TEST" to CTRL * Function: * - This program rotates the motor until the sensor (digital 8) reads 1000 counts (500 rev) * - Note that this program uses polling for simplicity, however, some variables cannot * be updated continuously. A more practical approach is to use interrupt. *****************************************************************/ #define OPTO1 8 // Input from opto sensor #1 #define OPTO2 9 // Input from opto sensor #2 #define DIR1 12 // Output #1 to set motor direction #define DIR2 13 // Output #2 to set motor direction #include // Include LCD function library // LiquidCrystal display with: // RS on pin 0 // RW on pin 1 // ENABLE on pin 2 // DATA: D0, D1, D2, D3 on pins 4, 5, 6, 7 LiquidCrystal lcd(0, 1, 2, 4, 5, 6, 7); char text[16]; int count=0; void setup() { pinMode(OPTO1, INPUT); // Set port as input pinMode(OPTO2, INPUT); // Set port as input pinMode(DIR1, OUTPUT); // Set port as output pinMode(DIR2, OUTPUT); // Set port as output // Enable pull-up resistors on input ports digitalWrite(OPTO1, HIGH); digitalWrite(OPTO2, HIGH); // Set direction of rotation digitalWrite(DIR1, LOW); digitalWrite(DIR2, HIGH); lcd.clear(); // Clear the LCD display } void loop() { lcd.setCursor(0,0); // Move the cursor to home position lcd.print("CNT:"); // Print text to LCD lcd.print(count, DEC); // Print number to LCD lcd.print(",OPTO:XX"); // Print text to LCD while (digitalRead(OPTO1) == HIGH); // Wait for the sensor to be hit count++; // Count up while (digitalRead(OPTO1) == LOW); // Wait for the sensor to be clear if (count >= 1000) digitalWrite(DIR2, LOW); // Stop the motor }