1. iorodeo
  2. educational_software

Commits

jorodeo  committed 180ea00

Created new project for all educational software.

  • Participants
  • Branches default

Comments (0)

Files changed (10)

File lcd_photogate_v3/DisplayHandler.cpp

View file
  • Ignore whitespace
+#include "util/atomic.h"
+#include "DisplayHandler.h"
+
+DisplayHandler::DisplayHandler(Print &port) {
+    _lcd.setPort(port); 
+    for (int i=1; i<DISPLAY_TRACE_BUF_LEN; i++) {
+        _traceBuf[i] = 0;
+    }
+    _tracePlotPos = 0;
+    _traceClearPos = DISPLAY_CLEAR_OFFSET;
+}
+
+void DisplayHandler::init() {
+    _lcd.clearScreen();
+    setBrightness();
+}
+
+void DisplayHandler::setBrightness() {
+    // Read Potentiometer value and use value to set brightness
+    // of LCD.
+    int pot;
+    int brightness;
+    ATOMIC_BLOCK(ATOMIC_RESTORESTATE) {
+        pot = analogRead(DISPLAY_POT_AIN);
+    }
+    brightness = map(pot,0,1024,0,100);
+    _lcd.setBrightness(brightness);
+}
+
+void DisplayHandler::showSplashScreen() {
+    // Draw Splash Screen w/ IO Rodeo logo.
+    _lcd.drawBox(1,1,127,63,1);
+    _lcd.print("PENDULUM EXPERIMENT",8,18);
+    _lcd.print("IO RODEO INC.",28,28);
+    delay(DISPLAY_SPLASH_TIMEOUT);
+    _lcd.clearScreen();
+}
+
+void DisplayHandler::updatePeriodText(uint16_t period) {
+    // Update period text on lcd display.
+    char msg[20];
+    int sec;
+    int frac;
+    if (period == 0) {
+        // We don't have a valid period measurement, display empty values
+        sprintf(msg,"PERIOD: _.___ S  ");
+        _lcd.print(msg,2,3);
+    }
+    else {
+        // Get seconds and fractional seconds
+        sec = period/1000;
+        frac = period - sec*1000;
+        sprintf(msg,"PERIOD: %d.%03d S  ",sec,frac);
+        _lcd.print(msg,2,3);
+    }
+}
+
+void DisplayHandler::updateTracePlot(uint16_t value) {
+    // Update the streaming plot trace.
+    uint16_t displayValue;
+    static uint16_t displayValueLast=0;
+
+    // Adjust value for display
+    displayValue = 55 - map(value,0,1024,0,35);
+
+    if ((displayValue > 55) || (displayValue < 20)) {
+        displayValue = displayValueLast;
+    }
+    
+    // Clear old trace values
+    if (_traceClearPos == 0) {
+        _lcd.setPixel(_traceClearPos,_traceBuf[_traceClearPos],0);
+    }
+    else {
+        _lcd.drawLine(
+                _traceClearPos-1,
+                _traceBuf[_traceClearPos-1],
+                _traceClearPos,
+                _traceBuf[_traceClearPos],
+                0
+                );
+    }
+    // Clear artifacts  - not sure what causes these
+    _lcd.drawLine(_traceClearPos, 19, _traceClearPos, 56, 0); 
+
+    // Plot new trace values.
+    if (_tracePlotPos == 0) {
+        _lcd.setPixel(_tracePlotPos,displayValue,1);
+    }
+    else {
+        _lcd.drawLine(
+                _tracePlotPos-1,
+                displayValueLast,
+                _tracePlotPos,
+                displayValue,
+                1
+                );
+    }
+
+    // Save last value and add data to trace buffer.
+    displayValueLast = displayValue;
+    _traceBuf[_tracePlotPos] = displayValue;
+
+    // Update trace plot position 
+    _tracePlotPos += 1;
+    if (_tracePlotPos > 127) {
+        _tracePlotPos = 0;
+    }
+
+    // Update trace clear position
+    _traceClearPos += 1;
+    if (_traceClearPos > 127) {
+        _traceClearPos = 0;
+    }
+}

File lcd_photogate_v3/DisplayHandler.h

View file
  • Ignore whitespace
+#ifndef _DISPLAY_HANDLER_H_
+#define _DISPLAY_HANDLER_H_
+#include "WProgram.h"
+#include "SerialLCD.h"
+
+#define DISPLAY_POT_AIN A7
+#define DISPLAY_SPLASH_TIMEOUT 2000 
+#define DISPLAY_TRACE_BUF_LEN 128
+#define DISPLAY_CLEAR_OFFSET 15
+
+class DisplayHandler {
+    private:
+        SerialLCD _lcd;
+        uint8_t _tracePlotPos;
+        uint8_t _traceClearPos;
+        uint16_t _traceBuf[DISPLAY_TRACE_BUF_LEN];
+    public:
+        DisplayHandler(Print &port);
+        void init();
+        void setBrightness();
+        void showSplashScreen();
+        void updatePeriodText(uint16_t period);
+        void updateTracePlot(uint16_t value);
+};
+
+#endif

File lcd_photogate_v3/Sensor.cpp

View file
  • Ignore whitespace
+#include "Sensor.h"
+
+Sensor::Sensor() {
+    _state = HIGH;
+    _currValue = 1024;
+    _prevValue = 1024;
+    _eventIntCntr = 0;
+    _timeOutFlag = true;
+    _numEvents = 0;
+    _valueBufPos = 0;
+    pinMode(SENSOR_LED_PIN,OUTPUT);
+    digitalWrite(SENSOR_LED_PIN,LOW);
+    for (int i=0; i<SENSOR_BUF_SZ; i++) {
+        _valueBuf[i] = 1024;
+    }
+}
+
+void Sensor::update() {
+    int val; 
+    val = analogRead(SENSOR_AIN);
+
+    // Increment event interval counter and check for timeout
+    _eventIntCntr++;
+    if (_eventIntCntr > SENSOR_TIMEOUT_CNT) {
+        _numEvents = 0;
+        _timeOutFlag = true;
+    }
+
+    if (_state == HIGH) {
+        if (val < (SENSOR_THRESHOLD - SENSOR_HYSTERESIS)) {
+            high2lowEvent();
+        }
+    }
+    else {
+        if (val > (SENSOR_THRESHOLD + SENSOR_HYSTERESIS)) {
+            low2highEvent();
+        }
+    }
+    _prevValue = _currValue;
+    _currValue = val;
+    updateValueBuf(val);
+}
+
+void Sensor::high2lowEvent() {
+    // Handle high-to-low transition event
+    _state = LOW;
+    digitalWrite(SENSOR_LED_PIN,HIGH);
+    if (_timeOutFlag == false) {
+        // Record high to low transition event
+        if (_numEvents < 3) {
+            _eventBuf[_numEvents] = _eventIntCntr;
+        }
+        else {
+            _eventBuf[0] = _eventBuf[1];
+            _eventBuf[1] = _eventBuf[2];
+            _eventBuf[2] = _eventIntCntr;
+        }
+        _numEvents++;
+        if (_numEvents > 3) {
+            _numEvents == 3;
+        }
+    }
+    // Resent event interval counter and time out flag.
+    _eventIntCntr = 0;
+    _timeOutFlag = false;
+}
+
+void Sensor::low2highEvent() {
+    // Handle low-to-high transition event
+    _state = HIGH;
+    digitalWrite(SENSOR_LED_PIN,LOW);
+}
+
+uint16_t Sensor::getCurrValue() {
+    // Return the current sensor value.
+    return _currValue;
+}
+
+uint16_t Sensor::getPrevValue() {
+    // Return the previous sensor value.
+    return _prevValue;
+}
+
+uint16_t Sensor::getPeriod() {
+    // Calculate the period of the pendulum using every other high-to-low
+    // transision.
+    uint16_t period;
+    if (_numEvents < 3) {
+        period = 0;
+    }
+    else {
+        period = _eventBuf[2] + _eventBuf[1];
+    }
+    return period;
+}
+
+void Sensor::updateValueBuf(uint16_t value) {
+    _valueBuf[_valueBufPos] = value;
+    _valueBufPos++;
+    if (_valueBufPos >= SENSOR_BUF_SZ) {
+        _valueBufPos = 0;
+    }
+}
+
+
+uint16_t Sensor::getMinValueInBuf() {
+    uint16_t value;
+    uint16_t minValue = 1024;
+    for (int i=0; i<SENSOR_BUF_SZ; i++) {
+        value = _valueBuf[i];
+        if (value < minValue) {
+            minValue = value;
+        }
+    }
+    return minValue;
+}

File lcd_photogate_v3/Sensor.h

View file
  • Ignore whitespace
+#ifndef _SENSOR_H_
+#define _SENSOR_H_
+#include "WProgram.h"
+
+#define SENSOR_AIN A0
+#define SENSOR_THRESHOLD 500
+#define SENSOR_HYSTERESIS 20
+#define SENSOR_LED_PIN 10
+#define SENSOR_TIMEOUT_CNT 2000
+#define SENSOR_BUF_SZ 20
+
+class Sensor {
+    private:
+        uint8_t _state;
+        uint16_t _currValue;
+        uint16_t _prevValue;
+        uint16_t _eventIntCntr;
+        uint8_t _numEvents;
+        uint16_t _eventBuf[3];
+        uint16_t _valueBuf[SENSOR_BUF_SZ];
+        uint16_t _valueBufPos;
+        bool _timeOutFlag;  
+        void high2lowEvent();
+        void low2highEvent();
+        void updateValueBuf(uint16_t value);
+        
+    public:
+        Sensor();
+        void update();
+        uint8_t getState();
+        uint16_t getCurrValue();
+        uint16_t getPrevValue();
+        uint16_t getPeriod();
+        uint16_t getMinValueInBuf();
+};
+#endif

File lcd_photogate_v3/SerialLCD.cpp

View file
  • Ignore whitespace
+#include "SerialLCD.h"
+
+SerialLCD::SerialLCD() {
+}
+
+SerialLCD::SerialLCD(Print &port) {
+    setPort(port);
+}
+
+void SerialLCD::setPort(Print &port) {
+    _portPtr = &port;
+}
+
+void SerialLCD::clearScreen(void) {
+    // Clear the LCD display sets x and y offsets to 0,0
+    _portPtr -> print(0x7C,BYTE);
+    _portPtr -> print(0x00,BYTE);
+}
+
+void SerialLCD::print(char *data) {
+    // Print data to the LCD
+    _portPtr -> print(data);
+}
+
+void SerialLCD::print(char *data, byte x, byte y) {
+    // Print data at position given by x and y.
+    setPos(x,y);
+    print(data);
+}
+
+void SerialLCD::setBrightness(byte value) {
+    // Sets the LCD backlight brightness - adjusts duty cycle.
+    // value should be between 0 and 100.
+    _portPtr -> print(0x7C,BYTE);
+    _portPtr -> print(0x02,BYTE);
+    _portPtr -> print(value,BYTE);
+}
+
+void SerialLCD::setX(byte x) {
+    // Set the x coordinate for where the next character will be drawn
+    _portPtr -> print(0x7C,BYTE);
+    _portPtr -> print(0x18,BYTE);
+    _portPtr -> print(x,BYTE);
+}
+
+void SerialLCD::setY(byte y) {
+    // Set the y coordinate for where the next character will be drawn
+    _portPtr -> print(0x7C,BYTE);
+    _portPtr -> print(0x19,BYTE);
+    _portPtr -> print(y,BYTE);
+}
+
+void SerialLCD::setPos(byte x, byte y) {
+    // Set x and y coordinates for where next character will be drawn
+    setX(x);
+    setY(y);
+}
+
+void SerialLCD::setPixel(byte x, byte y, byte state) {
+    // Set pixel and coordinate x,y to value given by state.
+    _portPtr -> print(0x7C,BYTE);
+    _portPtr -> print(0x10,BYTE);
+    _portPtr -> print(x,BYTE);
+    _portPtr -> print(y,BYTE);
+    _portPtr -> print(state,BYTE);
+}
+
+void SerialLCD::drawLine(
+        byte startX, 
+        byte startY, 
+        byte endX, 
+        byte endY, 
+        byte state
+        )
+{
+    _portPtr -> print(0x7C,BYTE);
+    _portPtr -> print(0x0C,BYTE);
+    _portPtr -> print(startX,BYTE);
+    _portPtr -> print(startY,BYTE);
+    _portPtr -> print(endX,BYTE);
+    _portPtr -> print(endY,BYTE);
+    _portPtr -> print(state,BYTE);
+}
+
+void SerialLCD::drawBox(
+        byte topLeftX, 
+        byte topLeftY, 
+        byte bottomRightX, 
+        byte bottomRightY, 
+        byte state
+        )
+{
+    _portPtr -> print(0x7C,BYTE);
+    _portPtr -> print(0x0F,BYTE);
+    _portPtr -> print(topLeftX,BYTE);
+    _portPtr -> print(topLeftY,BYTE);
+    _portPtr -> print(bottomRightX,BYTE);
+    _portPtr -> print(bottomRightY,BYTE);
+    _portPtr -> print(state,BYTE);
+}
+
+void SerialLCD::drawCircle(
+        byte startX, 
+        byte startY, 
+        byte radius, 
+        byte state
+        )
+{
+    _portPtr -> print(0x7C,BYTE);
+    _portPtr -> print(0x03,BYTE);
+    _portPtr -> print(startX,BYTE);
+    _portPtr -> print(startY,BYTE);
+    _portPtr -> print(radius,BYTE);
+    _portPtr -> print(state,BYTE);
+}
+
+void SerialLCD::drawFilledBox(
+        byte topLeftX, 
+        byte topLeftY, 
+        byte bottomRightX, 
+        byte bottomRightY, 
+        byte state
+        )
+{
+    _portPtr -> print(0x7C,BYTE);
+    _portPtr -> print(0x12,BYTE);
+    _portPtr -> print(topLeftX,BYTE);
+    _portPtr -> print(topLeftY,BYTE);
+    _portPtr -> print(bottomRightX,BYTE);
+    _portPtr -> print(bottomRightY,BYTE);
+    _portPtr -> print(state,BYTE);
+}
+
+void SerialLCD::toggleAuxFont() {
+    _portPtr -> print(0x7C,BYTE);
+    _portPtr -> print(0x08,BYTE);
+}
+
+void SerialLCD::toggleRevMode() {
+    _portPtr -> print(0x7C,BYTE);
+    _portPtr -> print(0x14,BYTE);
+}
+
+void SerialLCD::toggleSplash() {
+    _portPtr -> print(0x7C,BYTE);
+    _portPtr -> print(0x15,BYTE);
+}
+

File lcd_photogate_v3/SerialLCD.h

View file
  • Ignore whitespace
+#ifndef _SERIAL_LCD_H_
+#define _SERIAL_LCD_H_
+#include "WProgram.h"
+
+
+class SerialLCD {
+    private:
+        Print *_portPtr;
+    public:
+        SerialLCD();
+        SerialLCD(Print &port);
+        void setPort(Print &port);
+        void clearScreen(void);
+        void setBrightness(byte value);
+        void print(char *data);
+        void print(char *data, byte x, byte y);
+        void setX(byte x);
+        void setY(byte y);
+        void setPos(byte x, byte y);
+        void setPixel(byte x, byte y, byte state);
+        void drawLine(byte startX, byte startY, byte endX, byte endY, byte state);
+        void drawBox(byte topLeftX, byte topLeftY, byte bottomRightX, byte bottomRightY, byte state);
+        void drawCircle(byte startX, byte startY, byte radius, byte state);
+        void drawFilledBox(byte topLeftX, byte topLeftY, byte bottomRightX, byte bottomRightY, byte state);
+        void toggleAuxFont();
+        void toggleRevMode(); 
+        void toggleSplash();
+};
+
+#endif

File lcd_photogate_v3/lcd_photogate.h

View file
  • Ignore whitespace
+#ifndef _LCD_PHOTOGATE_H_
+#define _LCD_PHOTOGATE_H_
+
+#define LOOP_DELAY 25
+
+// Timer parameters - 1kHz overflow on timer 2
+#define TIMER_TCCR2A 0b00000011 
+#define TIMER_TCCR2B 0b00001100  
+#define TIMER_OCR2A 249 
+
+#endif

File lcd_photogate_v3/lcd_photogate_v3.pde

View file
  • Ignore whitespace
+#include <NewSoftSerial.h>
+#include "lcd_photogate.h"
+#include "SerialLCD.h"
+#include "Sensor.h"
+#include "DisplayHandler.h"
+#include "util/atomic.h"
+
+NewSoftSerial softSerial(8,7);
+DisplayHandler display(softSerial);
+Sensor photogate;
+
+void setup() {
+    softSerial.begin(115200);
+    delay(2500);
+    display.init();
+    display.showSplashScreen();
+    timerInit();
+}
+
+void loop() {
+    uint16_t period;
+    uint16_t currValue;
+
+    ATOMIC_BLOCK(ATOMIC_RESTORESTATE) {
+        //currValue = photogate.getCurrValue();
+        currValue = photogate.getMinValueInBuf();
+        period = photogate.getPeriod();
+    }
+
+    // Update display
+    display.setBrightness();
+    display.updatePeriodText(period);
+    display.updateTracePlot(currValue);
+    delay(LOOP_DELAY);
+}
+
+ISR(TIMER2_OVF_vect) {
+    // Timer 2 overflow interrupt service routine. Take photogate 
+    // sensor readings.
+    photogate.update();
+}
+
+void timerInit(void) {
+    // Initialize timer 2 - 1kHz overflow for reading photogate 
+    TCCR2A = TIMER_TCCR2A;
+    TCCR2B = TIMER_TCCR2B;
+    OCR2A = TIMER_OCR2A;
+    // Timer 2 overflow interrupt enable 
+    TIMSK2 |= (1<<TOIE2) | (0<<OCIE2A);
+    TCNT2 = 0;
+}
+

File photogate_period/photogate_period.pde

View file
  • Ignore whitespace
+/* photogate_period
+ 
+This program calculates the period of a pendulum passing through a photogate timer. 
+Period is calculated from the time between every other high to low transition time. 
+Measurements are returned to the Serial monitor in seconds.
+Created : Sept. 2012 by IO Rodeo                                                                                             
+                                                                                                             
+*/
+
+
+#include <Streaming.h>
+
+#define LED_PIN 10
+#define ANALOG_PIN 0
+#define THRESHOLD 500
+#define SENSOR_HIGH 0
+#define SENSOR_LOW 1
+
+
+
+void setup() {
+    Serial.begin(115200);
+    pinMode(LED_PIN,OUTPUT);
+}
+
+void loop() {
+    int sensorVal;
+    
+    unsigned long timeLow2High;
+    unsigned long period;
+    static int state;
+    static bool isFirst = true;  
+    static unsigned long timeHigh2Low[3];          // Create an array of 3 values to store timepoints.
+    static unsigned int countHigh2Low = 0;
+    static unsigned int countLow2High = 0;
+    
+    sensorVal = analogRead(ANALOG_PIN);             // Voltage measurements from the photogate 
+    
+    // Initialize and assign sensor state. Runs only once, when the program is first run. 
+    if (isFirst) {
+      if (sensorVal > THRESHOLD) {
+        state = SENSOR_HIGH;
+      }
+      
+      else {
+        state = SENSOR_LOW;
+      }
+      timeHigh2Low[0] = micros();
+      isFirst = false;
+    }
+
+
+    // Take action based on sensor state.
+    if (state == SENSOR_HIGH) {                 
+     
+      if (sensorVal <= THRESHOLD) {                 // sensor is in high but value is below threshold ! Object is moving into beam.
+        state = SENSOR_LOW;                         // switch states
+        timeHigh2Low[countHigh2Low] = micros();     //  take time measurement
+        countHigh2Low = ++ countHigh2Low;           // increment count by one
+      
+      }
+    }
+    
+    if (state == SENSOR_LOW) {                 
+     
+      if (sensorVal >= THRESHOLD) {                 // sensor is in low but value is above threshold ! Object is moving into beam.
+        state = SENSOR_HIGH;                        // switch states
+       
+      }
+    }
+    
+    
+    // Calculate period from the counts
+    
+    if (countHigh2Low == 3) {
+      countHigh2Low = 0;
+      period = timeHigh2Low[2] - timeHigh2Low[0];
+      Serial << "period: " << float(period)/1000000.0 << " seconds" << endl;       // Print period to Serial Monitor in seconds.
+      
+    }
+     
+  }
+    
+    

File photogate_velocity/photogate_velocity.pde

View file
  • Ignore whitespace
+/* photogate_velocity
+ 
+This program calculates the velocity of an object passing through a photogate timer. 
+Velocity is calculated from the total time the object blocks the sensor and the objects width.
+Measurements are returned to the Serial Monitor in 
+Created : Sept. 20102 by IO Rodeo                                                                                             
+                                                                                                               
+*/
+
+
+#include <Streaming.h>
+
+#define LED_PIN 10
+#define ANALOG_PIN 0
+#define THRESHOLD 500
+#define SENSOR_HIGH 0
+#define SENSOR_LOW 1
+
+
+void setup() {
+    Serial.begin(115200);
+    pinMode(LED_PIN,OUTPUT);
+}
+
+void loop() {
+    int sensorVal;
+    int pendulumWidth = 16;                         // Width of the pendulum in millimeters.
+    
+    unsigned long timeLow2High;
+    unsigned long dt;
+    static int state;
+    static bool isFirst = true;  
+    static unsigned long timeHigh2Low; 
+    
+    sensorVal = analogRead(ANALOG_PIN);             // Voltage measurements from the photogate 
+    
+    // Initialize and assign sensor state. Runs only once, when the program is first run. 
+    if (isFirst) {
+      if (sensorVal > THRESHOLD) {
+        state = SENSOR_HIGH;
+      }
+      
+      else {
+        state = SENSOR_LOW;
+      }
+      timeHigh2Low = micros();
+      isFirst = false;
+    }
+
+    // Take action based on sensor state.
+    if (state == SENSOR_HIGH) {                 
+     
+      if (sensorVal <= THRESHOLD) {                 // sensor is in high but value is below threshold ! Object is moving into beam.
+        state = SENSOR_LOW;                         // switch states
+        timeHigh2Low = micros();                    //  take time measurement
+//        Serial << "timeHigh2Low " << _DEC(timeHigh2Low) << endl;
+      
+      }
+    }
+    
+    else {
+      if (sensorVal > THRESHOLD) {                  // sensor is in low state but value is higher than thrshold ! Object is moved out of the beam.
+        state = SENSOR_HIGH;                        // switch states
+        timeLow2High = micros();                    // take time measurement
+  
+  
+        dt = (timeLow2High - timeHigh2Low);           // calculate the time the sensor was blocked
+        
+     
+        
+        Serial << " Velocity: " << (float(pendulumWidth)/dt)*1000.0 << " m/s" << '\n'; // calculate velocity, convert to m/s and print to Serial Monitor
+        
+      }
+    }
+    
+  }
+    
+