Thursday, December 13, 2018

project 2

#include <LiquidCrystal.h>
#include "VernierLib.h" //library available from Veriner into interface with their sensors
VernierLib Vernier; //create an instance of the VernierLib library to able to use functions in Vernierlib.h
LiquidCrystal lcd(12, 11, 10, 9, 8, 7);

const int selectButton = 4;
const int leftButton = 5;
const int rightButton = 6;
const int CCWpin = 2; //pin for counterclockwise direction
const int CWpin = 3; //pin for clockwise direction
int Bpress = 1; //counter to keep track of button presses
float sensorReading; //use to store analog sensor reading
double displacement = 0; //variable for displacement wheel conversion to millimeters

volatile int counter = 0; //varible for displacement wheel counting

void setup() {
  Serial.begin(9600); //start serial communication
  Vernier.autoID(); //identify the analog sensor being used. Only one sensor can be detected with Autoint with additional multiplexers to interface multiple with one arduino.
  pinMode(CCWpin, INPUT_PULLUP); //setup CCW pin
  pinMode(CWpin, INPUT_PULLUP); //setup CW pin
  pinMode(selectButton, INPUT);
  pinMode(leftButton, INPUT);
  pinMode(rightButton, INPUT);
  lcd.begin(16,2);        // '16,2' is 16 columns, 2 rows on the display
  digitalWrite(CCWpin, LOW); //initialize CCW pin status
  digitalWrite(CWpin, HIGH); //initialize CW pin status
  attachInterrupt(digitalPinToInterrupt(CCWpin), movingCCW, RISING); //trigger when pin goes from LOW to HIGH
  attachInterrupt(digitalPinToInterrupt(CWpin), movingCW, FALLING); //trigger when pin goes from HIGH to LOW
  Serial.println("Dist  Force  Time");
  Serial.print("\n");
 }

void loop() {
  displacement = counter*0.1; //converts angular movement of displacement wheel to linear displacement of tackle
  sensorReading = Vernier.readSensor(); //readSensor reads the voltage from BTA sensor and automatically converts to units of newtons
  lcd.setCursor(0,0);
  lcd.print(millis() / 1000.0, 3); //time since arduino turned/reset
  lcd.print(" sec       ");
 
  if (digitalRead(leftButton) == HIGH)
    { //this increments through the 3 screens to the left
      Bpress++;
        if (Bpress > 3) //ensures the user doesnt go past the thrid screen
          {
            Bpress = 3;
          }
    }
 
  if (digitalRead(rightButton) == HIGH)
    { //this increments through the 3 screens to the right
      Bpress--;
        if (Bpress < 0) //ensures user doesnt go past the first screen
          {
            Bpress = 1;
          }
    }
   
  switch(Bpress)  //each case shows different screen on LCD
    {
      case 1: //this is default when arduino is powered on
        DispToLCD();
        break;
      case 2: // 2nd screen
        ForceToLCD();
        break;
      case 3: //3rd screen
        serialConnection();
        break;
    }
  delay(250); //wait quarter second
}

void ForceToLCD()
{ //function shows force sensor data when called on LCD
  lcd.setCursor(0,0);
  lcd.print(millis() / 1000.0, 3); //this expression makes time show with decimals for better accuracy
  lcd.print(" sec");
  lcd.setCursor(0,1);
  lcd.print("Force: ");
  lcd.print(sensorReading); //print force data value
  lcd.print(Vernier.sensorUnits()); //vernier lib function that auto detects correct units for sensor used
  lcd.print(" ");
}

void DispToLCD()
{ //function shows displacement data when called on LCD
  lcd.setCursor(0,0);
  lcd.print(millis() / 1000.0, 3); //this expression makes time show with decimals for better accuracy
  lcd.print(" sec");
  lcd.setCursor(0,1);
  lcd.print("Disp: ");
  lcd.print(displacement);  //resolution of displacement sensor is 0.1mm.
  lcd.print(" mm   ");
}

void DataToSerial()
{ //function to print all sensor data to serial when called
  Serial.print(displacement); //resolution of displacement sensor is 0.1mm.
  Serial.print("  "); //space to make data look nice on serial monitor
  Serial.print(sensorReading); //print force data value
  Serial.print("   "); //print a space
  Serial.println(millis() / 1000.0, 3); //this expression makes time show with decimals for better accuracy
  Serial.print("\n");
}

void serialConnection()
{ //function asks user if they want to send data over serial and awaits for select button to be held to initiate
  lcd.setCursor(0,0);
    lcd.print("send data to pc");
    lcd.setCursor(0,1);
    lcd.print("over serial?   ");
    if (digitalRead(selectButton) == HIGH)  //when the select button is held down data will be sent to PC over serial bus
    {
      DataToSerial();
      lcd.setCursor(0,0);
      lcd.print("serial monitor  ");
      lcd.setCursor(0,1);
      lcd.print("running on pc   ");
    }
}

void movingCCW()
{ //counts with interrupt as displacement wheel is turned counterclockwise
    counter++; //count UP
}

void movingCW()
{ //counts with interrupt as displacement wheel is turned clockwise
    counter--; //count DOWN
}   

No comments:

Post a Comment