#include // Encoder library http://www.pjrc.com/teensy/td_libs_Encoder.html #include #include #include // LCD Screen Library http://www.rmcybernetics.com/projects/code/index.htm // PORT DEFINITIONS #define AD_VAL A1 #define LED_PWR 10 #define ENCODER_A 2 #define ENCODER_B 3 #define ENCODER_SW 8 // ENCODER VARIABLES Encoder ENCODER(3, 2); // Set up encoder using didital pins 3 and 2 (these two use an interrupt) unsigned int encoderInc[5] = {1, 10, 100, 1000, 10000}; // multiplication factor of encoder when adjusting values. Declared array size should be one greater than number of elements int maxMulti = 4; // max array position selectable from above int encoderIncIndex = 0; //index position of increment values // GRAPH VARIABLES int yOffset = 63; // Sets the starting Y position. (32 is middle of screen, 63 is bottom) int xStep = 1; // The number of pixels to step x coordinate by on each loop int x = xStep; // the X coordinate int y = 0; // the Y coordinate int yP = 0; // the previous Y coordinate boolean mode = true; // false=points, true=lines boolean showY = true; // If true, shows the Y values on screen // PUSH SWITCH VARIABLES boolean encSwP = 0; // Previous state of encoder push switch I2C_KS0108C_GLCD lcd; // Sensor Variables float dustVal=0; int delayTime=280; int delayTime2=40; float offTime=9680; void setup(){ Serial.begin(9600); pinMode(LED_PWR,OUTPUT); pinMode(AD_VAL, INPUT); pinMode(ENCODER_A, INPUT); pinMode(ENCODER_B, INPUT); pinMode(ENCODER_SW, INPUT); // ENABLE PULLUP RESISTORS digitalWrite(ENCODER_A, HIGH); digitalWrite(ENCODER_B, HIGH); digitalWrite(ENCODER_SW, HIGH); lcd.begin (); TWBR = ((16000000 / 400000L) - 16) / 2;// Increase I2C speed to 400kHz instead of the default 100kHz delay(500); lcd.clear (0, 0, 128, 64, 0); // Set Whole screen Blank lcd.gotoxy (1, 0); // Move cursor to x,y position } void loop(){ int num_avgs = 10; // Set how many reading to take and make average from dustVal = 0; for (int i=0; i36.455) Serial.println((float(dustVal/1024)-0.0356)*120000*0.035); //Serial.println(dustVal); // Display Results if (x > 127) { // If has reached end of screen lcd.clear (0, 0, 127, 63, 0); // Clear Screen x = xStep; // Reset x coordinate } boolean encSw = !digitalRead(ENCODER_SW); // get switch state, use ! becasue active low if (encSw == 1) { // If pressed if (encSwP == 0) { // and was preveiously not pressed (to prevent retriggering) mode = !mode; } encSwP = 1; } else encSwP = 0; long scale = (ENCODER.read()*3); int pk = 800+scale; if (pk <85) pk=85; y = map((int)dustVal,80,pk,0,yOffset-6); // Scale reading to display // Clip Y values to screen limits if (y > yOffset-6) y = yOffset-6; if (y < 0) y = 0; // Show Scale char buff[13]; // text output buffer sprintf(buff, "Pk=%d", (int)pk); // Convert number to text (e.g. shows Y=12) lcd.gotoxy (10*8, 0); // reset text cursor position lcd.string(" ", 0); // Clear text lcd.gotoxy (10*8, 0); // reset text cursor position lcd.string(buff, 0); // Display text if (showY) { // If showing Y values char buf[13]; // text output buffer sprintf(buf, "Y=%d", (int)dustVal); // Convert number to text (e.g. shows Y=12) lcd.gotoxy (0, 0); // reset text cursor position lcd.string(" ", 0); // Clear text lcd.gotoxy (0, 0); // reset text cursor position lcd.string(buf, 0); // Display text } if (mode == true) { //lcd.setPixel(x, yOffset-y, 1); // Plot Points lcd.line(x-xStep, 63, x, yOffset-y, 1); // Plot Vertical Lines (Bar) } else { lcd.line(x-xStep, yOffset-yP, x, yOffset-y, 1); // Plot Joined Lines } yP = y; // Store previous Y coordinate (used for drawing lines) x = x + xStep; // Increment X }