Im new to this forum and Arduino itself so excuse the mess that is my code!
i have two accelerometers plugging into the 6 analogue inputs on my Uno.
When 0-2 are plugged in from accelerometer one's xyz, the programme operates as should.
Include the x from accelerometer two and everything is still ok.
However, the minute y or z are connected to ai4 or ai5 then the arduino appears to crash and needs resetting.
ive included my code, any help would be GREATLY appreciated.
many thanks,
RGUBell!
// LIBRARIES
#include <LiquidCrystal_I2C.h>
#include <Wire.h>
LiquidCrystal_I2C lcd(0x27, 20, 4);
// ASSIGN LED COLOURS TO INTEGER for pins
int redled = 9;
int yelled = 8;
int greled = 7;
int buttonin = 5;
// ASSIGN SWITCH FOR PHASE DISPLAY for pins
int SwitchRight = 2;
int SwitchLeft = 3;
float Xone;
float Yone;
float Zone;
float Xtwo;
float Ytwo;
float Ztwo;
float ABSXone;
float ABSYone;
float ABSZone;
float ABSXtwo;
float ABSYtwo;
float ABSZtwo;
int scale = 3;
double counter = 0;
// INITAL SEQUENCE UPON START UP
void setup()
{
Serial.begin(115200); //start serial at 9600 baud
Serial.println("1");
pinMode(redled, OUTPUT); //set digital pins
pinMode(yelled, OUTPUT);
pinMode(greled, OUTPUT);
pinMode(buttonin, INPUT);
lcd.begin(); //initiate lcd
Wire.begin();
lcd.backlight();
delay(1000);
lcd.setCursor(0, 0); //initial title splash screen
lcd.print("Kieran Bell");
lcd.setCursor(0, 1);
lcd.print("1306903");
lcd.setCursor(0, 3);
lcd.print("MEng Project");
// LAMP TEST SEQUENCE
digitalWrite(redled, LOW);
digitalWrite(greled, LOW);
digitalWrite(yelled, HIGH);
delay (1000);
digitalWrite(redled, LOW);
digitalWrite(greled, HIGH);
digitalWrite(yelled, LOW);
delay (1000);
digitalWrite(redled, LOW);
digitalWrite(greled, LOW);
digitalWrite(yelled, HIGH);
delay (1000);
lcd.clear();
lcd.setCursor(0, 0); //secondary title splash screen
lcd.print("Generator Condition");
lcd.setCursor(0, 1);
lcd.print(" Monitoring System");
lcd.setCursor(0, 3);
lcd.print(" Vibration Sensing");
// LAMP TEST SEQUENCE
digitalWrite(redled, HIGH);
digitalWrite(greled, LOW);
digitalWrite(yelled, LOW);
delay (1000);
digitalWrite(redled, LOW);
digitalWrite(greled, LOW);
digitalWrite(yelled, HIGH);
delay (1000);
digitalWrite(redled, LOW);
digitalWrite(greled, HIGH);
digitalWrite(yelled, LOW);
delay (500);
lcd.clear();
delay (500);
Xone = 0;
Yone = 0;
Zone = 0;
Xtwo = 0;
Ytwo = 0;
Ztwo = 0;
}
void loop()
{
int SwitchOne;
int SwitchTwo;
SwitchOne = digitalRead(SwitchRight);
SwitchTwo = digitalRead(SwitchLeft);
int rawXone = analogRead(A0);
int rawYone = analogRead(A1);
int rawZone = analogRead(A2);
int rawXtwo = analogRead(A3);
int rawYtwo = analogRead(A4);
int rawZtwo = analogRead(A5);
float scaledXone, scaledYone, scaledZone;
float scaledXtwo, scaledYtwo, scaledZtwo;
scaledXone = mapf(rawXone, 0, 675, -scale, scale); // 3.3/5 *1023 ~=675
scaledYone = mapf(rawYone, 0, 675, -scale, scale);
scaledZone = mapf(rawZone, 0, 675, -scale, scale);
scaledXtwo = mapf(rawXtwo, 0, 675, -scale, scale);
scaledYtwo = mapf(rawYtwo, 0, 675, -scale, scale);
scaledZtwo = mapf(rawZtwo, 0, 675, -scale, scale);
ABSXone = abs(scaledXone);
ABSYone = abs(scaledYone);
ABSZone = abs(scaledZone);
ABSXtwo = abs(scaledXtwo);
ABSYtwo = abs(scaledYtwo);
ABSZtwo = abs(scaledZtwo);
// WRITE NEW PEAK VALUE
if ( ABSXone > Xone ) { Xone = scaledXone; }
if ( ABSYone > Yone ) { Yone = scaledYone; }
if ( ABSZone > Zone ) { Zone = scaledZone; }
if ( ABSXtwo > Xtwo ) { Xtwo = scaledXtwo; }
if ( ABSYtwo > Ytwo ) { Ytwo = scaledYtwo; }
if ( ABSZtwo > Ztwo ) { Ztwo = scaledZtwo; }
if ( counter == 1000 || counter == 0)
{
if ( SwitchTwo == 0 && SwitchOne == 1 )
{
lcd.setCursor(0, 0);
lcd.print("Brng. Vibr. (g) NDE");
lcd.setCursor(0, 1);
lcd.print("X: ");
lcd.print(ABSXone);
lcd.setCursor(9, 1);
lcd.print("(Pk: ");
lcd.print(Xone);
lcd.print(")");
lcd.setCursor(0, 2);
lcd.print("Y: ");
lcd.print(abs(ABSYone));
lcd.setCursor(9, 2);
lcd.print("(Pk: ");
lcd.print(Yone);
lcd.print(")");
lcd.setCursor(0, 3);
lcd.print("Z: ");
lcd.print(abs(ABSZone));
lcd.setCursor(9, 3);
lcd.print("(Pk: ");
lcd.print(Zone);
lcd.print(")");
}
else //if ( SwitchTwo == 1 && SwitchOne == 0 )
{
lcd.setCursor(0, 0);
lcd.print("Brng. Vibr. (g) DE");
lcd.setCursor(0, 1);
lcd.print("X: ");
lcd.print(ABSXtwo);
lcd.setCursor(9, 1);
lcd.print("(Pk: ");
lcd.print(Xtwo);
lcd.print(")");
lcd.setCursor(0, 2);
lcd.print("Y: ");
lcd.print(ABSYtwo);
lcd.setCursor(9, 2);
lcd.print("(Pk: ");
lcd.print(Ytwo);
lcd.print(")");
lcd.setCursor(0, 3);
lcd.print("Z: ");
lcd.print(ABSZtwo);
lcd.setCursor(9, 3);
lcd.print("(Pk: ");
lcd.print(Ztwo);
lcd.print(")");
}
// ALARM SETUP
if ( Xone > 1 || Yone > 1 || Zone > 2)
{
digitalWrite(redled, HIGH);
digitalWrite(greled, LOW);
digitalWrite(yelled, LOW);
}
else if( Xone > 0.5 || Yone > 0.5 || Zone > 1.5)
{
digitalWrite(yelled, HIGH);
digitalWrite(redled, LOW);
digitalWrite(greled, LOW);
}
else
{
digitalWrite(greled, HIGH);
digitalWrite(yelled, LOW);
digitalWrite(redled, LOW);
}
counter = 0;
}
//delay(100);
// RATHER THAN DELAY, IMPLEMENT COUNTER TO RUN THROUGH CODE.
// IF COUNTER REACHES VALUE, PRINT LCD
counter++;
}
float mapf(float x, float in_min, float in_max, float out_min, float out_max)
{
return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
}