Show Posts
Pages: [1] 2 3 ... 15
1  Using Arduino / Programming Questions / Re: Hall Effect Speed and Distance Sensor Code on: April 20, 2014, 01:44:58 pm
and for the speed you can also calculate it with a simple equation, something like:
Speed = Distance / Time

So if you know the circumference of the wheel and you masure how long it took it to make one turn, then you can also calculate the speed and display it on your lcd.
2  Using Arduino / Programming Questions / Re: Hall Effect Speed and Distance Sensor Code on: April 20, 2014, 08:50:51 am
if you have one magnet on the wheel and the sensor on the frame, each time the magnet passes the wheel you will have traveled the same distance as the circumference of your wheel.
3  Using Arduino / Programming Questions / Re: Toggle between two modes or tasks on: April 19, 2014, 06:21:33 am
when you press the button set the buzzer to buzz and save the time (using the millis() ) into a variable, called maybe something like buzzBegin.

Then on the loop() check if your interval (1 second) as passed, something like
if(millis() - buzzBegin >= interval) {
   // stop your buzzer

this will help you avoid the use of the delay() function.
4  Using Arduino / Programming Questions / Re: "Cutting off" circuit with 2 momentary switches on: April 19, 2014, 05:58:20 am
was it something like this that you were looking for?
   * led to Arduino pin 4
   * button1 to Arduino pin 8
   * button2 to Arduino pin 9
      (using a 10k pull-up resistor for both buttons)

int led = 4;
boolean ledState = LOW;
long ledStart;

int interval = 2000;

int but1 = 8;
int but1State;
int lastBut1State = HIGH;

int but2 = 9;
int but2State;
int lastBut2State = HIGH;

void setup() {

void loop() {
  // read buttons
  but1State = digitalRead(but1);
  but2State = digitalRead(but2);

  // if button1 is pressed
  if(but1State != lastBut1State && but1State == LOW) {
    ledState = HIGH;      // set led state to HIGH
    ledStart = millis();  // save starting time

  // if button2 is pressed
  if(but2State != lastBut2State && but2State == LOW) {
    ledState = LOW;      // set led state to LOW

  // check if 2 second have passed since led was turned on
  if(millis() - ledStart >= interval){
    ledState = LOW;      // set led state to LOW
  digitalWrite(led,ledState);  // write the ledState
  // update lastButton variables
  lastBut1State = but1State;
  lastBut2State = but2State;

i didn't try it, but give it a go and make any changes you need.

as you can see i am not using the delay() function. i am using a very useful technic to see if a set amount of time as gone by which you can learn how to use here:

Good lock!
5  Using Arduino / Programming Questions / Re: if else or switch case? on: April 19, 2014, 02:29:01 am
i would imagine that with so many if statements things would be better with a nicelly organized switch case.
.. but i don't really have an explanation for that. it is just intuition, or something...

But a quick search here in the forum took me to another thread with has a good amount of interesting information about this subject:,44519.0.html
You can read though it, but i think in the end they found out that actually if statements were smaller and faster on Arduino.
...but i have to tell you that i didn't read it whole, i just "jumped over it". I need to go back some other time with a bit more time!

Would love to see to what conclusion you come. I have often thought about this also...
6  Using Arduino / Programming Questions / Re: Toggle between two modes or tasks on: April 19, 2014, 02:20:19 am
you are missing an opening curly bracket " {" after this line:
if(currState != prevState)

and then you also need to add one more to the end of the void loop.

i think also that maybe you should change this line:
prevState = currState;
to a different place. Where you have it now, it is just working if the button is pressed. But you also want it to update the prevState on each pass, even if the button isn't pressed.

So maybe something like this would work:
const int swPin = 2;
const int ledRd = 9;
const int ledGn = 8;

int prevState = HIGH;
int currState;

byte mode = 0;

void setup() {
  pinMode(swPin, INPUT_PULLUP);
  pinMode(ledGn, OUTPUT);
  pinMode(ledRd, OUTPUT);

void loop() {
  int currState = digitalRead(swPin);
  if(currState != prevState) {
    if(currState == LOW) {
      mode = !mode; // Toggle mode
      digitalWrite(ledRd, HIGH);
      digitalWrite(ledGn, LOW);
    else {
      digitalWrite(ledRd, LOW);
      digitalWrite(ledGn, HIGH);
  prevState = currState;
7  Using Arduino / Programming Questions / Re: Serial.print() on: April 18, 2014, 03:04:42 pm
you can alway try something like


void setup() {

void loop() {

just to check that everything is in order...
8  Using Arduino / Programming Questions / Re: Serial.print() on: April 18, 2014, 02:01:25 pm
i think what they mean is that we can maybe help you further if you post the code you are using (using the CODE tags!)
9  Using Arduino / Programming Questions / Re: Where am I going wrong with the no delay method? on: April 18, 2014, 11:24:24 am

You do know that min and max use the same basic code if not more?

you think that mapping the value of that analogRead would use more code?
i meant something like this:
int interval = map(analogRead(0), 0, 1023, 5, 255);
the "5" and "255" could easily be replaced by any values he wishes to use as minimum and maximum Interval times.
10  Using Arduino / Programming Questions / Re: Expected Initializer before 'int' Error on: April 18, 2014, 11:13:10 am
very often it is just that we forgot a ";" at the end of a line...
but in your case i think it is because you often have two words as a variable name, something like:
int Red Pin 9;
this doesn't work, you need to have a variable name in just one word, something like:
int redPin = 9;
correct all those variable names and check if it works better...

what is this
  for(int i=0;i< 3;i++);
    pinMode (ledDigitalOne[i],INPUT) ;//SET THE RGB LED PINS AS OUTPUTS
  Serial.begin(9600); // start the serial connection with the battery
between your setup() and your loop()? Is it working like you intended it? It seems you have code outside from any functions. It maybe be from the formatting, but maybe you can also check that out...
(ah, and CMD+T auto formats your code...    smiley-wink   )

Why are you using Serial.begin() twice?

Good luck!
11  Using Arduino / Programming Questions / Re: Where am I going wrong with the no delay method? on: April 18, 2014, 11:06:12 am
in your void flash, instead of
interval = analogRead(0)/10; //read pot value, divide by 10 to reduce range of flash sweep speed
  if (interval <5) // delays of less that 5 create too fast of a flash sweep. Set to minimum of 5
    interval = 5;
you could maybe map the value, like this you can easily control it's min and max values. No need for that if statement.

on your code there are things like this:
if (ledState = LOW)
but i imagine you want
if (ledState == LOW)

== is the comparison operator. it is kind of like "is it equal to?"
= is something different. it is an assignment operator.  Makes the left variable equal to the right one.

The idea behind the BlinkWithoutDelay is quite simple, but i remember it also took me some time to understand it.
The basic idea is that for example instead of a delay(100) (which would "pause" the whole Arduino sketch for 100 miliseconds and not allow for anything to happen in this period) we can use the millis() function to see if a predetermind amount of time as gone by.
So the easiest way to change your original sketch (which works for you) would be to just replace those delay() with that millis() technic.

With that first code you posted, the one using delay(), was it actually working like you wanted? Or was it just not giving you any errors?

Well, just a couple of ideas...
12  Using Arduino / Programming Questions / Re: PWM preset state issues ("not declared in this scope") on: April 18, 2014, 04:59:27 am
when posting some code, it would be best to post it between CODE tags. You can press the NUMBER SYMBOL ( # ) button up there on the format icons and then insert your code between the tags, something like this:

[ code]
your code here
[ /code]

and it will look like:
your code here

As to your problem, at first glance it seems to me that you are trying to use some variables on your loop() which you declared inside the setup().

When you declare a variable inside a function it will be a local variable, so it will be accessible only inside that function.
For instance your acStatus variable. You are declaring it inside your setup() and trying to use it on your loop(). This will not work.
The way to make it work is to make them global variables. You can declare it before your setup(), up with your other variables.

Also notice that they are case sensitive, so acStatus is different from acstatus.

So, make your acStatus and your enginetempStatus variables global, something like:
int acStatus;
int enginetempStatus       <----  maybe you could write engineTempStatus. helps reading it

Then if you want to use them on your setup () you don't need to declare them again, you can just write something like
acStatus = analogRead (A2);

Good luck!
13  Using Arduino / Programming Questions / Re: PRINTING VALUE OF TCNT1, TCNT1L, TCNT1H WITH Serial.println on: April 18, 2014, 02:50:18 am
what AWOL meant was that you should use the CODE tags when you post some code.
You can click up there on the third button from the right on the last row of message formating buttons (it has a number sign - # - on it) and then put all your code inside the tags.
Something like this

[ code]
will look like this
[ /code]

will look like this
14  Using Arduino / Programming Questions / Re: Help me adapt this code for MIDI? on: April 18, 2014, 02:44:46 am
if you don't want messages to be sent the whole time, you just need to check if the encoder has a NEW value before sending it.
something like this:
// read the encoder and save value to valiable
// compare variable of NEW value with LAST value
// if the value if DIFFERENT, send the value
// if it is the same, do nothing
// make LAST value = to NEW value

What are you trying to build?
I have built a couple of MIDI controllers using an Arduino and it has been great fun. I didn't use any encoders though. I have used buttons, leds and some pots (normal knobs).
The way i have done it was like this:
- analog read the pot value (it will give a value between 0 - 1023)
- midi will take values from 0 - 127, so i map the pot value to the midi range
- then a midi message needs several pieces of information, so you need to decide which information you want your pot to send.

On my last project i decided to send cmd, pitch and velocity for each element (either a button, a pot,...).
I have created a function that would send this data via serial:
void noteOn (int cmd, int pitch, int vel){
} // end void noteOn

then when i would read the value (let's say from a pot) i would compare it to the last value (so it send a message only if there is a new value) and send something like this:
noteOn(0xB0, 20, mappedPotVal)

here is a great article about MIDI, so you can understand how MIDI works, which kind of info is sent on a MIDI message, .... It helped me a lot:

but there was still another thing.
On the computer side, the computer can't use the serial messages as MIDI, so i still had to convert them. But that wasn't so much trouble because there are some converters available which do all the work for you.
I use the Spikenzielabs Serial-MIDI converter, and it has always worked great. You can get it here:

So, i guess it all depends on what you are building, what kind of MIDI message you need to send. But the idea is always kind of the same...

Good luck!
15  Using Arduino / Interfacing w/ Software on the Computer / Re: First processing sketch on: April 17, 2014, 07:34:23 am
thanks for your input guys.
i have been reading more about it and i ended up thinking that using the Firmata library would be a good option for this project.
I am still waiting for the parts to come, so i created a little sketch to try things out with two pots, a button and an led.
it is working like this:
- the pot values are being printed on the window;
- on button press it records both pot values into a file and lights the LED for 1second.

here is the code:

 * Button (with pull-up res) arduino pin 4
 * LED arduino pin 9
 * 2 POTS arduino pins A0 and A4
 On button press Processing will read the pot values and store them
 on a file called "potValues.txt". It will also light the LED for
 1 second to signal the sucessfull record.

import processing.serial.*;
import cc.arduino.*;

Arduino arduino;

PrintWriter output;    // initialize the PrintWriter object "output"

int pot1 = 0;
int pot2 = 4;
int pot1read;
int pot2read;

int button = 4;
int butState;
int lastButState = arduino.HIGH;

int led = 9;
int ledMode = 0;
long startLED;
int ledTime = 1000;

void setup() {
  // window size
  size(120, 80);

  // print the available ports

  // Select and start arduino board
  arduino = new Arduino(this, Arduino.list()[0], 57600);

  // Set arduino pinModes
  arduino.pinMode(pot1, arduino.INPUT);
  arduino.pinMode(pot2, arduino.INPUT);
  arduino.pinMode(button, arduino.INPUT);
  arduino.pinMode(led, arduino.OUTPUT);

  // Create the output file
  output = createWriter("potValues.txt");

void draw() {

  pot1read = arduino.analogRead(pot1);
  pot2read = arduino.analogRead(pot2);
  butState = arduino.digitalRead(button);

  if (butState == arduino.LOW && butState != lastButState) {
    printData(pot1read, pot2read);
    startLED = millis();
  lastButState = butState;

  text(pot1read, 40, 40);
  text(pot2read, 40, 60);


void printData(int pot1data, int pot2data) {
  output.println(pot1data + ", \t" + pot2data);

void lightLED() {
  if (millis() - startLED < ledTime) {
    arduino.digitalWrite(led, Arduino.HIGH);
  else {
    arduino.digitalWrite(led, Arduino.LOW);

the code seems to be working ok.
But now my question is, is there a way NOT to create a new file on each time i run the sketch?
What is happening now is that on setup i am creating a new file
output = createWriter("potValues.txt");
so i am overwriting any data that was previously in the file.

Is it possible to do something like?
// check if file exists
// if not, create file
// if it exists, continue using the file

thanks!   =)
Pages: [1] 2 3 ... 15