Author Topic: Arduino Schematic HELP!  (Read 13421 times)

0 Members and 1 Guest are viewing this topic.

Offline skillz21Topic starter

  • Frequent Contributor
  • **
  • Posts: 311
  • Country: au
Arduino Schematic HELP!
« on: September 30, 2016, 10:37:38 am »
Hey, guys!
I am trying to build a... thing ( :-//) that tracks motion and turns on some led lights when I step off my bed. I will be using two HC-SR04 ping sensors to track both sides of my bed. a problem that I am currently having is, I need to make a variable continually count down every second, and to stop when it reaches around -10. I want the LEDs to light up when the variable is above 0. how do I make the variable stop once it reaches -10? If you need more info, please ask :)

Thanks

BTW: I have had some issues on the forum when I wanted to get straight answers and people were trying to teach me C#. Guys, I'm doing that at the moment. I'm still only 12.
 

Offline skillz21Topic starter

  • Frequent Contributor
  • **
  • Posts: 311
  • Country: au
Re: Arduino Schematic HELP!
« Reply #1 on: September 30, 2016, 10:47:08 am »
Also, how do I make the value count down 1 every second?
 

Offline JPortici

  • Super Contributor
  • ***
  • Posts: 3452
  • Country: it
Re: Arduino Schematic HELP!
« Reply #2 on: September 30, 2016, 10:55:29 am »
BTW: I have had some issues on the forum when I wanted to get straight answers and people were trying to teach me C#. Guys, I'm doing that at the moment. I'm still only 12.
what else should have they done?

the logic is pretty straightforward
- you have a variable that should be updated every second
- if the variable value is greater than zero then switch on the leds
- otherwise (else), if the variable value is lower than ten then switch off the leds and stop counting
it's pretty basic [insert language name], nobody will seriously bother telling you because if you can't do/learn that on your own it probably means that you don't care (worst scenario) or you can't undestand what's going on anyway (best scenario)

note that the worst case was put first, for a very good reason. in case you genuinely don't know the language you must learn it before so you can understand what you are being answered

then you need to generate an event every second or to wait idling for a second, there are lots of ways to do that. for the second one you usually have ready made functions
« Last Edit: September 30, 2016, 10:57:01 am by JPortici »
 

Offline madires

  • Super Contributor
  • ***
  • Posts: 7693
  • Country: de
  • A qualified hobbyist ;)
Re: Arduino Schematic HELP!
« Reply #3 on: September 30, 2016, 10:59:33 am »
 

Offline skillz21Topic starter

  • Frequent Contributor
  • **
  • Posts: 311
  • Country: au
Re: Arduino Schematic HELP!
« Reply #4 on: September 30, 2016, 11:02:34 am »
wait, are you saying that I don't care about learning c#? I do, very much. please don't get the wrong impression just because I'm a kid :(
 

Offline skillz21Topic starter

  • Frequent Contributor
  • **
  • Posts: 311
  • Country: au
Re: Arduino Schematic HELP!
« Reply #5 on: September 30, 2016, 11:05:13 am »
For the 1s timer: http://playground.arduino.cc/Code/Timer1
Is it possible to do it just with a variable and not with a timer and a dedicted library.
 

Offline skillz21Topic starter

  • Frequent Contributor
  • **
  • Posts: 311
  • Country: au
Re: Arduino Schematic HELP!
« Reply #6 on: September 30, 2016, 11:16:44 am »
Here is the sketch that I made. please correct me in any place that I am wrong. also, the time reset is now a pushbutton, setting the value to 5. This sketch does not work.

int timeVal = 0;
void setup() {
  pinMode(8, OUTPUT);
  pinMode(A0, INPUT);
}

void loop() {
timeval - 1
if(timeVal > 0){
  digitalWrite(8, HIGH);
  }
else{
  digitalWrite(8, LOW);
  }
if(A0, HIGH){
  timeVal = 5;
  }
}

Also, I have gained most of my knowledge of C# from Scratch. I am trying to use the same... components of the programming, if that makes any sense.
 

Offline madires

  • Super Contributor
  • ***
  • Posts: 7693
  • Country: de
  • A qualified hobbyist ;)
Re: Arduino Schematic HELP!
« Reply #7 on: September 30, 2016, 11:29:19 am »
Is it possible to do it just with a variable and not with a timer and a dedicted library.

Using a timer for such things is a standard solution. Either you use an interrupt (with an interrupt handler) or have to check for the timer flag regularly. The library is convenient, but you can do it also directly. I'm sure you'll find a lot of examples when searching the internet.
 

Offline skillz21Topic starter

  • Frequent Contributor
  • **
  • Posts: 311
  • Country: au
Re: Arduino Schematic HELP!
« Reply #8 on: September 30, 2016, 11:34:52 am »
Is it possible to do it just with a variable and not with a timer and a dedicted library.

Using a timer for such things is a standard solution. Either you use an interrupt (with an interrupt handler) or have to check for the timer flag regularly. The library is convenient, but you can do it also directly. I'm sure you'll find a lot of examples when searching the internet.
ok :)
 

Offline Ammar

  • Regular Contributor
  • *
  • Posts: 154
  • Country: au
Re: Arduino Schematic HELP!
« Reply #9 on: September 30, 2016, 11:58:03 am »
Happy to give you some hints.

You can use the millis() function to find the time in ms since power on in milliseconds. Something like:

timeNew = millis();

See if you can use your loop and some if statements with the millis() function to solve your problem.

You could use a while loop with the condition you outlined to make the variable stop when you want. I hope that helps and good luck!
 

Offline Psi

  • Super Contributor
  • ***
  • Posts: 9889
  • Country: nz
Re: Arduino Schematic HELP!
« Reply #10 on: September 30, 2016, 12:03:48 pm »
I dunno if this will compile, but ive tried to do what you want using the millis function.


int TimeVal = 0;
unsigned long PreviousMS = 0;
unsigned long CurrentMS = 0;

void setup() {
  pinMode(8, OUTPUT);
  pinMode(A0, INPUT);
}

void loop()
{
    // loop runs VERY fast, we need to run some code slower to do the subtraction at 1 second.
    // Arduino has a millis() function to read the number off milliseconds
    // since the arduino started running.
    // We can store this number and then wait for the current time to equal the prev time plus 1000ms
    // then update the previous time to current time and let it repeat forever. This lets us run some code once a second.
   
    // get current time
    CurrentMS = millis();

    // if more than 1000 ms have past since previous time, do stuff.   
    if ( CurrentMS > (PreviousMS + 1000))
    {
         // code inside here happens once a second (1000ms)

         // only count down if value is larger than -10, once it reaches -10 it stops count and stays at -10
         if (TimeVal > -10)
         {
              TimeVal = TimeVal - 1;
         }

         // last thing we do is update our previous time variable to the current time
         // so the process can repeat again in another 1000ms.
         PreviousMS = CurrentMS;
    }



    // this is outside of our 1 second if statement, so it repeats very fast.
    // which is ok for setting leds on or checking inputs.

    if (TimeVal > 0)
    {
         digitalWrite(8, HIGH);
    } else {
         digitalWrite(8, LOW);
    }

    if (digitalRead(A0) == HIGH)
    {
         TimeVal = 5;
    }
}
« Last Edit: October 01, 2016, 11:06:59 pm by Psi »
Greek letter 'Psi' (not Pounds per Square Inch)
 

Offline madires

  • Super Contributor
  • ***
  • Posts: 7693
  • Country: de
  • A qualified hobbyist ;)
Re: Arduino Schematic HELP!
« Reply #11 on: September 30, 2016, 12:12:14 pm »
If you're using millis() you should read https://www.arduino.cc/en/Reference/Millis , especially:

Quote
Returns the number of milliseconds since the Arduino board began running the current program. This number will overflow (go back to zero), after approximately 50 days.

So you need to add an overflow detection.
 

Online Ian.M

  • Super Contributor
  • ***
  • Posts: 12805
Re: Arduino Schematic HELP!
« Reply #12 on: September 30, 2016, 01:16:06 pm »
Why would you want to learn C#?
That's one of Microsoft's .NET managed abominations and has *NOTHING* to do with writing Arduino code.

The Arduino 'programming language' is merely a very thin wrapper around GNU C++. 
See http://hackaday.com/2015/07/28/embed-with-elliot-there-is-no-arduino-language/ for the details.
 

Offline zapta

  • Super Contributor
  • ***
  • Posts: 6189
  • Country: us
Re: Arduino Schematic HELP!
« Reply #13 on: September 30, 2016, 01:32:33 pm »


The Arduino 'programming language' is merely a very thin wrapper around GNU C++. 
See [

The Arduino programming language *is* C++.

They just don't tell you that. It's a conspiracy to trick the population into learning embedded C++ programming.

 

Offline stj

  • Super Contributor
  • ***
  • Posts: 2153
  • Country: gb
Re: Arduino Schematic HELP!
« Reply #14 on: September 30, 2016, 02:11:02 pm »
Hey, guys!
I am trying to build a... thing ( :-//) that tracks motion and turns on some led lights when I step off my bed.

a pressure-mat or a pir sensor would do that!
 

Offline alsetalokin4017

  • Super Contributor
  • ***
  • Posts: 2055
  • Country: us
Re: Arduino Schematic HELP!
« Reply #15 on: September 30, 2016, 04:23:36 pm »
Here is the sketch that I made. please correct me in any place that I am wrong. also, the time reset is now a pushbutton, setting the value to 5. This sketch does not work.

int timeVal = 0;
void setup() {
  pinMode(8, OUTPUT);
  pinMode(A0, INPUT);
}

void loop() {
timeval - 1
if(timeVal > 0){
  digitalWrite(8, HIGH);
  }
else{
  digitalWrite(8, LOW);
  }
if(A0, HIGH){
  timeVal = 5;
  }
}

Also, I have gained most of my knowledge of C# from Scratch. I am trying to use the same... components of the programming, if that makes any sense.

Your code doesn't compile because of the errors in the lines I have bolded up above.

1. You have not initialized the variable "timeval".... you obviously mean to use "timeVal" but the compiler can't read your mind like I can   8)  . C++ variable names are case sensitive.
2. You did not terminate that line with a semicolon.
3. You have to explicitly read the A0 pin and use the conditional test == to compare to a value.
4. You need to tell it what to do if the conditional test fails (if A0 is LOW): include an else statement to reset timeVal.

So if you replace your line
timeval -1
with the line
timeVal -1;
or
timeVal --;

and the line
if(A0, HIGH){
with the line
if(digitalRead(A0)==HIGH){

and add the else statement
else timeVal=0;
after the final "if" statement

your sketch should compile and run.

But will it do what you intend?  I think you will need to insert some delay() or test for some millis() values as Psi suggests. (His suggested pseudocode makes the same error about the capital V in timeVal though.)

If you don't intend to stay in bed for more than 50 days, I wouldn't worry about overflowing the millis() timer.    :-DD
« Last Edit: September 30, 2016, 05:21:11 pm by alsetalokin4017 »
The easiest person to fool is yourself. -- Richard Feynman
 

Offline skillz21Topic starter

  • Frequent Contributor
  • **
  • Posts: 311
  • Country: au
Re: Arduino Schematic HELP!
« Reply #16 on: October 01, 2016, 12:37:43 am »
Why would you want to learn C#?
That's one of Microsoft's .NET managed abominations and has *NOTHING* to do with writing Arduino code.

The Arduino 'programming language' is merely a very thin wrapper around GNU C++. 
See http://hackaday.com/2015/07/28/embed-with-elliot-there-is-no-arduino-language/ for the details.
So what is the most similar programming language to Arduino?
 

Offline skillz21Topic starter

  • Frequent Contributor
  • **
  • Posts: 311
  • Country: au
Re: Arduino Schematic HELP!
« Reply #17 on: October 01, 2016, 12:40:10 am »
I dunno if this will compile, but ive tried to do what you want using the millis function.


int timeVal = 0;
unsigned long PreviousMS = 0;
unsigned long CurrentMS = 0;

void setup() {
  pinMode(8, OUTPUT);
  pinMode(A0, INPUT);
}

void loop()
{
    // loop runs VERY fast, we need to run some code slower to do the subtraction at 1 second.
    // Arduino has a millis() function to read the number off milliseconds
    // since the arduino started running.
    // We can store this number and then wait for the current time to equal the prev time plus 1000ms
    // then update the previous time to current time and let it repeat forever. This lets us run some code once a second.
   
    // get current time
    CurrentMS = millis();

    // if more than 1000 ms have past since previous time, do stuff.   
    if ( CurrentMS > (PreviousMS + 1000))
    {
         // code inside here happens once a second (1000ms)

         // only count down if value is larger than -10, once it reaches -10 it stops count and stays at -10
         if (timeval > -10)
         {
              timeval = timeval - 1;
         }

         // last thing we do is update our previous time variable to the current time
         // so the process can repeat again in another 1000ms.
         PreviousMS = CurrentMS;
    }



    // this is outside of our 1 second if statement, so it repeats very fast.
    // which is ok for setting leds on or checking inputs.

    if (timeVal > 0)
    {
         digitalWrite(8, HIGH);
    } else {
         digitalWrite(8, LOW);
    }

    if (digitalRead(A0) == HIGH)
    {
         timeVal = 5;
    }
}
Is it possible to make different loops? with different speeds?
 

Offline skillz21Topic starter

  • Frequent Contributor
  • **
  • Posts: 311
  • Country: au
Re: Arduino Schematic HELP!
« Reply #18 on: October 01, 2016, 12:43:37 am »
Hey, guys!
I am trying to build a... thing ( :-//) that tracks motion and turns on some led lights when I step off my bed.

a pressure-mat or a pir sensor would do that!
??? Thats true, but wouldn't that be more expensive?
 

Offline skillz21Topic starter

  • Frequent Contributor
  • **
  • Posts: 311
  • Country: au
Re: Arduino Schematic HELP!
« Reply #19 on: October 01, 2016, 12:50:32 am »
If you're using millis() you should read https://www.arduino.cc/en/Reference/Millis , especially:

Quote
Returns the number of milliseconds since the Arduino board began running the current program. This number will overflow (go back to zero), after approximately 50 days.

So you need to add an overflow detection.
wait... so can I change the value of millis?
 

Offline stj

  • Super Contributor
  • ***
  • Posts: 2153
  • Country: gb
Re: Arduino Schematic HELP!
« Reply #20 on: October 01, 2016, 01:45:35 am »
Hey, guys!
I am trying to build a... thing ( :-//) that tracks motion and turns on some led lights when I step off my bed.

a pressure-mat or a pir sensor would do that!
??? Thats true, but wouldn't that be more expensive?

pir sensors cost a couple of $ on ebay.
http://www.ebay.co.uk/itm/321614189142
 

Offline skillz21Topic starter

  • Frequent Contributor
  • **
  • Posts: 311
  • Country: au
Re: Arduino Schematic HELP!
« Reply #21 on: October 01, 2016, 01:53:59 am »
how would i use millis in a sketch like this:

const int Button = A0;
const int Light = 8;

int timeVal = 0;
int buttonstate = 0;

void setup() {
  pinMode(Light, OUTPUT);
  pinMode(Button, INPUT);
}

void loop() {
  buttonstate = digitalRead(Button);

if(timeVal > 0){
  digitalWrite(Light, HIGH);
  }
else{
  digitalWrite(Light, LOW);
  }
if(buttonstate == HIGH){
  timeVal = 5;
  }
}
 

Offline skillz21Topic starter

  • Frequent Contributor
  • **
  • Posts: 311
  • Country: au
Re: Arduino Schematic HELP!
« Reply #22 on: October 01, 2016, 01:58:44 am »
Hey, guys!
I am trying to build a... thing ( :-//) that tracks motion and turns on some led lights when I step off my bed.

a pressure-mat or a pir sensor would do that!
??? Thats true, but wouldn't that be more expensive?
Ok, I should have done a bit more research before i bought the two ping sensors. Also, I bought the ping sensors so if this project doesn't work, I could use the sensors for something like a... robot. Thanks for the suggestion though!  :)

pir sensors cost a couple of $ on ebay.
http://www.ebay.co.uk/itm/321614189142
 

Offline alsetalokin4017

  • Super Contributor
  • ***
  • Posts: 2055
  • Country: us
Re: Arduino Schematic HELP!
« Reply #23 on: October 01, 2016, 02:17:40 am »
Why would you want to learn C#?
That's one of Microsoft's .NET managed abominations and has *NOTHING* to do with writing Arduino code.

The Arduino 'programming language' is merely a very thin wrapper around GNU C++. 
See http://hackaday.com/2015/07/28/embed-with-elliot-there-is-no-arduino-language/ for the details.
So what is the most similar programming language to Arduino?

c++
The easiest person to fool is yourself. -- Richard Feynman
 

Offline alsetalokin4017

  • Super Contributor
  • ***
  • Posts: 2055
  • Country: us
Re: Arduino Schematic HELP!
« Reply #24 on: October 01, 2016, 02:19:16 am »
Is it possible to make different loops? with different speeds?
Yes, using interrupts, but we aren't quite ready for that yet.
The easiest person to fool is yourself. -- Richard Feynman
 

Offline alsetalokin4017

  • Super Contributor
  • ***
  • Posts: 2055
  • Country: us
Re: Arduino Schematic HELP!
« Reply #25 on: October 01, 2016, 02:50:27 am »
how would i use millis in a sketch like this:

const int Button = A0;
const int Light = 8;

int timeVal = 0;
int buttonstate = 0;

void setup() {
  pinMode(Light, OUTPUT);
  pinMode(Button, INPUT);
}

void loop() {
  buttonstate = digitalRead(Button);

if(timeVal > 0){
  digitalWrite(Light, HIGH);
  }
else{
  digitalWrite(Light, LOW);
  }
if(buttonstate == HIGH){
  timeVal = 5;
  }
}

OK... first let  me offer a suggestion. Change the Light pin to 13 for easy testing. Most Arduino boards already have a built-in LED and resistor on Pin 13. So it's easy to test this code on an Uno, all you need is a jumper to pretend to be a Button, connecting A0 to either 5V or GND.
Now... remember what I said about telling it what to do if the buttonstate is _not_ HIGH? You need to place an else statement after the last if statement. As it is, the Light pin stays high no matter what the buttonstate is.
So I've changed your code this slight amount:
Code: [Select]
const int Button = A0;
const int Light = 13;

int timeVal = 0;
int buttonstate = 0;

void setup() {
  pinMode(Light, OUTPUT);
  pinMode(Button, INPUT);
}

void loop() {
  buttonstate = digitalRead(Button);

  if(timeVal > 0)  digitalWrite(Light, HIGH);
  else  digitalWrite(Light, LOW);

  if(buttonstate == HIGH)  timeVal = 5;
  else timeVal = 0;
}
(scroll the code window if you can't see the whole sketch)

Notice with this sketch the Light state follows the buttonstate. When the button is HIGH (at 5V) the Light (LED on pin 13) is ON and when the button is LOW (at ground) the Light is OFF.  See what happens if you comment out that last "else" statement.

Now look at the way Psi used the millis() statement in his example. Millis() returns the number of milliseconds since the sketch started. So it's a constantly increasing number until it overflows at about 50 days and restarts from zero. The only time your sketch would need to worry about overflow is if the overflow instant happened during the sketch's time delay. As long as the entire time delay is within the 50 day cycle you are fine. Probably. (I'm sure the purists will be all over me for sloppy programming; yes, you should deal with the overflow gracefully, but there is plenty of time for that after you get something working! In 50 days time, you'll know how to deal with millis() overflow.)
So look at how Psi used the millis() to set a "start" time, test if "now" is a certain time later than "start", do something if it is, then reset "start" to the new "now". Play around with that in your sketch and see what you get.
« Last Edit: October 01, 2016, 02:52:16 am by alsetalokin4017 »
The easiest person to fool is yourself. -- Richard Feynman
 

Offline zapta

  • Super Contributor
  • ***
  • Posts: 6189
  • Country: us
Re: Arduino Schematic HELP!
« Reply #26 on: October 01, 2016, 02:51:22 am »
Why would you want to learn C#?
That's one of Microsoft's .NET managed abominations and has *NOTHING* to do with writing Arduino code.

The Arduino 'programming language' is merely a very thin wrapper around GNU C++. 
See http://hackaday.com/2015/07/28/embed-with-elliot-there-is-no-arduino-language/ for the details.
So what is the most similar programming language to Arduino?

It's not 'similar', the arduino programming language is C++, period. 

When you program in C++ you also use libraries, standard C++ libraries,  your own libraries, and other people libraries. In the case of Arduino the language is C++ and it comes packaged not just with the C++ libraries but also with libraries that were written by the Arduino people.
 

Offline exmadscientist

  • Frequent Contributor
  • **
  • Posts: 338
  • Country: us
  • Technically A Professional
Re: Arduino Schematic HELP!
« Reply #27 on: October 01, 2016, 03:46:52 am »
If you're using millis() you should read https://www.arduino.cc/en/Reference/Millis , especially:

Quote
Returns the number of milliseconds since the Arduino board began running the current program. This number will overflow (go back to zero), after approximately 50 days.

So you need to add an overflow detection.

On the subject of overflow detection, here's a kind-of-tested code snippet I wrote a while back for Arduino that gracefully handles overflows during delay generation:

Code: [Select]
void readDelay(unsigned long start, unsigned long delay_us)
{   
  if (start == 0)
    start = micros();
  unsigned long target = start + delay_us;
  //We might have overflowed, so do this carefully:
  if (target > start) {
    //no overflow
    while (micros() < target)
      continue;
  } else {
    //We overflowed: target is actual_target - 2^32, so current is larger than it
    //We can treat this case by being clever with signs: if interpreted as signed
    //  numbers, right now current < 0 and target > 0, with current increasing over
    //  time. So things will work out fine for us if we use signed comparisons here.
    //This hasn't been tested as I don't know how to get the Arduino into this
    //  state without watching it for an hour.
    Serial.print("Timer overflow!\n");
    while ((signed long)(micros()) < (signed long)target)
      continue;
  }
}

(I post this not because it's helpful to the original poster -- it's quite clearly not useful right now -- but because 1. I have an unhealthy fondness for cleverly efficient code that usually bites me in the rear when I have to debug my own creations and 2. I must admit to being disappointed, after clicking on this thread title, to see that it wasn't actually about the Arduino hardware schematics. There is a special place in my heart for schematics that randomly mix English and Italian, and no, it is not a nice, happy, fun place.)
 

Offline skillz21Topic starter

  • Frequent Contributor
  • **
  • Posts: 311
  • Country: au
Re: Arduino Schematic HELP!
« Reply #28 on: October 01, 2016, 07:48:56 am »
Why would you want to learn C#?
That's one of Microsoft's .NET managed abominations and has *NOTHING* to do with writing Arduino code.

The Arduino 'programming language' is merely a very thin wrapper around GNU C++. 
See http://hackaday.com/2015/07/28/embed-with-elliot-there-is-no-arduino-language/ for the details.
So what is the most similar programming language to Arduino?

It's not 'similar', the arduino programming language is C++, period. 

When you program in C++ you also use libraries, standard C++ libraries,  your own libraries, and other people libraries. In the case of Arduino the language is C++ and it comes packaged not just with the C++ libraries but also with libraries that were written by the Arduino people.
Isn't C++ one of the hardest programming languages?
 

Offline skillz21Topic starter

  • Frequent Contributor
  • **
  • Posts: 311
  • Country: au
Re: Arduino Schematic HELP!
« Reply #29 on: October 01, 2016, 08:08:59 am »
I did some more research into millis. Do I do it like, if any movement is found, the current millis value is recorded as a variable and the light turns on. then, once the millis value increases by about 30 seconds, the light is turned off. Can i do that?
 

Offline IanB

  • Super Contributor
  • ***
  • Posts: 11790
  • Country: us
Re: Arduino Schematic HELP!
« Reply #30 on: October 01, 2016, 08:18:24 am »
So you need to add an overflow detection.

On the subject of overflow detection, here's a kind-of-tested code snippet I wrote a while back for Arduino that gracefully handles overflows during delay generation:

There is no need to check for overflow when using unsigned counters. If you subtract "previous time" from "current time" in unsigned arithmetic the result will always be the desired elapsed time even if the timer has wrapped around in the mean time. See here for example: http://www.microchip.com/forums/m723128.aspx
 

Offline skillz21Topic starter

  • Frequent Contributor
  • **
  • Posts: 311
  • Country: au
Re: Arduino Schematic HELP!
« Reply #31 on: October 01, 2016, 09:08:01 am »
would this kind of thing work? (it doesn't work, it says that A0 was not declared in this scope.)

const int Button = A0;
const int Light = 8;

unsigned long time;

int timeVal = 0;
int buttonstate = 0;
int timeoffVal = timeVal + 30000

void setup(){
  pinMode(Light, OUTPUT);
  pinMode(Button, INPUT);
}
void loop(){
  buttonstate = digitalRead(Button);
 
  if(buttonstate == HIGH){
  timeVal = time;
  digitalWrite(Light, HIGH);
  }
  else{
  }

  if(timeoffVal = time) {
    timeoffVal = 0;
    digitalWrite(Light, LOW);
    }
}
 

Offline zapta

  • Super Contributor
  • ***
  • Posts: 6189
  • Country: us
Re: Arduino Schematic HELP!
« Reply #32 on: October 01, 2016, 09:51:58 am »
Why would you want to learn C#?
That's one of Microsoft's .NET managed abominations and has *NOTHING* to do with writing Arduino code.

The Arduino 'programming language' is merely a very thin wrapper around GNU C++. 
See http://hackaday.com/2015/07/28/embed-with-elliot-there-is-no-arduino-language/ for the details.
So what is the most similar programming language to Arduino?

It's not 'similar', the arduino programming language is C++, period. 

When you program in C++ you also use libraries, standard C++ libraries,  your own libraries, and other people libraries. In the case of Arduino the language is C++ and it comes packaged not just with the C++ libraries but also with libraries that were written by the Arduino people.
Isn't C++ one of the hardest programming languages?
It has many features, some are simple and some aren't. When you write, you choose what features to use.
 

Offline timb

  • Super Contributor
  • ***
  • Posts: 2536
  • Country: us
  • Pretentiously Posting Polysyllabic Prose
    • timb.us
Arduino Schematic HELP!
« Reply #33 on: October 01, 2016, 10:16:22 am »
Why would you want to learn C#?
That's one of Microsoft's .NET managed abominations and has *NOTHING* to do with writing Arduino code.

The Arduino 'programming language' is merely a very thin wrapper around GNU C++. 
See http://hackaday.com/2015/07/28/embed-with-elliot-there-is-no-arduino-language/ for the details.
So what is the most similar programming language to Arduino?

It's not 'similar', the arduino programming language is C++, period. 

When you program in C++ you also use libraries, standard C++ libraries,  your own libraries, and other people libraries. In the case of Arduino the language is C++ and it comes packaged not just with the C++ libraries but also with libraries that were written by the Arduino people.
Isn't C++ one of the hardest programming languages?

Not at all. See, you need to focus on learning the basic concepts of programming. Arduino can do that; there's plenty of tutorials both on the Arduino site and elsewhere. If you get stuck, remember, it's just C(++).

Once you know the fundamentals of programming, such as functions, loops, variables, registers, libraries, etc. than you can quickly pickup pretty much any language.

I got started programming in BASIC when I was about 10. I moved on to C when I was 12. This gave me a good set of fundamentals that have served me well the last 20 years.

Another thing, I think you're confused with this whole C/C++/C# thing:

First, there was C, which is a compiled procedural language. It's heavily used everywhere, both in the PC and embedded worlds.

Then there was C++, which is built on top of C. It adds object oriented functionality as well as other features to C. Most of the time you can mix C and C++ so long as you're using a CPP compiler.

C# is Microsoft's own unique take on C++. There is an embedded version of it, but I'd avoid it altogether.

Arduino is C++ plus a library that acts as a bit of an abstraction layer to make things easier. (For example, digitalWrite is just a function that's part of the Arduino library; it hides all the register manipulation you'd have to do otherwise.)

Finally, Arduino will teach you C/C++, but it won't teach you good habits, so keep that in mind.
« Last Edit: October 01, 2016, 10:19:42 am by timb »
Any sufficiently advanced technology is indistinguishable from magic; e.g., Cheez Whiz, Hot Dogs and RF.
 

Offline skillz21Topic starter

  • Frequent Contributor
  • **
  • Posts: 311
  • Country: au
Re: Arduino Schematic HELP!
« Reply #34 on: October 01, 2016, 10:32:12 am »
Why would you want to learn C#?
That's one of Microsoft's .NET managed abominations and has *NOTHING* to do with writing Arduino code.

The Arduino 'programming language' is merely a very thin wrapper around GNU C++. 
See http://hackaday.com/2015/07/28/embed-with-elliot-there-is-no-arduino-language/ for the details.
So what is the most similar programming language to Arduino?

It's not 'similar', the arduino programming language is C++, period. 

When you program in C++ you also use libraries, standard C++ libraries,  your own libraries, and other people libraries. In the case of Arduino the language is C++ and it comes packaged not just with the C++ libraries but also with libraries that were written by the Arduino people.
Isn't C++ one of the hardest programming languages?

Not at all. See, you need to focus on learning the basic concepts of programming. Arduino can do that; there's plenty of tutorials both on the Arduino site and elsewhere. If you get stuck, remember, it's just C(++).

Once you know the fundamentals of programming, such as functions, loops, variables, registers, libraries, etc. than you can quickly pickup pretty much any language.

I got started programming in BASIC when I was about 10. I moved on to C when I was 12. This gave me a good set of fundamentals that have served me well the last 20 years.

Another thing, I think you're confused with this whole C/C++/C# thing:

First, there was C, which is a compiled procedural language. It's heavily used everywhere, both in the PC and embedded worlds.

Then there was C++, which is built on top of C. It adds object oriented functionality as well as other features to C. Most of the time you can mix C and C++ so long as you're using a CPP compiler.

C# is Microsoft's own unique take on C++. There is an embedded version of it, but I'd avoid it altogether.

Arduino is C++ plus a library that acts as a bit of an abstraction layer to make things easier. (For example, digitalWrite is just a function that's part of the Arduino library; it hides all the register manipulation you'd have to do otherwise.)

Finally, Arduino will teach you C/C++, but it won't teach you good habits, so keep that in mind.
thanks, i get it now!
 

Online Ian.M

  • Super Contributor
  • ***
  • Posts: 12805
Re: Arduino Schematic HELP!
« Reply #35 on: October 01, 2016, 10:48:13 am »
Why would you want to learn C#?
That's one of Microsoft's .NET managed abominations and has *NOTHING* to do with writing Arduino code.

The Arduino 'programming language' is merely a very thin wrapper around GNU C++. 
See http://hackaday.com/2015/07/28/embed-with-elliot-there-is-no-arduino-language/ for the details.
So what is the most similar programming language to Arduino?

c++
C++ is 99.9% backwards compatible with code written in ANSI C.  *MOST* 'sketches'  are very 'C'-like and the most blatant' C++'ism you will see is libraries for various types of hardware organised as classes  and their member functions.   C++ is quite big and complex to learn so start with learning C to the ANSI/ISO C99 standard which will get you reasonably competent at slinging code together as quickly as possible then learn the rest of C++ as you need it.  C and C++ compilers targeting all major PC OSes are readily available. e.g you can use an x86 targeted GCC (basically the same compiler the Arduino IDE invokes, but with different support files and options) or if you are on Windows, there's the alternative of Microsoft Visual C++.   If you are dabbling in PC programming for the first time, stick to writing console applications.  The complex mesh of events and callbacks needed to support any GUI application is a steep learning curve, no matter what OS you are on.
 

Offline zapta

  • Super Contributor
  • ***
  • Posts: 6189
  • Country: us
Re: Arduino Schematic HELP!
« Reply #36 on: October 01, 2016, 12:27:55 pm »


... Finally, Arduino will teach you C/C++, but it won't teach you good habits, so keep that in mind.

That's a very good observation.

 

Offline alsetalokin4017

  • Super Contributor
  • ***
  • Posts: 2055
  • Country: us
Re: Arduino Schematic HELP!
« Reply #37 on: October 01, 2016, 01:43:16 pm »
would this kind of thing work? (it doesn't work, it says that A0 was not declared in this scope.)

const int Button = A0;
const int Light = 8;

unsigned long time;

int timeVal = 0;
int buttonstate = 0;
int timeoffVal = timeVal + 30000

void setup(){
  pinMode(Light, OUTPUT);
  pinMode(Button, INPUT);
}
void loop(){
  buttonstate = digitalRead(Button);
 
  if(buttonstate == HIGH){
  timeVal = time;
  digitalWrite(Light, HIGH);
  }
  else{
  }

  if(timeoffVal = time) {
    timeoffVal = 0;
    digitalWrite(Light, LOW);
    }
}
You left off a semicolon up in the declarations. This resulted in a weird error on my Arduino IDE. I didn't get the "A0 not declared in this scope" error though. When I added the semicolon the error went away and the sketch compiled.
 
However... it still doesn't do what you want or need.
 
First, you initialize a variable "time" but you don't give it a value. This usually results in a value of 0 but you really ought to do it explicitly to be sure. Then, in loop{} , nothing happens to change this value.

Next, in the last if statement you have the wrong syntax. You are assigning (=) rather than comparing (==). This doesn't result in a compiler error because it's OK to do this if it's what you really need, but in this case it isn't, and it always makes the "if" statement enclosing it evaluate as "true".

Ok, so those are the easy errors. Now let's flow through the loop{} after corrections.

First, read the button.
Now if the button is High, assign the value in "time" (0) to "timeVal". Now both "time" and "timeVal" contain the value 0.
Then turn ON the light.

Now test to see if "timeoffVal" is equal to "time", which of course is 0. But "timeoffVal" was declared as the initial value of "timeVal" (0) plus 30000, so the test fails and the next two statements aren't executed. So "timeoffVal" stays at 30000 and the light isn't turned off, so it stays ON.

Now go back to the top of the loop{} and read the button.
Now if the button is LOW, the first if statement fails so the next two statements aren't executed. "timeVal" stays at 0 and the light stays at whatever it was at before, which is ON.

Now test to see if "timeoffVal" is equal to "time", which of course is 0. But "timeoffVal" still has what it had the last time through which is 30000, so the test fails and the next two statements aren't executed. So "timeoffVal" stays at 30000 and the light isn't turned off.

And go to the top of the loop{} .....   

So when you start the sketch, with the button "unpressed" (LOW), the light will be off. Then when you press the button the light will turn on and stay on, and remain on no matter what else happens with the button, until the sketch is restarted.

If you include
digitalWrite(Light, LOW);
inside that empty else statement at least the light will turn off when the button goes LOW, but I think you want the light to stay on for a while once it has been turned on, even if the button is "unpressed" after being pressed.

So I think what you need to do is to test to see if some length of time has elapsed since the light was turned on, and if it has, turn off the light.





« Last Edit: October 01, 2016, 01:54:14 pm by alsetalokin4017 »
The easiest person to fool is yourself. -- Richard Feynman
 

Offline skillz21Topic starter

  • Frequent Contributor
  • **
  • Posts: 311
  • Country: au
Re: Arduino Schematic HELP!
« Reply #38 on: October 01, 2016, 08:43:55 pm »
Could i just do. When button pressed, light turns on, delay, light turns off?
 

Offline Psi

  • Super Contributor
  • ***
  • Posts: 9889
  • Country: nz
Re: Arduino Schematic HELP!
« Reply #39 on: October 01, 2016, 11:16:00 pm »
You can do anything you want.

Just store the Millis() value and then keep comparing this to the latest Millis() value.
When it has increased by 1000 then you know it's now 1 second later than before, and you can do some stuff etc..
« Last Edit: October 01, 2016, 11:18:23 pm by Psi »
Greek letter 'Psi' (not Pounds per Square Inch)
 

Offline zapta

  • Super Contributor
  • ***
  • Posts: 6189
  • Country: us
Re: Arduino Schematic HELP!
« Reply #40 on: October 01, 2016, 11:38:46 pm »
You can do anything you want.

Just store the Millis() value and then keep comparing this to the latest Millis() value.
When it has increased by 1000 then you know it's now 1 second later than before, and you can do some stuff etc..
s/comparing this to/subtracting this from/
 

Offline Bukurat

  • Regular Contributor
  • *
  • Posts: 65
  • Country: au
Re: Arduino Schematic HELP!
« Reply #41 on: October 01, 2016, 11:44:38 pm »
would this kind of thing work? (it doesn't work, it says that A0 was not declared in this scope.)

const int Button = A0;
const int Light = 8;

unsigned long time;

int timeVal = 0;
int buttonstate = 0;
int timeoffVal = timeVal + 30000

void setup(){
  pinMode(Light, OUTPUT);
  pinMode(Button, INPUT);
}
void loop(){
  buttonstate = digitalRead(Button);
 
  if(buttonstate == HIGH){
  timeVal = time;
  digitalWrite(Light, HIGH);
  }
  else{
  }

  if(timeoffVal = time) {
    timeoffVal = 0;
    digitalWrite(Light, LOW);
    }
}

The compiler is telling you it doesn't know what A0 is.   Have you included the Arduino.h  header file?

 

Offline zapta

  • Super Contributor
  • ***
  • Posts: 6189
  • Country: us
Re: Arduino Schematic HELP!
« Reply #42 on: October 02, 2016, 01:28:45 am »
The button is a digital in so you don't need the A notation. Just use pin number as you do for the light.
 

Offline alsetalokin4017

  • Super Contributor
  • ***
  • Posts: 2055
  • Country: us
Re: Arduino Schematic HELP!
« Reply #43 on: October 02, 2016, 01:48:32 am »
The compiler error is caused by the lack of a semicolon terminating the line

int timeoffVal = timeVal + 30000

in the sketch.

 If the "A0 not declared in this scope" error persists after the semicolon is put on the end of that line, check to see if the IDE is installed properly with all files where they are supposed to be in their directories. This should be done automatically but sometimes it is possible to "orphan" a sketch so that the header files are not accessible as they normally should be.

Yes, it's kind of a waste to use an analog input pin for a (digital) button press. But it does work.


Could i just do. When button pressed, light turns on, delay, light turns off?

Yes, you can. Have you tried writing a sketch to do this? It's pretty simple and can be done in a number of ways.
I've got one working but it won't do you any good if I just give it to you. You should try to figure it out yourself, using the hints I and others have given you.

The easiest person to fool is yourself. -- Richard Feynman
 

Offline skillz21Topic starter

  • Frequent Contributor
  • **
  • Posts: 311
  • Country: au
Re: Arduino Schematic HELP!
« Reply #44 on: October 03, 2016, 09:28:56 am »
ok, i'll try and do that. :)
 

Offline skillz21Topic starter

  • Frequent Contributor
  • **
  • Posts: 311
  • Country: au
Re: Arduino Schematic HELP!
« Reply #45 on: October 03, 2016, 10:00:38 am »
YAY!  :) This works!

const int Light = 8;
const int Button = A0;

int buttonstate = 0;

void setup() {
pinMode(Light, OUTPUT);
pinMode(Button, INPUT);
}

void loop() {
if(digitalRead(Button) == HIGH){
  digitalWrite(Light, HIGH);
  delay(5000);
  digitalWrite(Light, LOW);
  }
}

With Help, I did it alone! (that doesn't even make sense, Thanks for all the help guys, i learnt lots :)) You guys were right, it feels much better when you make the sketch alone!  :D However, the ping sensors should be here in a few weeks, i'll ask for help if i need it! Thanks for your help once more guys!  :-+
 

Offline skillz21Topic starter

  • Frequent Contributor
  • **
  • Posts: 311
  • Country: au
Re: Arduino Schematic HELP!
« Reply #46 on: October 03, 2016, 10:29:00 am »
Ok, new question, i ordered in a arduino nano. i tried to find a low power mode library or something. sadly, i couldn't find anything. Do you guys know of any ways to get lower power consumption without removing the leds? or am i better off using my Uno?
 

Offline madires

  • Super Contributor
  • ***
  • Posts: 7693
  • Country: de
  • A qualified hobbyist ;)
 

Offline alsetalokin4017

  • Super Contributor
  • ***
  • Posts: 2055
  • Country: us
Re: Arduino Schematic HELP!
« Reply #48 on: October 03, 2016, 05:43:26 pm »
YAY!  :) This works!

const int Light = 8;
const int Button = A0;

int buttonstate = 0;

void setup() {
pinMode(Light, OUTPUT);
pinMode(Button, INPUT);
}

void loop() {
if(digitalRead(Button) == HIGH){
  digitalWrite(Light, HIGH);
  delay(5000);
  digitalWrite(Light, LOW);
  }
}

With Help, I did it alone! (that doesn't even make sense, Thanks for all the help guys, i learnt lots :)) You guys were right, it feels much better when you make the sketch alone!  :D However, the ping sensors should be here in a few weeks, i'll ask for help if i need it! Thanks for your help once more guys!  :-+

That's great, congratulations! I guess the "A0 not declared in this scope" compiler error went away once you got the syntax errors straightened out.

As you've realized by now there are usually many ways to achieve the same goal in programming. So here's the sketch I wrote yesterday, to illustrate two ways to do the same thing. One is essentially the same as what you wrote using delay(), and the other shows (one way) how to use millis() and a comparison to implement the On-time delay.

Code: [Select]
/*
Get Out of Bed Light On with Off Delay
 
 Two versions: using delay() or using millis()
 See comments and comment/uncomment lines as needed to change version
 
*/

const int Button = 7;  // use digital IO pin for Button
const int Light = 13;  // use built-in LED for testing/debugging

unsigned long time;    // this line can be commented out for delay() version

boolean buttonstate = false;
long int timeOnVal = 5000; // On time in milliseconds is set here

void setup(){
  pinMode(Light, OUTPUT);
  pinMode(Button, INPUT);
  digitalWrite(Light, LOW);   // assures sketch starts with light Off
}
void loop(){
  buttonstate = digitalRead(Button);
  time = millis();            // comment out this line for delay() version
 
  if(buttonstate == true) {
    digitalWrite(Light, HIGH);
    //    delay(timeOnVal);      // uncomment this line for delay() version

    // comment out next line for delay() version
    while(millis() < (time + timeOnVal)) { }   // stay here and check time until On time is exceeded

  }
  else digitalWrite(Light, LOW);  // turn light Off if On time is exceeded and buttonstate is LOW
}

The easiest person to fool is yourself. -- Richard Feynman
 

Offline IanB

  • Super Contributor
  • ***
  • Posts: 11790
  • Country: us
Re: Arduino Schematic HELP!
« Reply #49 on: October 03, 2016, 06:24:40 pm »
The following line would not work correctly if millis() wraps back round to zero:

Code: [Select]
    while(millis() < (time + timeOnVal)) { }   // stay here and check time until On time is exceeded
However, it will be safe if written this way instead:

Code: [Select]
    while( (millis() - time) < timeOnVal ) { }   // stay here and check time until On time is exceeded
This is because subtractions between two unsigned values will always produce the expected positive difference.
 

Offline retrolefty

  • Super Contributor
  • ***
  • Posts: 1648
  • Country: us
  • measurement changes behavior
Re: Arduino Schematic HELP!
« Reply #50 on: October 03, 2016, 08:35:20 pm »
Ok, new question, i ordered in a arduino nano. i tried to find a low power mode library or something. sadly, i couldn't find anything. Do you guys know of any ways to get lower power consumption without removing the leds? or am i better off using my Uno?

 Well the power on led is not under any kind of software control so sleeping won't do anything for it.
 

Offline skillz21Topic starter

  • Frequent Contributor
  • **
  • Posts: 311
  • Country: au
Re: Arduino Schematic HELP!
« Reply #51 on: October 08, 2016, 04:59:49 am »
sleep mode will at least save a bit of power.
 

Online sleemanj

  • Super Contributor
  • ***
  • Posts: 3020
  • Country: nz
  • Professional tightwad.
    • The electronics hobby components I sell.
Re: Arduino Schematic HELP!
« Reply #52 on: October 08, 2016, 05:19:01 am »
Kill the leds.  If you are not using the onboard regulator cut it's legs (the cheap AMS1117 regs from china have a high quiescent even if you're powering after the reg).  If you don't need the ADC, shut it down.
~~~
EEVBlog Members - get yourself 10% discount off all my electronic components for sale just use the Buy Direct links and use Coupon Code "eevblog" during checkout.  Shipping from New Zealand, international orders welcome :-)
 

Offline skillz21Topic starter

  • Frequent Contributor
  • **
  • Posts: 311
  • Country: au
Re: Arduino Schematic HELP!
« Reply #53 on: October 08, 2016, 11:22:00 am »
ok, i might do that :D. i havent actually gotten the arduino yet, so i'll test it out when i get it.
 

Online Ian.M

  • Super Contributor
  • ***
  • Posts: 12805
Re: Arduino Schematic HELP!
« Reply #54 on: October 08, 2016, 06:27:37 pm »
Rather than butchering Arduinos, why not start from an ATmega328P chip, flash it with an Arduino bootloader then program it via a USB <=> logic level serial cable?

That way you don't have to hack off LEDs or regulators and you can easily isolate the MCU to check its power consumption.

There's even a page about doing this at Arduino.cc
 

Offline skillz21Topic starter

  • Frequent Contributor
  • **
  • Posts: 311
  • Country: au
Re: Arduino Schematic HELP!
« Reply #55 on: October 08, 2016, 11:27:00 pm »
Rather than butchering Arduinos, why not start from an ATmega328P chip, flash it with an Arduino bootloader then program it via a USB <=> logic level serial cable?

That way you don't have to hack off LEDs or regulators and you can easily isolate the MCU to check its power consumption.

There's even a page about doing this at Arduino.cc
Thats actually a good idea :)
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf