I am trying to measure the angle of a facet maching using a pot. I want the angle to be accurate to 00.00 so I am using a 16 bit ad converter to take the readings because the 10 bit on the nano are not accurate enough.
2 problems:
- when I take any reading the angle is always xx.00 nothing in the decilmals.
- the buzzer never goes off
all of the Serial.print lines are for debug when it is working they will be commented out.
I store the calibration for the pot in the eprom at 90 degrees and 0 degrees.
I take 10 readings to get a good average.
I store the total of the 10 readings for 90 and 0
I do not bother to divide by 10 to save cycles.
more explination of wireing in the comments.
// i have 3 switches connected to ground the other is on a1, a2, a3
// a pot is connected to ground and 5 volts with the sweep on pin A1 on 16 bit ad 115
// a buzzer is connected to ground and pin D3
// an ic2 lcd is connected to power, ground, pinds 4 and 5
//an i2c 115 ad converter is connected power, ground, pin 4 and 5
//ad tested standalone
// i store calibration values in the eprom
// all of the serial.print are for debut will be commented out when working
// the goal is to read a percision linear pot and display it as an angle with 1 or2 digits of percision
// 2 modes read and calibration
// in calibration you set the no digits and calubrate the pot at 90 and 0 which are then stored in eprom
// in read mode a pot reading is taken and displayed
// if in read mode you press set then the angle is stored and if that angle is the same or less then a buzzer is set off
// if the angle is not the same the buzzer is turned off
// in read if clear button is set then the buzzer is turned off
// Include necessary libraries
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <EEPROM-Storage.h>
#include <Bounce2.h>
#include <ADS1X15.h>
// Define pin mappings
const int potentiometer_pin = A0;
const int set_button_pin = A1;
const int clear_button_pin = A2;
const int mode_button_pin = A3;
const int buzzer_pin = 6; // D3 for buzzer
// set up the buttons
//instanciste tbe buttons
Bounce2::Button set_button = Bounce2::Button(); // INSTANTIATE A Bounce2::Button OBJECT
Bounce2::Button clear_button = Bounce2::Button(); // INSTANTIATE A Bounce2::Button OBJECT
Bounce2::Button mode_button = Bounce2::Button(); // INSTANTIATE A Bounce2::Button OBJECT
// set op ads
ADS1115 ADS(0x48);
// Define constants and variables
const char version[5] = "v1.0";
float angle = 0.0;
float buzzer_angle = 0.0;
long zero = 0;
EEPROMStorage<long> epromzero(0, 0);
long ninety = 0;
EEPROMStorage<long> epromninety(90, 0);
int mode = 0; // 0 - Read, 1 - Calibrate
int decimals = 2; // Default to two decimal places
EEPROMStorage<long> epromdecimals(40, 2);
long totalVoltage = 0;
int buzzer_on = 0; //buzzer is off
long pot_reading = 0;
int mode_button_state = 1; // used to do things once
int set_button_state = 1; // used to do things once
int clear_button_state = 1; //used to do things once
// LCD object initialization
LiquidCrystal_I2C lcd(0x27, 16, 2); // LCD address 0x27, 16 chars by 2 lines
// Time tracking variables
unsigned long lastButtonPressTime = 0;
unsigned long currentTime = 0;
void setup() {
// Initialize LCD, pins, etc.
Wire.begin();
Serial.begin(115200);
lcd.init(); // Initialize LCD with 16 columns and 2 rows
lcd.backlight(); // Turn on backligh
//init ad
ADS.begin();
set_button.attach(set_button_pin, INPUT_PULLUP); //set up the set button
set_button.interval(10); //set button debounce time
set_button.setPressedState(LOW); //set button pressed state
clear_button.attach(clear_button_pin, INPUT_PULLUP);
clear_button.interval(10);
clear_button.setPressedState(LOW);
mode_button.attach(mode_button_pin, INPUT_PULLUP);
mode_button.interval(10);
mode_button.setPressedState(LOW);
pinMode(potentiometer_pin, INPUT); //set the pot pin
pinMode(buzzer_pin, OUTPUT); //set the buzzer pin
ninety = epromninety; // set the variable from the eprom
zero = epromzero; // set thevariable from the eprom
decimals = epromdecimals; // set the variable from he eprom
}
void loop() {
// set gain
ADS.setGain(0);
currentTime = millis();
readall();
readpot();
if (ninety == 0) { // if ninety is 0 then the machines calibration
mode = 1;
}
if (mode_button.fell()) {
mode = 1; // incriment mode
lastButtonPressTime = currentTime;
}
if (currentTime - lastButtonPressTime > 60000) {
mode = 0; // Default to Read mode if no action for 1 minute
}
// Handle modes
switch (mode) {
case 0: // Read mode
// lcd.clear();
//lcd.print("in read mode");
// Handle SET button
if (set_button.fell()) {
buzzer_on = 1;
buzzer_angle = angle;
lastButtonPressTime = currentTime;
Serial.print("buzzer on");
}
// Handle CLEAR button
if (clear_button.fell()) {
buzzer_on = 0;
lastButtonPressTime = currentTime;
Serial.println("buzzer off");
}
if (mode_button.fell()) {
mode = 1;
lastButtonPressTime = currentTime;
}
displayAngle();
break;
case 1: // Calibrate mode
// lcd.clear();
// lcd.print("Set = 1 decimal");
// lcd.setCursor(0, 1);
// lcd.print("Clear for 2");
Serial.println("set for 1 dec clear for 2 ");
while ((!set_button.fell()) && (!clear_button.fell())) {
set_button.update();
clear_button.update();
Serial.println("1 or 2 ");
}
Serial.println("at test of which buttn");
if (set_button.fell()) {
epromdecimals = 1;
decimals = 1;
lastButtonPressTime = currentTime;
Serial.println("set pressed");
}
if (clear_button.fell()) {
epromdecimals = 2;
decimals = 2;
lastButtonPressTime = currentTime;
Serial.println("clear pressed");
}
Serial.println("set to 90 and hit set ");
readpot();
set_button.update();
// lcd.clear();
// lcd.print("Set to 90");
// lcd.setCursor(0, 1);
// lcd.print("Press SET");
// Wait for SET button press
while (!set_button.fell()) {
// lcd.clear();
Serial.println("waiting for 90");
// lcd.print("waiting for 90");
readpot();
set_button.update();
}
// this is to do the loop only once on a long press of thebutton
ninety = totalVoltage;
epromninety = totalVoltage;
lastButtonPressTime = currentTime;
// lcd.clear();
// lcd.print("90° set: ");
// lcd.print(ninety);
Serial.println("ninety set ");
Serial.println("set to 0 ");
readpot();
set_button.update();
// lcd.clear();
// lcd.print("Set to 0");
// lcd.setCursor(0, 1);
// lcd.print("Press SET");
// Wait for SET button press for zero degree calibration
while (!set_button.fell()) {
// lcd.clear();
Serial.println("waiting for 0");
// lcd.print("waiting for 0");
readpot();
set_button.update();
}
// this is to do the loop only once on a long press of thebutton
zero = totalVoltage;
epromzero = totalVoltage;
lastButtonPressTime = currentTime;
mode = 0;
Serial.println("zero set");
//end if while
// lcd.clear();
// lcd.print("90° set: ");
// lcd.print(ninety);
break; // end of case 1
}
}
void displayAngle() {
if (decimals == 1) {
lcd.clear();
lcd.print("Angle = ");
lcd.print(angle, 1);
Serial.print("angle = ");
Serial.println(angle);
} else {
Serial.print("angle is ");
Serial.println(angle);
lcd.clear();
lcd.print("Angle = ");
lcd.print(angle, 2);
}
if (buzzer_on == 1) {
lcd.setCursor(0, 1);
lcd.print("Busser at ");
lcd.print(buzzer_angle);
if (angle <= buzzer_angle) {
digitalWrite(buzzer_pin, HIGH);
Serial.println("bzzzzzzz");
}
} else {
lcd.setCursor(0, 1);
lcd.print("Buzzer off");
digitalWrite(buzzer_pin, LOW);
Serial.println("no bzz");
}
}
void readall() {
set_button.update();
clear_button.update();
mode_button.update();
}
void readpot() {
totalVoltage = 0;
for (int i = 0; i < 10; i++) {
pot_reading = ADS.readADC(1);
totalVoltage += pot_reading;
delay(20);
}
angle = (totalVoltage - zero) / ((ninety - zero) / 90);
}
type or paste code here