Jump to content

Arduinos


system 4-50

Featured Posts

3 minutes ago, 1st ade said:

Doubtless Microsoft thought the same...

Windows2.png.ed3f184236f88403c38a41fdbd625195.png

 

I have heard that one very expensive bit of engineering software once reported "Oh Dear, Something Bad has just Happened"

 

...............Dave

Link to comment
Share on other sites

4 hours ago, Jess-- said:

I catch the rollover (at the expense of a single inaccurate timing when it happens) by adding an extra check for millis being less than my previous recorded value or greater than the previous recorded value + the delay time

 

using the loop from blink without delay as an example my modification is in Red (original line in green).

 

void loop()
{
  // here is where you'd put code that needs to be running all the time.
 
  // check to see if it's time to blink the LED; that is, if the 
  // difference between the current time and last time you blinked 
  // the LED is bigger than the interval at which you want to 
  // blink the LED.
  unsigned long currentMillis = millis();
 
  if(currentMillis - previousMillis > interval) {

  if((currentMillis - previousMillis > interval) || (currentMillis < previousMillis)) {
    // save the last time you blinked the LED 
    previousMillis = currentMillis;   
 
    // if the LED is off turn it on and vice-versa:
    if (ledState == LOW)
      ledState = HIGH;
    else
      ledState = LOW;
 
    // set the LED with the ledState of the variable:
    digitalWrite(ledPin, ledState);
  }

 

There's a pretty print code block available on the forum that has numerous formatting/highlighting options.

 

I picked this example because it's shorter than the OP's code.

 

void loop()
{
  // here is where you'd put code that needs to be running all the time.
 
  // check to see if it's time to blink the LED; that is, if the 
  // difference between the current time and last time you blinked 
  // the LED is bigger than the interval at which you want to 
  // blink the LED.
  unsigned long currentMillis = millis();
 
  // original code
  // if(currentMillis - previousMillis > interval) {

  // Jess's modification
  if((currentMillis - previousMillis > interval) || (currentMillis < previousMillis)) {
    // save the last time you blinked the LED 
    previousMillis = currentMillis;   
 
    // if the LED is off turn it on and vice-versa:
    if (ledState == LOW)
      ledState = HIGH;
    else
      ledState = LOW;
 
    // set the LED with the ledState of the variable:
    digitalWrite(ledPin, ledState);
  }

 

Link to comment
Share on other sites

I must say trying to understand the listings and syntax has put me right off ever again trying programming. I even got as far as writing assembly language snippets on the BBC 6502 but the Andreno stuff seems gibberish to me. I had thought about trying to do a controller for watering my greenhouse but I don't think so. Just one hope left and that's  Pi done in basic  but even basic seems to have gone stupid since visual basic.

 

A serious question. If this Mills function rolls over after an undetermined amount of time, presumably because its a counter that runs out of space to store its value, is it not possible to set it to zero as part of the button press routine so it starts again?

 

 

Link to comment
Share on other sites

3 minutes ago, Tony Brooks said:

A serious question. If this Mills function rolls over after an undetermined amount of time, presumably because its a counter that runs out of space to store its value, is it not possible to set it to zero as part of the button press routine so it starts again?

Only if you have pressed the button before it runs out. There are ways round it, where it rolling over to zero doesn't matter.

Link to comment
Share on other sites

16 minutes ago, Tony Brooks said:

I must say trying to understand the listings and syntax has put me right off ever again trying programming. I even got as far as writing assembly language snippets on the BBC 6502 but the Andreno stuff seems gibberish to me. I had thought about trying to do a controller for watering my greenhouse but I don't think so. Just one hope left and that's  Pi done in basic  but even basic seems to have gone stupid since visual basic.

 

 

I have to agree, I have no clue what's going on and I used to spend far too many hours playing with basic, even starting to consider assembly,  admittedly I have drunk many many units of alcohol since

Link to comment
Share on other sites

31 minutes ago, Tony Brooks said:

I must say trying to understand the listings and syntax has put me right off ever again trying programming. I even got as far as writing assembly language snippets on the BBC 6502 but the Andreno stuff seems gibberish to me. I had thought about trying to do a controller for watering my greenhouse but I don't think so. Just one hope left and that's  Pi done in basic  but even basic seems to have gone stupid since visual basic.

 

A serious question. If this Mills function rolls over after an undetermined amount of time, presumably because its a counter that runs out of space to store its value, is it not possible to set it to zero as part of the button press routine so it starts again?

 

 

 

C is not a visually friendly language. When I was taught programming I was told that as computers advance the languages would become more English like. Basic was a good example. But C has gone in the opposite direction and is even more cryptic than assembler. Although I dislike it I do accept there is something attractive about its minimalism.

 

If you wrote your own timer code then yes, doing a reset to zero would be good, but the millis() thing is a sort of built in "system function" so might be used within other bits of code within the Arduino (in what passes for an "operating system") so a reset might well screw other things up.

 

.............Dave

  • Greenie 1
Link to comment
Share on other sites

6 minutes ago, dmr said:

 

C is not a visually friendly language. When I was taught programming I was told that as computers advance the languages would become more English like. Basic was a good example. But C has gone in the opposite direction and is even more cryptic than assembler. Although I dislike it I do accept there is something attractive about its minimalism.

 

If you wrote your own timer code then yes, doing a reset to zero would be good, but the millis() thing is a sort of built in "system function" so might be used within other bits of code within the Arduino (in what passes for an "operating system") so a reset might well screw other things up.

 

.............Dave

 

Thanks Dave, that makes sense.

Link to comment
Share on other sites

19 minutes ago, tree monkey said:

I have to agree, I have no clue what's going on and I used to spend far too many hours playing with basic, even starting to consider assembly,  admittedly I have drunk many many units of alcohol since

 

If you made a little crib sheet you could easily get into it.

Its not unlike Basic in some ways but instead of nice If and End If the code is split into blocks with funny brackets. C and its derivatives are often called "curly brace languages".

 

Every line must end in a semicolon, this is done just to cause you lots of trouble when you forget.

and instead of "If A=2"  C likes to say  "If (A==2)

 

..............Dave

Link to comment
Share on other sites

1 hour ago, dmr said:

I have the same issue when writing proper code for the PC and doing error handlers/error messages. Sometimes I think an error is so obscure that its quite likely nobody will ever see my lovely error message.

As someone who's code runs (as root) on every Android phone in the world, and half the cheap plastic home routers, I can confirm that, in the end,  _every_ error message will turn up. Probably in an email when you least expect it.

 

MP.

 

  • Greenie 2
Link to comment
Share on other sites

8 minutes ago, MoominPapa said:

As someone who's code runs (as root) on every Android phone in the world, and half the cheap plastic home routers, I can confirm that, in the end,  _every_ error message will turn up. Probably in an email when you least expect it.

 

MP.

 

That's ever so slightly impressive.

 

Wasn't the Atari renowned for off the wall error messages or was that an urban myth 

Link to comment
Share on other sites

12 minutes ago, dmr said:

 

If you made a little crib sheet you could easily get into it.

Its not unlike Basic in some ways but instead of nice If and End If the code is split into blocks with funny brackets. C and its derivatives are often called "curly brace languages".

 

Every line must end in a semicolon, this is done just to cause you lots of trouble when you forget.

and instead of "If A=2"  C likes to say  "If (A==2)

 

..............Dave

But I liked if and end, simple i like simple,  :)

problem is I have no use for it, no end product, nonetheless it is interesting 

Link to comment
Share on other sites

 

2 hours ago, tree monkey said:

Wasn't the Atari renowned for off the wall error messages or was that an urban myth 

Dunno about Atari. There is the famous "printer on fire"  message in Unix  https://en.wikipedia.org/wiki/Lp0_on_fire.

 

and it was a sad day when the historic unix error "Is not a typewriter" was changed to "Inappropriate ioctl for device". The programming symbol is still ENOTTY though.

 

MP.

  • Greenie 1
Link to comment
Share on other sites

37 minutes ago, MoominPapa said:

 

Dunno about Atari. There is the famous "printer on fire"  message in Unix  https://en.wikipedia.org/wiki/Lp0_on_fire.

 

and it was a sad day when the historic unix error "Is not a typewriter" was changed to "Inappropriate ioctl for device". The programming symbol is still ENOTTY though.

 

MP.

 

Not so nice to see such cryptic unintelligible to ordinary folk error messages have been carries into modern Linus distributions. Still noworse than Windows Error + a string of characters no end user understands.

Link to comment
Share on other sites

6 minutes ago, Tony Brooks said:

 

Not so nice to see such cryptic unintelligible to ordinary folk error messages have been carries into modern Linus distributions. Still noworse than Windows Error + a string of characters no end user understands.

Ordinary folk shouldn't ever see them: they're low-level kernel errors that should be dealt with long before they ever get to a user.

 

MP.

Link to comment
Share on other sites

5 hours ago, tree monkey said:

I have no clue what's going on

 

Believe me, in December, when I started this caper, nor did I have any idea what was going on. I thought i would plug the thing into a PC, download someone's ready made code from t'internet, tweak it a bit, then off we go.

 

A fortnight ago I was for giving up, and then I re read my code, realised that the relays needed a LOW to work them and how to sort the if  ... else arguments a bit better and now I have a thing which is protecting the boiler from boiling and giving me alarms when I want them for a total investment of £25 or less.  And its kept me off the streets. 

The first thing you need to do is invent a control problem that you would like to solve.

 

cheers.

Link to comment
Share on other sites

6 minutes ago, davidb said:

 

The first thing you need to do is invent a control problem that you would like to solve.

 

 


This is so true! I tried to learn C programming by reading a book. Complete waste of time, and disheartening. But once I had an actual and necessary project, it all fell into place.

  • Greenie 1
Link to comment
Share on other sites

1299533514_ScreenShot2021-02-02at17_18_40.png.f2e2f1594c8378eb8e9c44a71b9bbf9e.png

 

 

there is a useful looking timeout sketch in the elapsedMillis library which looks promising, but way above my pay grade at present,  I am getting there very slowly.

 

I have cleared out all the delays and various other redundant bits from my code and pasted the present version below with Jes's code tagged on at the bottom.

 

It all compiled fine and when I ran it, I could not do anything with the blink speed which seems to be roughly 4 per second. I can't see where the following is used, so no doubt it is part of the problem:

 

 const long interval = 1000;           // interval at which to blink (milliseconds) "
 
The processor seems to be wizzing along now when I watch the serial monitor.
 
I'm going to set up my spare UNO with a simulator circuit shortly because I don't want to upset the current working system.
 
Thanks for all your help and comments so far.
 
 
________________________________________________________  Feb2nd Sketch_____________
 
//    Remember outputs need to be low to energise the relays
 
 int myPin = A2; // INPUT Analogue Sensor
   int stoke = 2; // INPUT Stoking Temp Sensor (Bottom of store)
   int houseUFH = 3; // OUTPUT UFH Relay
     int annex = 5; // INPUT Annex Pump
      int house = 6; // INPUT House Pump
       int alarm = 7; // OUTPUT Alarm Light Relay
        int clok = 8; // INPUT from Clock
         int veryHot = 9; // INPUT  Boiler too hot
          int StoreUFH = 10; // OUTPUT Store UFH Relay
           int button = 11;   // alarm cancel button
           const int ledPin =  13;// the number of the LED pin
           
      int dt = 250; // Delay of 250 mSec

void setup() {
  Serial.begin(9600);
   
    pinMode(myPin,INPUT);          //  pin A2
     pinMode(stoke,INPUT);         // stat acts 70deg    pin 2
      pinMode(houseUFH,OUTPUT);   // Yellow Relay Wire  pin 3
        pinMode(annex,INPUT);      //    pin 5
         pinMode(house,INPUT);     //    pin 6
          pinMode(alarm,OUTPUT);           // Blue Relay Wire   pin 7
           pinMode(clok,INPUT);            // 5v from clock      pin 8
            pinMode(veryHot,INPUT);        // stat acts at 85deg   pin 9
             pinMode(StoreUFH,OUTPUT);   // Green Relay Wire  pin 10
              pinMode(button,INPUT);        //  push button      pin 11
               pinMode(ledPin,OUTPUT);      // Indicator LED     pin13
               
}

void loop() {



int analog = analogRead(myPin);     // Reads data from myPin (A2) pin and puts in analog Int
int readStoke = digitalRead(stoke); // Reads data from stoke (2) and puts in readStoke Int
int readAlarm = digitalRead(alarm); // Reads data from alarm (7) and puts in readAlarm Int
int readClock = digitalRead(clok);  // Reads data from clok (8) and puts it in readClock Int
int readHot = digitalRead(veryHot); // Reads data from veryHot (9) and puts it in readHot Int
int readUFH = digitalRead(StoreUFH);// Reads data from pin (10) and puts it in readUFH Int
int readBut = digitalRead(button);  // Reads data from button (11) and puts it in readBut Int
int ledState = digitalRead(ledPin);  // Reads data from pin (13)    and puts in ledState int

if (readClock == 1){
    digitalWrite(houseUFH,LOW);         // Turns House UFH on pin3  
    Serial.println("House UFH Turned On");
}
  if (readClock== 0){
  digitalWrite(houseUFH,HIGH);             // Turns House UFH off readClock == 0
  }

if (readStoke == 0){                    
  Serial.println("Boiler Requires Stoking");
    Serial.println(     );
     digitalWrite(alarm,LOW);            //  turns alarm light on    
      digitalWrite(ledPin,HIGH);         //  turns LED 13 on    
}
else{
  digitalWrite(ledPin,LOW);digitalWrite(alarm,HIGH);  //alarms off
}

if (readBut == 1){
  Serial.println("button pressed");
  }
  if (readHot == 1){
  digitalWrite(StoreUFH,LOW);       // Turns Store Pump relay on pin 10
  Serial.println("Store Pump Turned On");
  }
else{
  digitalWrite(StoreUFH,HIGH);      // Turns Store Pump Relay off
}
Serial.print("Stoke = ");
Serial.println(readStoke);

Serial.print("Alarm = ");
Serial.println(readAlarm);

Serial.print("UFH = ");             // sending to serial Monitor
Serial.println(readUFH);

Serial.print("Storing = ");             // sending to serial Monitor
Serial.println(readHot);



// here is where you'd put code that needs to be running all the time.
 
  // check to see if it's time to blink the LED; that is, if the
  // difference between the current time and last time you blinked
  // the LED is bigger than the interval at which you want to
  // blink the LED.
 
  unsigned long currentMillis = millis();
 
// Generally, you should use "unsigned long" for variables that hold time
// The value will quickly become too large for an int to store

unsigned long previousMillis = 0;        // will store last time LED was updated

// constants won't change:

const long interval = 1000;           // interval at which to blink (milliseconds)

if((currentMillis - previousMillis > interval) || (currentMillis < previousMillis)) {
    // save the last time you blinked the LED
   
    previousMillis = currentMillis;  
 
    // if the LED is off turn it on and vice-versa:
   
    if (ledState == LOW)
      ledState = HIGH;
    else
      ledState = LOW;
 
    // set the LED with the ledState of the variable:
    digitalWrite(ledPin, ledState);
  }


}

 

 

Link to comment
Share on other sites

This line is where interval is used

 

if((currentMillis - previousMillis > interval) || (currentMillis < previousMillis)) {
 

aka “if the difference between current millis() and previous one is greater than interval (currently set to 1000), or if the current millis() is less than the previous one (due to rollover) then ...”

 

So you would expect the if statement to be true once per second.

 

have you tried increasing interval to say 2000 and see if the flashing is slower?

Edited by nicknorman
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.