Hello good people of Arduino Land.
This question relates to another question about the same project.
It is in regards to a water drop controller which I am building for high speed photography, water drop collision projects.
The previous post goes into more detail about what I am hoping to achieve, and can be found with the following link:
https://forum.arduino.cc/index.php?topic=679995.0
I shall be using four Arduinos in total:
- One Nano as master
- Three ATMega328P-PU chips as slaves to operate the valves - one valve/chip
The Nano is the input for the timing data, and sends this to the relevant 328 chip via I2C.
It will also control the camera and flash - triggering the flash after the drop sequence has completed on the slaves.
So far, I have been reasonably successful, in that I have my timing data input correctly and displayed on a single 20x4 LCD, and I have been able to send the appropriate data to one of the 328 chips.
This is where the problems start.
The slave seems to receive the correct data, but the 'on/off' cycles of the valves (I'm using LEDs as stand ins) is not working correctly.
I want the valve to open and close three times, with a pause between each drop - which can set on the master.
However, I am getting five 'pulses'....not three.
And consequently the sequence is taking too long, so the flash goes out of sync.
I have stared and stared at the IDE, and I cannot for the life of me work out why it's doing that!
The slave sketch is very simple....just a series of digitalWrites to set the valve pins HIGH or LOW
Here is the master sketch:
(It's in two parts due to the 9000 character limit)
//Six pot, three valve drop controller
//MASTER
#include <digitalIOPerformance.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 20, 4);
byte customChar1[8] = {
0b10100,
0b01000,
0b00000,
0b00100,
0b01100,
0b00100,
0b00100,
0b00100
};
byte customChar2[8] = {
0b10100,
0b01000,
0b00000,
0b01110,
0b00010,
0b00100,
0b01000,
0b01110
};
byte customChar3[8] = {
0b10100,
0b01000,
0b00000,
0b01110,
0b00010,
0b00110,
0b00010,
0b01110
};
byte customChar4[8] = {
0b11100,
0b10000,
0b11000,
0b10001,
0b10010,
0b00100,
0b01000,
0b10000
};
byte customChar5[8] = {
0b00100,
0b01000,
0b10000,
0b00111,
0b00101,
0b00111,
0b00100,
0b00100
};
#define button1 2
#define button2 3
#define button3 4
#define pot1 A0
#define pot2 A1
#define pot3 A2
#define pot4 A3
#define pot5 A6
#define pot6 A7
#define startButton 5
#define flash 6
#define camera 7
#define trigger 9
int pot1Array[3];
int pot2Array[3];
int pot3Array[3];
int pot4Array[3];
int pot5Array[3];
int pot6Array[3];
int button1state = HIGH;
int button2state = HIGH;
int button3state = HIGH;
int startButtonState = HIGH;
void setup() {
Serial.begin(9600);
Wire.begin();
pinMode(button1, INPUT_PULLUP);
pinMode(button2, INPUT_PULLUP);
pinMode(button3, INPUT_PULLUP);
pinMode(startButton, INPUT_PULLUP);
pinMode(flash, OUTPUT);
pinMode(camera, OUTPUT);
pinMode(trigger, OUTPUT);
digitalWrite(flash, LOW);
digitalWrite(camera, LOW);
digitalWrite(trigger, LOW);
lcd.init();
lcd.backlight();
lcd.begin(20, 4);
lcd.clear();
lcd.createChar(0, customChar1);
lcd.createChar(1, customChar2);
lcd.createChar(2, customChar3);
lcd.createChar(3, customChar4);
lcd.createChar(4, customChar5);
lcd.setCursor(2, 0);
lcd.write((uint8_t)3);
lcd.setCursor(3, 0);
lcd.write((uint8_t)4);
lcd.setCursor(4, 0);
lcd.print(" D1 P1 D2 P2 D3 ");
lcd.setCursor(0, 1);
lcd.write((uint8_t)0);
lcd.setCursor(0, 2);
lcd.write((uint8_t)1);
lcd.setCursor(0, 3);
lcd.write((uint8_t)2);
}
void loop() {
int pot1Val = map(analogRead(pot1), 0, 1023, 0, 99);
int pot2Val = map(analogRead(pot2), 0, 1023, 0, 99);
int pot3Val = map(analogRead(pot3), 0, 1023, 0, 99);
int pot4Val = map(analogRead(pot4), 0, 1023, 0, 99);
int pot5Val = map(analogRead(pot5), 0, 1023, 0, 99);
int pot6Val = map(analogRead(pot6), 0, 1023, 0, 99);
int valve1Total = (pot2Array[0] + pot3Array[0] + pot4Array[0] + pot5Array[0] + pot6Array[0]);
int valve2Total = (pot1Array[1] + pot2Array[1] + pot3Array[1] + pot4Array[1] + pot5Array[1] + pot6Array[1]);
int valve3Total = (pot1Array[2] + pot2Array[2] + pot3Array[2] + pot4Array[2] + pot5Array[2] + pot6Array[2]);
int totalTime = max(valve1Total, max(valve2Total, valve3Total));
startButtonState = digitalRead(startButton);
if (startButtonState == LOW) {
Wire.beginTransmission(8);
Wire.write(pot2Array[0]);
Wire.write(pot3Array[0]);
Wire.write(pot4Array[0]);
Wire.write(pot5Array[0]);
Wire.write(pot6Array[0]);
Wire.endTransmission();
Wire.beginTransmission(9);
Wire.write(pot1Array[1]);
Wire.write(pot2Array[1]);
Wire.write(pot3Array[1]);
Wire.write(pot4Array[1]);
Wire.write(pot5Array[1]);
Wire.write(pot6Array[1]);
Wire.endTransmission();
Wire.beginTransmission(10);
Wire.write(pot1Array[2]);
Wire.write(pot2Array[2]);
Wire.write(pot3Array[2]);
Wire.write(pot4Array[2]);
Wire.write(pot5Array[2]);
Wire.write(pot6Array[2]);
Wire.endTransmission();
digitalWrite(camera, HIGH);
delay(500);
digitalWrite(trigger, LOW);
delay(totalTime + pot1Array[0]);
digitalWrite(flash, HIGH);
digitalWrite(trigger, HIGH);
delay(50);
digitalWrite(flash, LOW);
delay(500);
digitalWrite(camera, LOW);
}
else {
digitalWrite(flash, LOW);
digitalWrite(camera, LOW);
digitalWrite(trigger, HIGH);
}
//F/P
lcd.setCursor(2, 1);
lcd.print(" ");
lcd.setCursor(2, 1);
lcd.print(pot1Array[0]);
//delay(10);
lcd.setCursor(2, 2);
lcd.print(" ");
lcd.setCursor(2, 2);
lcd.print(pot1Array[1]);
//delay(10);
lcd.setCursor(2, 3);
lcd.print(" ");
lcd.setCursor(2, 3);
lcd.print(pot1Array[2]);
//delay(10);
//D1
lcd.setCursor(5, 1);
lcd.print(" ");
lcd.setCursor(5, 1);
lcd.print(pot2Array[0]);
//delay(10);
lcd.setCursor(5, 2);
lcd.print(" ");
lcd.setCursor(5, 2);
lcd.print(pot2Array[1]);
//delay(10);
lcd.setCursor(5, 3);
lcd.print(" ");
lcd.setCursor(5, 3);
lcd.print(pot2Array[2]);
//delay(10);
//P1
lcd.setCursor(8, 1);
lcd.print(" ");
lcd.setCursor(8, 1);
lcd.print(pot3Array[0]);
//delay(10);
lcd.setCursor(8, 2);
lcd.print(" ");
lcd.setCursor(8, 2);
lcd.print(pot3Array[1]);
//delay(10);
lcd.setCursor(8, 3);
lcd.print(" ");
lcd.setCursor(8, 3);
lcd.print(pot3Array[2]);
//delay(10);
//D2
lcd.setCursor(11, 1);
lcd.print(" ");
lcd.setCursor(11, 1);
lcd.print(pot4Array[0]);
//delay(10);
lcd.setCursor(11, 2);
lcd.print(" ");
lcd.setCursor(11, 2);
lcd.print(pot4Array[1]);
//delay(10);
lcd.setCursor(11, 3);
lcd.print(" ");
lcd.setCursor(11, 3);
lcd.print(pot4Array[2]);
//delay(10);
//P2
lcd.setCursor(14, 1);
lcd.print(" ");
lcd.setCursor(14, 1);
lcd.print(pot5Array[0]);
//delay(10);
lcd.setCursor(14, 2);
lcd.print(" ");
lcd.setCursor(14, 2);
lcd.print(pot5Array[1]);
//delay(10);
lcd.setCursor(14, 3);
lcd.print(" ");
lcd.setCursor(14, 3);
lcd.print(pot5Array[2]);
//delay(10);
//D3
lcd.setCursor(17, 1);
lcd.print(" ");
lcd.setCursor(17, 1);
lcd.print(pot6Array[0]);
//delay(10);
lcd.setCursor(17, 2);
lcd.print(" ");
lcd.setCursor(17, 2);
lcd.print(pot6Array[1]);
//delay(10);
lcd.setCursor(17, 3);
lcd.print(" ");
lcd.setCursor(17, 3);
lcd.print(pot6Array[2]);
//delay(10);