The code here is running (should) 4 sensors, speed control and cooling system control. The code compiled fine, but from my previous attempts, I figured that doesn't mean it'll run. I have no access to any sort of an Arduino, due to me being on away, so, please check the code if you have time to waste and send me warnings for all the stupid newb stuff I did :).
#include <Servo.h>
#include <LiquidCrystal.h>
Servo m1;
const byte buttonPin = A2;
const int (servoPin) = 9;
int writeValues[5] = {1000, 1250, 1500, 1750, 2000};
int buttonPushCounter = 0; // counter for the number of button presses
int buttonState = 0; // current state of the button
int lastButtonState = 0;
const int MotorModule = 1;
int ThermistorPin = A0;
int Vo;
float R1 = 10000;
float logR2, R2, T, Tc, Tf;
float c1 = 1.009249522e-03, c2 = 2.378405444e-04, c3 = 2.019202697e-07;
volatile int flow_frequency; // Measures flow sensor pulses
unsigned int l_hour; // Calculated litres/hour
unsigned int SPEED; // Calculated speed
unsigned char flowsensor = 2; // Sensor Input
unsigned long currentTime;
unsigned long cloopTime;
void flow () // Interrupt function
{
flow_frequency++;
}
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
void setup()
{
m1.attach(servoPin);
// initialize the button pin as a input:
pinMode(buttonPin, INPUT_PULLUP);
// initialize the LED as an output:
pinMode(servoPin, OUTPUT);
// initialize serial communication:
lcd.begin(16, 2);
Serial.begin(9600);
pinMode(flowsensor, INPUT);
digitalWrite(flowsensor, HIGH); // Optional Internal Pull-Up
Serial.begin(9600);
attachInterrupt(0, flow, RISING); // Setup Interrupt
sei(); // Enable interrupts
currentTime = millis();
cloopTime = currentTime;
}
void loop()
{
buttonState = digitalRead(buttonPin);
if (buttonState != lastButtonState)
{
// if the state has changed, increment the counter
if (buttonState == LOW)
{
// if the current state is HIGH then the button went from off to on:
buttonPushCounter++;
Serial.println(buttonPushCounter);
Serial.print("buttonPushCounter % 5 = ");
Serial.println(buttonPushCounter % 5);
Serial.print("writing ");
Serial.println(writeValues[buttonPushCounter % 5]);
}
else
{
// if the current state is LOW then the button went from on to off:
Serial.println("off");
}
// Delay a little bit to avoid bouncing
delay(50);
}
lastButtonState = buttonState;
m1.writeMicroseconds(writeValues[buttonPushCounter % 5]);
////////////////////////////////////////
{
Vo = analogRead(ThermistorPin);
R2 = R1 * (1023.0 / (float)Vo - 1.0);
logR2 = log(R2);
T = (1.0 / (c1 + c2 * logR2 + c3 * logR2 * logR2 * logR2));
Tc = T - 273.15;
Serial.print("Temperature: ");
Serial.print(Tc);
Serial.println(" C");
{
if (Tc > 60) {
// turn LED on:
digitalWrite(MotorModule, HIGH);
} else {
// turn LED off:
digitalWrite(MotorModule, LOW);
}
delay(500);
}
}
//////////////////////////////////////////////////////
{
int sensorVal = analogRead(A1);
Serial.println(sensorVal);
float voltage = (sensorVal * 5.0) / 1024.0;
Serial.print("Volts: ");
Serial.print(voltage);
float pressure_MPa = (sensorVal / 5 - 0.1) * 4 / 3;
float atm = pressure_MPa * 0.10133;
Serial.print("MPa: ");
Serial.print(pressure_MPa);
Serial.print("Atm: ");
Serial.print(atm);
delay(100);
lcd.setCursor(8, 2);
lcd.print("Pressure = ");
lcd.print(atm);
lcd.print("atm");
delay(500);
lcd.clear();
}
//////////////////////////////////////////////////
{
Vo = analogRead(ThermistorPin);
R2 = R1 * (1023.0 / (float)Vo - 1.0);
logR2 = log(R2);
T = (1.0 / (c1 + c2 * logR2 + c3 * logR2 * logR2 * logR2));
Tc = T - 273.15;
lcd.setCursor(0, 2);
lcd.print("Temp = ");
lcd.print(T);
lcd.print(" C");
delay(500);
lcd.clear();
}
///////////////////////////////////////////////////
{
// read the input on analog pin 0:
int sensorValue = analogRead(A0);
// Convert the analog reading (which goes from 0 - 1023) to a voltage (0 - 5V):
float voltage = sensorValue * (5.0 / 1023.0);
// print out the value you read:
Serial.println(voltage);
lcd.setCursor(8, 1);
lcd.print("Battery voltage = ");
lcd.print(voltage);
lcd.print(" Volts");
delay(500);
lcd.clear();
}
///////////////////////////////////////////////
{
currentTime = millis();
// Every second, calculate and print litres/hour
if (currentTime >= (cloopTime + 1000))
{
cloopTime = currentTime; // Updates cloopTime
// Pulse frequency (Hz) = 7.5Q, Q is flow rate in L/min.
l_hour = (flow_frequency * 60 / 7.5); // (Pulse frequency x 60 min) / 7.5Q = flowrate in L/hour
flow_frequency = 0; // Reset Counter
{
SPEED = (l_hour / 6);
Serial.print(SPEED, DEC);
Serial.println("km/h");
lcd.setCursor(0, 1);
lcd.print("km/h = ");
lcd.print(SPEED, DEC);
lcd.print(" km/h");
delay(500);
lcd.clear();
}
}
}
//////////////////////////////////////////////////
}
Look I am the newbiest coder you could imagine, hence why almost all the code is copied from threads, where it had worked for them. The only actual part that I wrote (with a lot of help from forum members) is the ESC control, which is the first one. You are talking in a language of gods for me, so figuring what a shadow variable ( even with the internet) is quite a challenge.
the braces, I thought you should on every start and end of every function, that why there is so many of them because for the newb, I thought that every individual code made or copied was like a function, hence the braces...
2.At any point in your code where you need flow_frequency, you need to call cli() or noInterrupts(), make a copy of that value in a different variable, and then sei() or Interrupts() to re-enable your interrupts. You should never try to access a multi-byte variable that is used in an interrupt while interrupts are running. But remember to have them off as short as possible. Just make a copy of the value and re-enable. I kind of get what you say, but I don't.
Do you seriously think that I haven't done any " thinking or reading"? Just because I don't understand what you are saying, you call me a moron, you pretty much criticise me to the point that I just want to the 2-year project. So if you aren't able to understand that even if I read the google articles or your instructions you have given, that it is still hard for me, then thanks for the "help".
If you actually want to help me you have to take in mind what I have said, most of the code is copied. I HAVE gone onto the Arduino web page and I HAVE read the references multiple times, but it is still not completely in the light for me.
If you are willing to help me, just try to please explain it in normie English.
Okay, that out of the way. I have read about a dozen articles about the cli() and sei(). Both of them are talking about a global interrupt flag or clearing the flag.so is this a system of checking some global variable, that is included in everything...?
The guy said that the cli, clears the global interrupt flag, in my language, it terminates all the interrupts that are going on... the sei() is a bit more complex, I think it turns all the interrupts back on, however "Enables interrupts by setting the global interrupt mask" is the explaination that I got...
Ok, do we move on to the curly braces?
If yes, then I found that they are always used to execute and terminate some sort of function, like a loop or statement ( if, else right?). Which is why I had them at every beginning of a new sensor's stage.
Matyk:
The guy said that the cli, clears the global interrupt flag, in my language, it terminates all the interrupts that are going on... the sei() is a bit more complex, I think it turns all the interrupts back on, however "Enables interrupts by setting the global interrupt mask" is the explaination that I got...
This is the part you will have to learn, you need to learn the terminology, and be able to speak it to a point.
The 2 calls you are talking about, are relatively self explanatory, you are making it more complex then it is.
One turns interrupts off and insures no interrupts fires that will change the registers. It is at that pint you copy your variable that was protected.
Next, turn the interrupts back on, and continue with your code execution. Please remember to keep the ISR (interrupt service routine) doing very little work.
Matyk:
Ok, do we move on to the curly braces?
If yes, then I found that they are always used to execute and terminate some sort of function, like a loop or statement ( if, else right?). Which is why I had them at every beginning of a new sensor's stage.
I can see where the confusion is. As you code more, and depend less on copypasta (copy paste coding) you will understand better when and where to use them. Some examples of where you would use them.
Matyk:
Ok, do we move on to the curly braces?
If yes, then I found that they are always used to execute and terminate some sort of function, like a loop or statement ( if, else right?). Which is why I had them at every beginning of a new sensor's stage.
You can't just take random lines of code, wrap curly braces around them and call them functions - you have to define functions:
<return type> <function name>(<arguments>)
{
//insert code here
}
"Serious bug if this is for continuous use. This will run for about 49 days and then take a very long break." However, the only thing I thought of is that the Arduino has limited memory, so after a while (49 days) the memory would fill up. I have not thought of anything else.
Wait, am I reading this correctly, have you honestly been struggling for 2 years?
Friend, if this is true, I truly can understand your frustration. While looking at other peoples code can help you learn, you need to study it and understand it. You can not simply copy code past it where you want and understand what it is doing. It simply does not work out well.
"Serious bug if this is for continuous use. This will run for about 49 days and then take a very long break." However, the only thing I thought of is that the Arduino has limited memory, so after a while (49 days) the memory would fill up. I have not thought of anything else.
Memory will not fill up unless you are doing something silly,at this stage you are not doing anything that will "fill up" the memory.
"Serious bug if this is for continuous use. This will run for about 49 days and then take a very long break." However, the only thing I thought of is that the Arduino has limited memory, so after a while (49 days) the memory would fill up. I have not thought of anything else.
Yes, but him telling you and you understanding are 2 different things. You need to understand why this is a better way. Hint, it has nothing to do with memory. unsigned long - Arduino Reference
Romanga, #21 ,to make it clear the project is an underwater scooter V2. I have made the first version, now I am working on a second version, which will be faster and more intelligent, in the form of a temp readout, depth readout, speed readout and a battery voltage indicator.
Matyk:
Romanga, #21 ,to make it clear the project is an underwater scooter V2. I have made the first version, now I am working on a second version, which will be faster and more intelligent, in the form of a temp readout, depth readout, speed readout and a battery voltage indicator.
Sounds like a fun project, I take it you are a diver.