Go Down

Topic: I want to make this a little cleaner (Read 332 times) previous topic - next topic

grio

Dec 08, 2017, 12:20 am Last Edit: Dec 08, 2017, 12:32 am by grio
Code: [Select]

/*
  Solid states relays have no movin parts and "last for ever" Their life span depends on overheating.  Their amp raiting is typically high. A 40amp relay goes for 10-12 on amazon. 25 amp goes for 9-10
  LCD display can be easily added to this system. I recomend a LCD display with I2C. None I2C LCD displays are a pain to wire up and take up a lot more space and require more parts. Long run i2c is cheaper and easier
  things after // or between the "/ *" "* /" do not take up memory when complied into the processor. These are just notes for a person readin the code.
*/


#include <OneWire.h> //Libary for 1 wire communication to sensors
#include <DallasTemperature.h> //libary fortalking to Dallas sensors.
#include <secTimer.h> //libary just for my ease so I am not timing in mili secounds and can delcare when to start the clock instead of basing it off system time.
secTimer myTimer;
byte relay = 2; // pin to the relay
byte active; // data storage to tell the relay to be on or off.
int temp1; // storage for the sensor
int temp2; // storage for the sensor
DeviceAddress t1 = { 0x28, 0xFF, 0xE9, 0x4D, 0x82, 0x17, 0x04, 0xD2} /* sensor with tape*/, t2 = { 0x28, 0xFF, 0x7E, 0x4C, 0x82, 0x17, 0x04, 0xBB}; // without tape also note these are for my sensors.
/* These are the device address for my Sensors. You will need to find your own device addresses. A webpage is linked on how to on the forums.
  Also the sensor with tape and without tape is just a visual indactor for myself so I do not mix up the sesnors.*/
byte sysrun; // stores a logic to allow the system to run..
unsigned long seconds = 0; //This is for the time after your pasturization temp. Presets it to start from 0
byte timer; //stores the logic for when to start the timinging logic.
#define overtep 8 /* defines an pin on the microprocessor to indicate the temp has gone over. This is to allow people to see if their
  process ever meets or exceeds 170F getting close to sterlization process. Once reached there is no logic to turn off the indiactor LED
  This is to allow you to go away and come back and see that the process needs tuning to your needs.*/
#define done 9 /* This is to give an LED indactor that the process is done. */
#define databuss  7 //declaring what pin that is used for the sensors for reading of data.
OneWire oneWire(databuss); //looking at the vaule of databuss for what pin to use for the onewire databus.
DallasTemperature sensors(&oneWire); // tells the dallas to look at onewire for the temp info

void setup() {
  pinMode(overtep, OUTPUT);
  pinMode(done, OUTPUT);
  pinMode(relay, OUTPUT);
  digitalWrite (overtep, HIGH);
  digitalWrite (done, HIGH);
  digitalWrite(relay, HIGH);

  sensors.begin();
  active = 0;
  timer = 0;
  Serial.begin(9600);
  /* This is only required if runnin the serial monitor. Remove the"//" to activate this. This is only if you have a usb
    between your computer and the controller. This also requires the source code and Arduino compiler.*/
  sysrun = 1; // ensures systrun is set to 1 for system opertation before start.

  // ensureing everything is off on startup.

}

void loop() {
  sensors.requestTemperatures(); // requsts data from sensors
  temp1 = sensors.getTempF(t1); // pulls data from sensor t1 and loads it to temp1
  temp2 = sensors.getTempF(t2); // pulls data from sensor t2 and loads it to temp2
  /* serial outputs to a consle on a computer.
    This only works if the micro processor is hooked to computer and the serial monitor is activated.
    Remove the "//" before Serial to activate this within the program*/
  Serial.print("Water or steam temp:  ");
  Serial.print(temp1);
  Serial.print("    ");
  Serial.print("Substrate Temp:  ");
  Serial.print(temp2);
  Serial.print("\n\n");
  delay(2000); // the processor on the temp sensor only updates every 2 secounds. This is to slow down the program to wait 2.
  //If things are not slown down you are looking at running at rate at a minum of 16Mhz. the delay forces 2 secounds before moving to the next line of code.
  Serial.print("syst status: ");
  Serial.print(sysrun);
  Serial.print("\n\n");

  if (sysrun == 1) // will only run temp control while sysrun is equal to 1
  {
    if (temp1 <= 155) // if temp is bellow or equal to 155 the relay turns on.
    {
      active = 1;
    }
    else if (temp1 > 162) // if the temp is above 162 the relay shuts off.
    {
      active = 0;
    }
    digitalWrite(relay, active);
  }
  Serial.print("relay status:  ");
  Serial.print(active);
  Serial.print("\n\n");
  if (sysrun == 0) //This is to ensure the relay is off after the temp control loop if finished.
  {
    digitalWrite(relay, 0);
  }

  if ((temp2 >= 155) && (timer == 0))
  {
    timer = 1;
    myTimer.startTimer();
    seconds = myTimer.readTimer();
  }

  if ((timer == 1) && (seconds > 1)) // willonle
  {
    seconds = myTimer.readTimer();
  }
  if (3600 <= seconds) // 3600 is one hour in secounds. Incress this or decress this to reduce the time of your pasturization run once at temp.
  {
    sysrun = 0;
    Serial.print("done");
  }
  if (temp1 >= 170)/* this is to indicate that at some point pasturization temp has gotten to or exceeded 170F with an LED.
  real time monitoring can be done with an LCD or a computer with the source code while hooked up to the microccontroller.
  The wire for the sensor can be exnded with minum accurcly lost so if you wish you can incress the length of the probes. */
  {
    digitalWrite (overtep, LOW);
  }
  if (sysrun == 0) // to give an LED indaction that the batch is finshed
  {
    digitalWrite (done, LOW);
  }


}

aarg

Try formatting it in the IDE with a ctrl-T.
  ... with a transistor and a large sum of money to spend ...
Please don't PM me with technical questions. Post them in the forum.

grio

Cool didn't know that.

But was more looking to make the code itself cleaner.

el_supremo

and then put spaces around all operators such as this
Code: [Select]
 if ((timer == 1) && (seconds > 1)) // willonle
and this
Code: [Select]
     seconds = myTimer.readTimer();

Pete
Don't send me technical questions via Private Message.

el_supremo

Don't send me technical questions via Private Message.

Coding Badly

But was more looking to make the code itself cleaner.
I suspect @aarg's post was meant to encourage you to...

1. Use Ctrl + T to reformat your source code.  (As you have done.)

2. Post the reformatted source code so the people on this side of your monitor can much more easily read it.


grio

Done, I am wanting to do more with less.

aarg

You could use millis() instead of a timer library.
  ... with a transistor and a large sum of money to spend ...
Please don't PM me with technical questions. Post them in the forum.

el_supremo

What more do you want to do?

I'd suggest you
- make your comments a lot easier to read by limiting them to about 80 characters per line.
- never hide an initialization in the middle of, or at the end of, a line like you do in this abomination:
Code: [Select]
DeviceAddress t1 = { 0x28, 0xFF, 0xE9, 0x4D, 0x82, 0x17, 0x04, 0xD2} /* sensor with tape*/, t2 = { 0x28, 0xFF, 0x7E, 0x4C, 0x82, 0x17, 0x04, 0xBB}; // without tape also note these are for my sensors.
It would be better like this:
Code: [Select]
DeviceAddress t1 = { 0x28, 0xFF, 0xE9, 0x4D, 0x82, 0x17, 0x04, 0xD2} /* sensor with tape*/,
  t2 = { 0x28, 0xFF, 0x7E, 0x4C, 0x82, 0x17, 0x04, 0xBB}; // without tape also note these are for my sensors.


And fix up all your spelling mistakes and typos. The first comment in your sketch could also do with some punctuation and plain English.

Pete
Don't send me technical questions via Private Message.

terryking228

Hi,

A good reason for doing all this is to be clear to other people (and yourself after months/years !) what the different sections of text mean and how they are related.

I like this basic sketch layout:

Code: [Select]

/* YourDuinoStarter Example: Sketch Template
 - WHAT IT DOES
 - SEE the comments after "//" on each line below
 - CONNECTIONS:
   -
   -
 - V1.00 09/11/12
   Questions: terry@yourduino.com */

/*-----( Import needed libraries )-----*/
/*-----( Declare Constants and Pin Numbers )-----*/
/*-----( Declare objects )-----*/
/*-----( Declare Variables )-----*/


void setup()   /****** SETUP: RUNS ONCE ******/
{


}//--(end setup )---


void loop()   /****** LOOP: RUNS CONSTANTLY ******/
{


}//--(end main loop )---

/*-----( Declare User-written Functions )-----*/


//*********( THE END )***********





and if you are doing something somewhat complicated with sensors and actuators, look at this example:

Code: [Select]

/* YourDuinoStarter Example: Automation Example:
 This shows a way of organizing more complex automation software.
 It works the same as "Lighting Control Example"
 - Reads voltage on Analog Pin 0 and may display value
 - Reads voltage on Analog Pin 1 and may display value
 - Compares values and turns on light if it's darker than setting
 - SEE the comments after "//" on each line below
 - CONNECTIONS:
 - Potentiometer from +5 to Ground, center to pin A0
 - Photoresistor from +5V to pin A1, 10K from A1 to Gnd.
 - Relay may be connected to Pin 13 (or other if "ledPin" is changed.)
 - V1.04 02/11/13
 Questions: terry@yourduino.com */

/*-----( Import needed libraries )-----*/
//none
/*-----( Declare Constants and Pin Numbers )-----*/
#define potPin    A0  // In separate group of pins
#define photoPin  A1  // In separate group of pins
#define ledPin    13  // The onboard LED
#define RelayPin  3  // Control an active-low relay
/*-----( Declare objects )-----*/
//none
/*-----( Declare Variables )-----*/
int      potValue;    // Hold the values read in
int      photoValue;
boolean  lightsOn;         // Should Lights should be turned on?
boolean  Debug_ShowValues;  // Should values be sent to Serial Monitor?
// Boolean Values can be only "true" or "false"

void setup()   /****** SETUP: RUNS ONCE ******/
{
  pinMode(ledPin, OUTPUT);
  pinMode(RelayPin, OUTPUT);  
  lightsOn         = false; // Start with Lights off
  Debug_ShowValues = false; // Do not show. Change this to test.
  if (Debug_ShowValues == true)
  {  
    Serial.begin(9600);     //Start sending to "Serial Monitor"
    Serial.println("YourDuinoStarter Example: Automation Example.");
  }
}//--(end setup )---


void loop()   /****** LOOP: RUNS CONSTANTLY ******/
{
  ReadSensors();  // Each of these is a function defined below
  MakeDecisions();
  TakeActions();

  delay(1000);     // Wait 1 second
}//--(end main loop )---

/*-----( Declare User-written Functions )-----*/

void ReadSensors()
{
  potValue   = analogRead(potPin); //Read value
  photoValue = analogRead(photoPin); //Read value  
  if (Debug_ShowValues == true)
  {
    Serial.print("POT VALUE = ");
    Serial.print(potValue,DEC); // Print value
    Serial.print("  PHOTORESISTOR VALUE = ");
    Serial.println(photoValue,DEC); // Print value
  }
}  

/*---------------------------*/
void MakeDecisions()
{
  if (photoValue < potValue)
  {    
    lightsOn = true;  
  }
  else
  {    
    lightsOn = false;
  }

}

/*---------------------------*/
void TakeActions()
{
  if (lightsOn == true)  // "==" means "compare Equal"
  {
    digitalWrite(ledPin, HIGH);
    digitalWrite(RelayPin, LOW);  // This relay is Active LOW!    
  }
  else
  {
    digitalWrite(ledPin, LOW);  
    digitalWrite(RelayPin, HIGH);      
  }
}
//*********( THE END )***********



I don't like feeling confused! And when I'm confusing myself it's even worse!

Regards, Terry King terry@yourduino.com  - Check great prices, devices and Arduino-related boards at http://YourDuino.com
HOW-TO: http://ArduinoInfo.Info

grio

I am trying to get a better understanding of millis.

So I want a 1 hour timer to start. Once the timer has started and 1 hour has past I want the system to disable.

So millis is since the system start. So that would need to be 3,600,000 Milliseconds.


So Wouldn't I have to do

  systetimer=millis()

  if ((temp2 >= 155) && (timer == 0))
  {
    timer = 1;
    millitimer=millis()
    millitimer=millitimer+3600000;
  }

if (millitimer <= systetimer)
  {
    sysrun = 0;
    Serial.print("done");
  }

gfvalvo

I am trying to get a better understanding of millis.
Here ya go. It's one of the sticky posts right at the top of this very forum. First thing you should see when you arrive:
https://forum.arduino.cc/index.php?topic=503368.0

Coding Badly


Judicious use of the F-macro is always a good choice.


Go Up