CODING
Arduino Coding
The coding of the whole program is shown below, programming is based on Arduino.
PROGRAM SOURCE CODE
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
#define OLED_RESET 4 // Reset pin
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
#define anjian 6
int count1_low = 0;
int count2_low = 0;
int count1_high = 0;
int count2_high = 0;
int y_standard_low = -100;
int y_standard_high = 100;
int z_standard_low = -200;
int z_standard_high = 100;
//int z_standard = 500;
float x_returnZero,y_returnZero,z_returnZero;
void setup()
{
Serial.begin(38400); // 19200 bps
Serial.println("CLEARDATA");//clear excel sheet
Serial.println("LABEL,current time,x.,y.,z.,count1_low.,count1_high.,count2_low.,count2_high.");
pinMode(anjian,INPUT);
if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // Address 0x3C for 128x32
Serial.println(F("SSD1306 allocation failed"));
for(;;); // Don't proceed, loop forever
}
display.clearDisplay();
display.setTextSize(1)
display.setTextColor(SSD1306_WHITE);
display.setCursor(20, 30);
display.println(F("Initializing..."));
display.display();
delay(2000); // Pause for 2 seconds
// Clear the buffer
display.clearDisplay();
display.display();
delay(2000);
}
void measure()
{
float x,y,z;
x=analogRead(0);
y=analogRead(1);
z=analogRead(2);
float x_returnZero= x-320;
float y_returnZero= y-345;
float z_returnZero= z-365;
Serial.print("DATA,TIME,");
Serial.print(x_returnZero);//random number generator from 0 to 100
Serial.print(",");//print "," for different value
Serial.print(y_returnZero);//random number generator from 0 to 100
Serial.print(",");
Serial.println(z_returnZero);
if (y_standard_low>y_returnZero){
count1_low +=1;
delay(50);
}
if (y_returnZero>y_standard_high){
count1_high +=1;
delay(50);
}
if (z_standard_low>z_returnZero){
count2_low +=1;
delay(50);
}
if (z_returnZero>z_standard_high){
count2_high +=1;
delay(50);
}
}
void loop(){
if(digitalRead(anjian)==HIGH){
Serial.println("start");
for(int i=0; i<=500; i++){
measure();
//Serial.println(count);
}
Serial.print(count1_low);
Serial.print(",");
Serial.print(count1_high);
Serial.print(",");
Serial.print(count2_low);
Serial.print(",");
Serial.println(count2_high);
ShowText();
}
}
void ShowText(void) {
display.clearDisplay();
display.setTextSize(0.5); // Draw 2X-scale text 2倍字体
display.setTextColor(SSD1306_WHITE);
display.setCursor(0, 0); //显示的坐标位置
display.println(F("Errors lifting the arm:"));
display.println(count1_high);
display.println(F("Errors putting down the arm:"));
display.println(count1_low);
display.display(); // Show text
}