Show Posts
Pages: 1 ... 60 61 [62] 63 64 ... 80
916  Using Arduino / Sensors / Re: Simple magnetic contact switch is not working right. on: September 01, 2012, 05:25:53 pm
Couple of potential problems.

Firstly, if this a simple reed switch then you need to have it arranged with a pull up or pull down resistor to give you a valid signal. Not sure if this is the case from your description.

Secondly, in your code you never actually reads the switch. Your code needs to look like this:
Code:
#include <Serial.h>

int sensorPin = 12;

void setup(){
  Serial.begin(9600);
  pinMode(sensorPin, INPUT);
}

void loop() {
  int magnetSensor = digitalRead(sensorPin);

  if(magnetSensor == HIGH){
    Serial.println("Close!");
  }
 
  if(magnetSensor == LOW){
    Serial.println("Far...");
    delay(200);
  }
}

And you can simplify the two if statements to an if..else.
917  Using Arduino / Audio / Re: A must build Audio Project on: September 01, 2012, 01:13:57 am
Just finished building one... What an amazing little device for so few components.

Have you finished the refactored code yet?
918  Using Arduino / LEDs and Multiplexing / Re: MAX7219 and LED matrix on: September 01, 2012, 12:09:28 am
Mine was all soldered up on a 'prototype pcb' - the type with just pads around the holes. I tried the breadboard approach but 8x8 LEDs and a breadboard was a bit hard for me to handle. It is actually very easy to build one up it up, but the number of solder joints meant there were bound to be some problems.

I have read before that on this forum some people have problems with the 7219 on breadboards because the high frequency used to update the display causes weird effects on the type of 'mechanical' electrical (if that makes sense) joints used.
919  Using Arduino / Sensors / Re: Photo Reflective Sensor Setup ?! on: September 01, 2012, 12:03:19 am
One thing to check - are you getting IR light from the LED? If you point a webcam or other digital cam at the LED you can see if it is 'on'. That would at least prove half the circuit is working.
920  Using Arduino / Sensors / Re: Photo Reflective Sensor Setup ?! on: August 31, 2012, 11:38:18 pm
Ok, it seems to work on my setup with a Pot on A0, but I have no idea why. A0 is defined as 14.

Analog input pins float all over the place unless they are connected, so getting a number when you disconnect does not mean a whole lot. That's the reason you need to have pull down/up resistors when you connect switch type devices.

Recheck your circuit. If you are using a breadboard, take a photo and post it or attach it to the posting.
921  Using Arduino / LEDs and Multiplexing / Re: MAX7219 and LED matrix on: August 31, 2012, 11:25:55 pm
When I built mine, there were shorts/dry solder joints that needed to be fixed up. Try debugging by manually connecting the LED matrix to power - for each row connecting each column (like the software would do) to make sure that only one LED comes on. Make sure you connect the power through a resistor or you could damage the LED.
922  Using Arduino / LEDs and Multiplexing / Re: Led Strobe Lights (Multi Patterns) Help on: August 31, 2012, 11:21:18 pm
I have modified my Multi_Blink example in the Playground to do the cool pattern you are looking for. Hope this helps.

Mulri_Blink2.h
Code:
// Multi_Blink2.h
//
// Blink lots of LEDs at diffreent frequencies simultaneously
//
// Header file is required to be able to define the structured types
//
#include <Arduino.h>

#ifndef  MULTIBLINKH
#define  MULTIBLINKH

// State values for FSM
#define  MB_NULL  0
#define  MB_LOW   1
#define  MB_HIGH  2
#define  MB_LOOP  3

#define  LOOP_UNDEF  255

typedef struct
{
  uint8_t  activeVal;     // state value for this state to be active (MB_* defines)
  uint16_t activeTime;    // time to stay active in this state stay in milliseconds or loop counter
  uint8_t  nextState;     // If MB_LOOP this is the state to loop back/forward to
} stateDef;

typedef struct
{
  uint8_t  ledPin;         // Arduino I/O pin number
  uint8_t  currentState;   // current active state
  uint8_t  currentLoop;    // current position in the cycle
  stateDef state[5];       // the MB_* state definitions. Add more states if required
  uint32_t nextWakeup;     // the 'time' for the next wakeup - saves the millis() value
} ledTable;

#endif

Multi_Blink2.ino
Code:
// Multi_Blink2
//
// Blink lots of LEDs at different frequencies simultaneously, include delays and loops in patterns
//
// Marco Colli - September 2012
//
// Demonstrates the way to carry out multiple time based tasks without using the delay() function
// Demonstrates the use of structures (and structures within structures)
// Demonstrates a data driven approach to programming to create compact, reusable code
//

#include "Multi_Blink2.h"  // type definitions

// Blink Table T - Modify this table to suit whatever the output requirements are
// Add or delete lines as required to achieve the desired effects.
// To add additional states the structure in the header file needs to be modified
//
ledTable  T[] =
//Pin  St Lopp  State 0          State 1            State 2          State 3         State 4          Wkup
{
  { 13, 0, 0, {{MB_HIGH, 20, 0}, {MB_LOW, 50, 0}, {MB_LOOP, 3, 0}, {MB_HIGH, 250, 0}, {MB_LOW, 450, 0}}, 0 },
  { 12, 0, 0, {{MB_LOW, 450, 0}, {MB_HIGH, 20, 0}, {MB_LOW, 50, 0}, {MB_LOOP, 3, 1}, {MB_HIGH, 250, 0}}, 0 },
};

// Self adjusting constants for loop indexes
#define  MAX_STATE  (sizeof(T[0].state)/sizeof(stateDef))
#define  MAX_LED    (sizeof(T)/sizeof(ledTable))

void setup()
{
  for (int i=0; i < MAX_LED; i++)
  {
    pinMode(T[i].ledPin, OUTPUT);
   
    T[i].nextWakeup = 0;
    digitalWrite(T[i].ledPin, LOW);
    T[i].currentLoop = LOOP_UNDEF;
  }
}

void loop()
{
  for (int i=0; i < MAX_LED; i++)
  {
    // check if the state active time has expired (ie, it is less than current time)
    if (millis() >= T[i].nextWakeup)
    {
      T[i].nextWakeup = millis() + T[i].state[T[i].currentState].activeTime;

      switch (T[i].state[T[i].currentState].activeVal)
      {
        case MB_LOW:
        case MB_HIGH:    // Write digital value
          digitalWrite(T[i].ledPin, T[i].state[T[i].currentState].activeVal == MB_HIGH ? HIGH : LOW);
          T[i].currentState = (++T[i].currentState) % MAX_STATE;
          break;
         
        case MB_LOOP:  // loop back to specified state if there is still count left
          // first time in this state set the loop counter
          if (T[i].currentLoop == LOOP_UNDEF)
          {
            T[i].currentLoop = T[i].state[T[i].currentState].activeTime;
          }

          // loop around or keep going?         
          if (T[i].currentLoop-- > 0)
          {
            T[i].currentState = T[i].state[T[i].currentState].nextState;
            T[i].nextWakeup = 0;  // do it immediately
          }
          else
          {
            T[i].currentLoop = LOOP_UNDEF; // set up loop counter
            T[i].currentState = (++T[i].currentState) % MAX_STATE;
          }
          break; 
         
        default:  // just move on - handles NULL and any stupid states we may get into
          T[i].currentState = (++T[i].currentState) % MAX_STATE;
          break;
      }
    }
  }
}
923  Using Arduino / Sensors / Re: Photo Reflective Sensor Setup ?! on: August 31, 2012, 11:18:27 pm
Code:
int val0=0;
int irPin0 = A0;  // <- this needs to be the pin you want to read from
void setup()
{
  Serial.begin(9600);
}

void loop()
{
  val0 = analogRead(irPin0);

  Serial.print(val0);
  Serial.print("\n\r");
  delay(1500);
}
924  Using Arduino / Sensors / Re: Photo Reflective Sensor Setup ?! on: August 31, 2012, 10:59:44 pm
Sounds correct. You don't say what your software is doing? Maybe post your code?
925  Using Arduino / Programming Questions / Re: For loops?? on: August 31, 2012, 10:56:10 pm
OK, so I modified MultiBlink to add the concept of looping in patterns. I think this does what you want from a flashing lights perspective, and I have a more flexible program to boot!

Let me know how you go with this.

Multi_Blink2.h
Code:
// Multi_Blink2.h
//
// Blink lots of LEDs at diffreent frequencies simultaneously
//
// Header file is required to be able to define the structured types
//
#include <Arduino.h>

#ifndef  MULTIBLINKH
#define  MULTIBLINKH

// State values for FSM
#define  MB_NULL  0
#define  MB_LOW   1
#define  MB_HIGH  2
#define  MB_LOOP  3

#define  LOOP_UNDEF  255

typedef struct
{
  uint8_t  activeVal;     // state value for this state to be active (MB_* defines)
  uint16_t activeTime;    // time to stay active in this state stay in milliseconds or loop counter
  uint8_t  nextState;     // If MB_LOOP this is the state to loop back/forward to
} stateDef;

typedef struct
{
  uint8_t  ledPin;         // Arduino I/O pin number
  uint8_t  currentState;   // current active state
  uint8_t  currentLoop;    // current position in the cycle
  stateDef state[4];       // the MB_* state definitions. Add more states if required
  uint32_t nextWakeup;     // the 'time' for the next wakeup - saves the millis() value
} ledTable;

#endif

Multi_Blink2.ino
Code:
// Multi_Blink2
//
// Blink lots of LEDs at different frequencies simultaneously, include delays and loops in patterns
//
// Marco Colli - September 2012
//
// Demonstrates the way to carry out multiple time based tasks without using the delay() function
// Demonstrates the use of structures (and structures within structures)
// Demonstrates a data driven approach to programming to create compact, reusable code
//

#include "Multi_Blink2.h"  // type definitions

// Blink Table T - Modify this table to suit whatever the output requirements are
// Add or delete lines as required to achieve the desired effects.
// To add additional states the structure in the header file needs to be modified
//
ledTable  T[] =
//Pin  St Lopp  State 0          State 1            State 2          State 3         Wkup
{
  { 3, 0, 0, {{MB_HIGH, 50, 0}, {MB_LOW, 100, 0}, {MB_LOOP, 4, 0}, {MB_LOW, 800, 0}}, 0 },
  { 4, 0, 0, {{MB_LOW, 100, 0}, {MB_HIGH, 50, 0}, {MB_LOOP, 4, 0}, {MB_LOW, 800, 0}}, 0 },
  { 5, 0, 0, {{MB_LOW, 800, 0}, {MB_HIGH, 50, 0}, {MB_LOW, 100, 0}, {MB_LOOP, 4, 1}}, 0 },
  { 6, 0, 0, {{MB_LOW, 800, 0}, {MB_LOW, 100, 0}, {MB_HIGH, 50, 0}, {MB_LOOP, 4, 1}}, 0 },
};

// Self adjusting constants for loop indexes
#define  MAX_STATE  (sizeof(T[0].state)/sizeof(stateDef))
#define  MAX_LED    (sizeof(T)/sizeof(ledTable))

void setup()
{
  for (int i=0; i < MAX_LED; i++)
  {
    pinMode(T[i].ledPin, OUTPUT);
   
    T[i].nextWakeup = 0;
    digitalWrite(T[i].ledPin, LOW);
    T[i].currentLoop = LOOP_UNDEF;
  }
}

void loop()
{
  for (int i=0; i < MAX_LED; i++)
  {
    // check if the state active time has expired (ie, it is less than current time)
    if (millis() >= T[i].nextWakeup)
    {
      T[i].nextWakeup = millis() + T[i].state[T[i].currentState].activeTime;

      switch (T[i].state[T[i].currentState].activeVal)
      {
        case MB_LOW:
        case MB_HIGH:    // Write digital value
          digitalWrite(T[i].ledPin, T[i].state[T[i].currentState].activeVal == MB_HIGH ? HIGH : LOW);
          T[i].currentState = (++T[i].currentState) % MAX_STATE;
          break;
         
        case MB_LOOP:  // loop back to specified state if there is still count left
          // first time in this state set the loop counter
          if (T[i].currentLoop == LOOP_UNDEF)
          {
            T[i].currentLoop = T[i].state[T[i].currentState].activeTime;
          }

          // loop around or keep going?         
          if (T[i].currentLoop-- > 0)
          {
            T[i].currentState = T[i].state[T[i].currentState].nextState;
            T[i].nextWakeup = 0;  // do it immediately
          }
          else
          {
            T[i].currentLoop = LOOP_UNDEF; // set up loop counter
            T[i].currentState = (++T[i].currentState) % MAX_STATE;
          }
          break; 
         
        default:  // just move on - handles NULL and any stupid states we may get into
          T[i].currentState = (++T[i].currentState) % MAX_STATE;
          break;
      }
    }
  }
}
926  Using Arduino / Sensors / Re: Photo Reflective Sensor Setup ?! on: August 31, 2012, 08:28:25 pm
You have got 2 things going on here and you need to do them separately:

On one side you have what is a LED and you need to wire it up in series with a resistor. The specs on the sheet say that the Forward Current (If) is 20mA, for Forward Voltage (Vf) of 1.2V. If you are running off a 5V Arduino, you will need to drop 3.8V across the resistor at 20mA. Using Ohms Law (V=I*R) it gives us a resistor of 3.8/.020 = 190 Ohm. Use something close but over this value without being excessively over.

One the output side you have what is a voltage divider. If Vcc is 5V then I would try something like a 1k resistor.

The wiring diagram, by the way, is bottom left of page 2 of the specsheet.
927  Using Arduino / Programming Questions / Re: code optimisation - stuck. on: August 29, 2012, 09:50:51 pm
Yes you can do that as long as LOW and HIGH are defined as 0 and 1. More generally though, they are not, so this form can be used:
Code:
       CS.digitalWrite(1, (Light_status == 0) ? LOW : HIGH);

Also you can simplify your arithmetic to
Code:
     Light_status = (++Light_status) % 2;

You last return statement also does not return anything.
928  Using Arduino / Programming Questions / Re: For loops?? on: August 28, 2012, 09:58:46 pm
What you can do with Multiblink as it stands is blink one LED fast (say at 50 ms) and then another one slowly (say at 150ms) by creating a table of 2 elements.

What will happen is that on the third blink the second LED will light at the same time as the first. That is probably not what you are looking for,

There is a way to modify multblink to include the cycling a pattern and then adding in a delay at the end of the cycle. You could then define that one led will cycle 3 times at 50ms then delay 100ms before starting the cycle again. The second LED would not need this as it only goes off once. The change is relatively easy to do and I can help with that if I have described it correctly.
929  Using Arduino / Programming Questions / Re: Help with ISO C++ forbids comparison between pointer and integer. on: August 27, 2012, 04:58:20 am
This line
Code:
 if(Ignition_Monitor == 1)

Is comparing the address of the function to 1. You probably mean this
Code:
 if(Ignition_Monitor() == 1)

which compares the result returned by the function to 1
930  Using Arduino / LEDs and Multiplexing / Re: Led Strobe Lights (Multi Patterns) Help on: August 25, 2012, 04:19:38 pm
You won't be able to respond to the buttons with all the delay statements. You need to look at the 'Blink without delay' example to work out how to restructure your code.

When you have done that it will be clearer what you need to do. In case it is not - use the button press to increment a variable that tells you what pattern number you are supposed to execute, then in the code you go to the right pattern depending on what that variable says it should be.
Pages: 1 ... 60 61 [62] 63 64 ... 80