TheMemberFormerlyKnownAsAWOL:
Seriously, post your code.
All of it.
Here is all of the code:
/* Comado052
* - added testingHeight variable for testing
* - Now printing on LCD testingHeight instead of calcHeight
* - added line 168 - calculating testingHeight
* - Changed the way curent limit works - now stops both on motion up and down if overcurrent occurs.
* - corrected values of CL and CR, previous values were wrong.
* - Implemented the in-plate mounted resistor system
* - Added blinking of LCD message while plate is not mounted
*/
// include the library code:
#include <LiquidCrystal.h>
// initialize the library by associating any needed LCD interface pin
// with the arduino pin number it is connected to
const int rs = 13, en = 10, d4 = 5, d5 = 6, d6 = 3, d7 = 2; // replaced pins: 12->13, 4->6
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
#include "DualMC33926MotorShield.h"
DualMC33926MotorShield md;
//=====================================================================================================
//============== User can change the values below =====================================================
//=========== Definition of arrays for 18 cases of plates=========
//plate No. 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
float At[18] = {72.3, 72.3, 72.3, 72.3, 78.3, 78.3, 78.3, 78.3, 81.6, 81.6, 81.6, 81.6, 81.6, 85.5, 85.5, 85.5, 85.5, 85.5}; //arc top
float Ab[18] = {98.7, 99.6, 100.5, 101.4, 104.3, 105.2, 106.1, 107.0, 106.1, 107.1, 108.1, 109.0, 110.0, 111.0, 112.0, 112.9, 113.9, 114.9}; // arc bottom
float CL[18] = {13.7, 14.3, 14.9, 15.6, 13.4, 14.1, 14.7, 15.4, 13.4, 14.0, 14.6, 15.3, 16.0, 14.5, 15.2, 15.9, 16.5, 16.8}; // left triangle base
float CR[18] = {12.1, 12.4, 12.8, 13.1, 12.2, 12.5, 12.9, 13.2, 12.4, 12.7, 13.1, 13.4, 13.8, 13.2, 13.5, 13.9, 14.2, 14.6}; // right triangle base
float hC[18] = {3.6, 3.6, 3.6, 3.6, 3.6, 3.6, 3.6, 3.6, 6.6, 6.6, 6.6, 6.6, 6.6, 8.6, 8.6, 8.6, 8.6, 8.6} ; //initial hieght
float plateRvalue[18] = {1, 2, 3, 3.9, 5.1, 6.2, 7.5, 8.2, 9.1, 10, 11, 12, 13, 15, 16, 18, 20, 22}; //value of resistor of each plate in kOhm
int correspondingReading[18]; // this is the expected corresponding reading that the program should see for each plate - calculated and assigned under Setup
const int currentLimit = 800; // current limit in mA
//=====================================================================================================
//=====================================================================================================
//=====================================================================================================
//=============The program below NOT to be touched ====================================================
int potIn = A5;
int selectorPot = A2;
int fwdButton = A3;
int rvsButton = 8;
int z=0; //counter
boolean ovrCurrent = false;
boolean BlinkOnOff = false;
#define Blink_interval 500
unsigned long previousMillis = 0;
float measure = 0; // the potentiometer value
float avgMeasure = 0;
float PL = 0;
float PR = 0;
float testingHeight = 0;
float perimeterCnst; // the constant part of the perimeter
int plateNo = 0; // number of plate selected
int plateSize = 88; // the size of selected plate (88-105)
int measureToPrint;
int actualSelectReading = 0; // actual reading of plate selecting input
int fwd = 0; // store the state of fwdButton pin
int rvs = 0; // store the state of rvsButton pin
double calcHeight = 0; // the calculated height
double perimeter; // the calculated perimeter
void setup() {
pinMode(fwdButton, INPUT);
pinMode(rvsButton, INPUT);
// set up the LCD's number of columns and rows:
lcd.begin(16, 2);
md.init();
Serial.begin(9600);
for (int k=0; k<18; k++) {
correspondingReading[k] = 1024.0*1.25*((float)plateRvalue[k])/(9.8 + (float)plateRvalue[k]); // this is the expected corresponding reading that the program should see for each plate. 1.25 is correction factor, reading is off by this factor
}
}
void loop() {
perimeterCnst = At[plateNo] + Ab[plateNo];
md.setM2Speed(0);
readPotentiometers();
readButtons();
movement();
calculate();
lcdPrint();
delay(10);
}
void readPotentiometers(){
for (int i=0; i< 200; i++) { // taking the average of 200 readings to stabilize the display
measure = measure + analogRead(potIn);
if (i==199) {
avgMeasure = measure/200;
measure = analogRead(potIn);
}
}
measureToPrint = avgMeasure; // the analog measured value is saved beacause avgMeasure is going to be mapped afterwards
for (int i=0; i<1000; i++) {
if (i==999) { //take a measurment once in 1000 cycles
actualSelectReading = analogRead(selectorPot);
for (z=0; z < 18 && abs(actualSelectReading - correspondingReading[z]) > 15; z++) { //check which plate the reading corresponds to
}
plateNo = z;
plateSize = plateNo + 88; //convert plate number to plate size for printing on LCD (values 88-105)
}
}
}
void readButtons () {
if (digitalRead(fwdButton) && fwd==LOW && rvs==LOW){
fwd = HIGH;
rvs = LOW;
}
if (digitalRead(rvsButton) && fwd==LOW && rvs==LOW){
rvs = HIGH;
fwd = LOW;
}
// if ((digitalRead(fwdButton) || digitalRead(rvsButton)) && (rvs || fwd)){
// fwd = LOW;
// rvs = LOW;
// }
}
void movement() {
if (md.getM1CurrentMilliamps()>currentLimit) { //checks overcurrent condition and if happens, stops motion
ovrCurrent = true;
md.setM1Speed(0);
}
if (fwd == HIGH && avgMeasure >180 && ovrCurrent == false) { //check conditions and then move DOWN
md.setM1Speed(400);
}
else if (rvs == HIGH && avgMeasure < 980 && ovrCurrent == false) { //check conditions and then move UP
md.setM1Speed(-400);
}
else {
ovrCurrent = false;
md.setM1Speed(0);
rvs = LOW;
fwd = LOW;
}
}
void calculate(){
avgMeasure = map((float)avgMeasure, 180.0, 980.0, 4350.0, 2650.0); // the smallest distance between pivot points is 26.5mm and the biggest is 43.5mm
calcHeight = (float(hC[plateNo]) + (sqrt(20250000.0 - (float)avgMeasure*(float)avgMeasure))/100); //side of triangle is 45mm, squared is 2025
PL = sqrt(float(CL[plateNo])*float(CL[plateNo]) + float(calcHeight)*float(calcHeight));
PR = sqrt(float(CR[plateNo])*float(CR[plateNo]) + float(calcHeight)*float(calcHeight));
perimeter = (float)perimeterCnst + (float)PL + (float)PR;
perimeter = round(float(perimeter)*10)/10.0;
testingHeight = (sqrt(20250000.0 - (float)avgMeasure*(float)avgMeasure))/100 - 3.05;
}
void lcdPrint(){
lcd.setCursor(0,0);
lcd.print("P: ");
lcd.print(perimeter);
lcd.setCursor(10, 0);
lcd.print("H");
lcd.print(testingHeight);
if (actualSelectReading > 100 && actualSelectReading < 900) {
lcd.setCursor(0, 1);
lcd.print("L");
if (plateSize <=99){
lcd.setCursor(1, 1);
lcd.print(plateSize);
lcd.print(" ");
}
if (plateSize > 99){
lcd.setCursor(1, 1);
lcd.print(plateSize);
lcd.print(" ");
}
}
else {
blinkError();
}
lcd.setCursor(10, 1);
lcd.print("AN");
lcd.print(measureToPrint);
}
void blinkError() {
unsigned long currentMillis = millis();
if(currentMillis - previousMillis > Blink_interval) {
lcd.setCursor(0,1);
previousMillis = currentMillis;
if(BlinkOnOff){
lcd.print("errPlate");
}
else {
lcd.print(" ");
}
BlinkOnOff = !BlinkOnOff;
}
}