Button To Run A File

Status
Not open for further replies.
i researched a lot of the code and i can't make heads or tails out of some of it, I don't even see how it's supposed to work. Like none of the code does anything with the pin assigned to the signal, and what's void rpm and attachintterupt.. Its just over my head. It doesn't do anything, can someone help me adapt it to the teensy? or atleast point me to some simple code to get the sensor to output something.. it's a rotor with a hall effect sensor.. thanks
 
Last edited:
i tried getting the led to change state with this code. didn't work.. the hallPin just stays HIGH.


int hallPin = 3;
int ledPin = 13;


void setup()
{
pinMode(ledPin, OUTPUT);
pinMode(hallPin, INPUT_PULLUP);
}

void loop(){
if (digitalRead(hallPin) == LOW) {
digitalWrite(ledPin, HIGH);
} else {
digitalWrite(ledPin, LOW);
}
}
 
Last edited:
You may want to google "attach interrupt"
If you don't undestand that then you may want to go and read through a few Arduino tutorials on the Adafruit website that deal with interrupts and explain the concept of an interrupt.

Continuing to buy hardware that you don't understand and trying to connect it to a microcontroller board you don't understand programming it with a programming language that you don't understand is not going to get you anywhere.
You really need to start doing your own homework!
 
You may want to google "attach interrupt"
If you don't undestand that then you may want to go and read through a few Arduino tutorials on the Adafruit website that deal with interrupts and explain the concept of an interrupt.

Continuing to buy hardware that you don't understand and trying to connect it to a microcontroller board you don't understand programming it with a programming language that you don't understand is not going to get you anywhere.
You really need to start doing your own homework!

I did google attachinterupt, i didn't understand it. Maybe you could present some code applicable to this situation to help me.. thanks
 
What exactly is it you are having the probelm with ? Is it a specific term etc. ?

That Arduino article does shed a little light on it.. I don't understand:
attachInterrupt(THIS0THIS, blink, CHANGE)

is it a pin number? if so what should it be connected to? if not what does it represent?

edit:
should that pin be connected to the sensor's signal output?
 
Last edited:
I believe there is a differnce between "standard" Arduino boards and the Arm based Due and Teensy boards.
For the Teensy boards the first parameter is the pin the interrupt will be attached to. As I think you can attach an interrupt to almost any pin.
That cannot be done on the Atmel based Arduinos. They tie specific interrupts to specific pins. As such for"normal" Arduinos that first parameter is the interrupt, and not the pin.

Others are more qualified to comment on this ;-)
 
could someone please tell me how to properly write this for teensy 3.1?

volatile int NbTopsFan; //measuring the rising edges of the signal
int Calc;
how does this?
int hallsensor = 2; //The pin location of the sensor

void rpm () //This is the function that the interupt calls
{
NbTopsFan++; //This function measures the rising and falling edge of the hall effect sensors signal
}

// The setup() method runs once, when the sketch starts
void setup() //

{
pinMode(hallsensor, INPUT); //initializes digital pin 2 as an input
Serial.begin(9600); //This is the setup function where the serial port is initialised,
Find this??
attachInterrupt(0, rpm, RISING); //and the interrupt is attached
}

// the loop() method runs over and over again,
// as long as the Arduino has power

void loop ()
{
NbTopsFan = 0; //Set NbTops to 0 ready for calculations
sei(); //Enables interrupts
delay (1000); //Wait 1 second
cli(); //Disable interrupts
Calc = (NbTopsFan * 60 / 5.5); //(Pulse frequency x 60) / 5.5Q, = flow rate in L/hour
Serial.print (Calc, DEC); //Prints the number calculated above
Serial.print (" L/hour\r\n"); //Prints "L/hour" and returns a new line
}



edit:
And also please point out anything that rewritten for the teensy.

Sorry for not being a wiz-bang hacker, i would like to learn though.. thanks in advance
 
Last edited:
you know what forget all that.. going back to:

int hallPin = 3;
int ledPin = 13;

void setup()
{
pinMode(ledPin, OUTPUT);
pinMode(hallPin, INPUT);
}

void loop(){
if (digitalRead(hallPin) == LOW) {
digitalWrite(ledPin, HIGH);
} else {
digitalWrite(ledPin, LOW);
}

I cannot make the pin == LOW when spinning the rotor, it just stays high. Can someone suggest a way to figure out how to make the hall effect sensor == LOW? Maybe what resistor values to try, or how to wire it up to make it respond? I got the sensor from ebay for 3 bucks(China), and cannot find a datasheet.
 
you know what forget all that.. going back to:

int hallPin = 3;
int ledPin = 13;

void setup()
{
pinMode(ledPin, OUTPUT);
pinMode(hallPin, INPUT);
}

void loop(){
if (digitalRead(hallPin) == LOW) {
digitalWrite(ledPin, HIGH);
} else {
digitalWrite(ledPin, LOW);
}

I cannot make the pin == LOW when spinning the rotor, it just stays high. Can someone suggest a way to figure out how to make the hall effect sensor == LOW? Maybe what resistor values to try, or how to wire it up to make it respond? I got the sensor from ebay for 3 bucks(China), and cannot find a datasheet.

So,
you have a simple program and you worry about if sensor is working or not.
approach:
1) remove sensor and see of by connecting hallPin to low led lights up. If so, hardware does not work as expected. If not, SW is not proper designed (maybe you need a delay(10) in your loop)
2) measure voltage output of sensor when spinning rotor. Is it digital output (0 or 3.3V) or analog (varying from 0 to 3.3 V)?

If hardware is digital with proper voltage and software is correct, then connect the two pieces.
if hardware is analog, then you have to use analog input, that is, rewrite your code.
 
thanks WMXZ, i understand what you're saying.. youre not gonna believe this but about the time i was going to measure the sensor i noticed the GRD connected to the V+ and the GRD was connected to the V+.. JEEZ! ..i guess i attribute it to oh btw these components have a common anode and having to wire some stuff backwards.. anyway thanks for the great advice plus i did learn about attachInterrupt().. turns out the code above does work if you connect to the interrupt pin and set the sensor pin to the same.. like:

int hallpin = 1;
attachInterrupt(1, blah blah);

thanks i really appreciate everything.. I'm building a weather station, getting pretty close to being finished..
 
Yes, this is a smart move, to work with the simplest code first. Throwing more complex stuff in the mix, like attachInterrupt, should only be done if you really need its more advanced capability. Often the simplest way is the best way!

you know what forget all that.. going back to:

int hallPin = 3;
int ledPin = 13;

void setup()
{
pinMode(ledPin, OUTPUT);
pinMode(hallPin, INPUT);
}

void loop(){
if (digitalRead(hallPin) == LOW) {
digitalWrite(ledPin, HIGH);
} else {
digitalWrite(ledPin, LOW);
}

I cannot make the pin == LOW when spinning the rotor, it just stays high.

Well, it looks like you're on the right track here, at least software-wise.

You're going to need a voltmeter for troubleshooting. Sounds like you might already be using one? Connect it to pin 3.

I'd recommend first testing with just a wire, touching between pin 3 and GND. You can use the voltmeter to check that the voltage on pin 3 really is high without the wire, and goes low when you touch the wire. The LED should also change.

This is the way you troubleshoot electronic projects, by isolating and testing smaller pieces to confirm which parts actually are working.

After you get the LED to respond with a wire touching the pin, then you're ready to troubleshoot the hall effect sensor. Again, think about doing the simplest test of just that part. Maybe just touch a magnet onto the sensor, since of running it with a spinning machine. Can you get the voltage to change on pin 3 when the magnet comes in contact (and stays there, so you can easily see the effect on the slow reading of a normal voltmeter)?
 
i mean what if you didnt want to write:

blink led
pinjondoe do this
blink led
pinjondoe do this
blink led
pinjondoe do this
stop blink led

is there a way to write?

blink led
pinjondoe do this
pinjondoe do this
pinjondoe do this
stop blink led
 
Wow Josh. I know you're frustrated, but come on. If you want to figure this stuff out, keep a clear head and try to focus!

For now, I'd recommend NOT using attachInterrupt(). Interrupts are a somewhat advanced feature. If you don't need them, spare yourself the grief of making your project more complex than it needs to be.

So that leaves us with "void", on your list of stuff not understood (msg #62).

All programming involves a certain amount of special syntax. You can spend a tremendous amount of time trying to learn every last detail, but that's very deep pit to jump into. Sometimes it's more effective to just accept the language has some arcane stuff and run with the examples.

But specifically about "void", programs are composed of functions. Arduino sketches always have at least 2 of them, called setup() and loop(). Functions are usually written with the parens "()", whereas other stuff isn't, so when you look at some code, the words that are functions can be seen by looking for the parens.

The setup() and loop() functions do not receive any data input and they generate no data output. Other functions can. The word "void" before the function name means it does NOT output (or "return") any data. The lack of any stuff between the parens "()" means it doesn't receive any input from the rest of the program. It's just a block of code that does something, without specifically getting any input or generating any output.

If you're just getting started, you're probably not about to go create a 3rd function, not to mention get the details right for it to receive inputs and return an output. Remember what I said about sometimes just running with existing examples. Well, if you did worry about such things, it'd look like this:

Code:
int myfunction(char a, int b) {
  // my code
  return 24;  // or whatever number, maybe from a variable
}

This myfunction() gets 2 inputs, which are type char (a character) and int (an integer) and when it's done, it returns an integer to the rest of your program.

If you wanted to get those 2 inputs, but not return anything back, you'd write it like this:

Code:
void myfunction(char a, int b) {
  // my code
}

The word "void" means the function will NOT give any return data.

These details are important when you write more complex code. In your program, you used digitalWrite() and digitalRead(). Both take inputs. The digitalRead() returns data, which is how it tells you what the pin actually was. The digitalWrite() has a return type of "void", because it doesn't give you anything back.

So, that's what "void" means. Hopefully this helps, at least with this specific question.

Somehow, you're going to have to manage to deal with using this unfamiliar stuff. If you do enough, you'll learn as you go. There are a LOT of these little arcane details. You can ask questions here, and there's a lot of good resources online and also in many good books. But ultimately, you're going to have to somehow emotionally manage how to make progress without getting so overwhelmed. Keep focused on your goals, and good luck!
 
Thanks Paul.. It is new to me but I have already learned a little, enough to build a garden module(totally finished, works perfectly).

I was so excited after getting the flow meter working i went crazy.. most of that stuff is still over my head but i will absorb it as i can.. thank you for your help..
 
hey, been a while.. need advice on how to write something..

i have 2 temperature sensors, one for inside temp, the other for outside temp.. i want turn on a fan when the inside is hotter than outside.. aside from the code that makes the sensors work this is what i'm using..

if (temp1 > temp2) {
turn fan on;
} else {
turn fan off;
}

and that works but not great because when the temps are the same, or close to the same, the fan can come on and go off every time the delay is evaluated which is hard on the fan motor.. i've been racking my brain how to make a buffer, or like make some categories so it couldn't come on and go off when the inside and outside temps are close..

has anyone ever done anything like this? any ideas greatly appreciated..
 
Two ideas that might help. First, you might want to make the fan only go on when the difference between the inside and outside temperature is greater than a threshold. You might want the fan only to go on if it is 10 degrees colder outside than inside.

Code:
if (temp1 - temp2 > 10) {
  turn fan on;
} else {
  turn fan off;
}

And you might want to add some hysteresis. This is a fancy word for saying, you don't want the fan to change state too rapidly. You might want the fan to turn on/off no more than once in an hour. To do this, your code would need to store in a variable the time whenever it turns the fan on or off. You'd need to do a test like the one above for temperature to determine if at least an hour has passed since the last change. There's a special feature of the Teensy code that makes measuring elapsed time easy. You can find more information here: https://www.pjrc.com/teensy/td_timing_elaspedMillis.html

[Edit: mlu's code is better.]
 
Last edited:
A possible answer is to go back to the issue of not wanting the motor to flick on and off within a short time period, and so just prevent it switching by inserting a delay() after an action. So it will only be ready to act upon the sensor info after a time gap. It bit simplistic, but does what you want. The time counter, and use of "state" variable seems best.
 
The simple logic, adding what is technically called hysterisis, is as follows
Code:
if (temp1-temp2>0) TurnFanOn;

if (temp1-temp2<-5) TurnFanOff;

Note that there is not any else statement, and that if temp1-temp2 is between -5 and 0 nothing changes.
So the fan will turn when temp1 gets above temp2, then it will stay on until temp1 is 5 degrees below temp2, and then it will stay off until tem1 is again above temp2.
 
thanks, great advice works great.. now i'm trying to add a time stamp to serial monitor using time.h lib not having the best luck.. if i add all that code from the example it does print the time and date but it's incorrect and when i try to adjust it like going hour = hour + 1 or int hour1 hour1=hour+1 print hour1 it kicks out out to the debugger and says something stupid like You cannot type hour1" .. anyways what the simplest way to pull the time from serial port and print it in the serial monitor? or maybe just adjust the time? thanks
 
josh, I'm having a little trouble following you here, but I'll take a guess...

If you want to add time and date stamps to log entries generated by the Teensy, the Teensy needs to get the correct time from somewhere...

If you compile and download a sketch, the time will be almost right because the clock on the Teensy is initially set to the time the code was compiled. If the teensy gets reset or powered down, it will start over again at the compile time. This could make you crazy if you didn't know what was going on.

So, you need to arrange for the Teensy to get the correct time every time it starts. There are a bunch of ways of doing this.


  • Make the Teensy prompt you to type in the time over a serial connection every time it starts.
  • Make the PC tell the Teensy the time over the serial connection, but you don't trust the laptop?
  • Add an Ethernet module to the Teensy and have it get the time over the network using NTP or something simpler.
  • Add a GPS module. All GPS modules need to be able to get very accurate time from the satellites.
 
  • Make the Teensy prompt you to type in the time over a serial connection every time it starts.

is there an example code in auduino? if not what would i use to input? thanks

also, do you have to solder a crystal on the bottom for it to being able to use time.h lib??
 
okay i have another question..

how would i write the code to make the led blink faster as a timer was about to expire.. like:

digitalwrite led high
delay 1000
digitalwrite led low
delay 999
digitalwrite led high
delay 998

and so so.. i flunked out after goto hello world but would it be something like:

for x = 1000 - 1
delay x

any help appreciated
 
You can control the time ON and the time OFF and the time between changes in OFF>ON and ON>OFF. you can only change these periods so short before it won't be visible.

How long is your overall time scale and when do you want to start building the sense of urgency.

If you can make of list of these changes as you want to seem them effected that will give you an idea of how to program it. Backing up from time zero you'll want to step through the changes in order to be effective and eye catching and informative.

If you do it with delay() you'll need to hit your marks exactly - and then the processor will be locked up not seeing your button com in. You should get comfortable with elapsedMillis or other ways to watch time pass and then conditionally act at key points.
 
Status
Not open for further replies.
Back
Top