Author Topic: Arduino Schematic HELP!  (Read 13526 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?
 

Online JPortici

  • Super Contributor
  • ***
  • Posts: 3461
  • 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: 7763
  • 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: 7763
  • 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: 9938
  • 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: 7763
  • 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: 12856
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: 6190
  • 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: 2155
  • 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: 2155
  • 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
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf