Loading...
  Show Posts
Pages: 1 ... 23 24 [25]
361  Forum 2005-2010 (read only) / Exhibition / Re: Morse decoder and password checker on: September 23, 2010, 03:11:32 pm
Nice work
I just wrapped your code up in the forum friendly code displayer (the little # button above when you post)
Just so its easier to read.

Top job smiley

Code:
/*
* Morse Code Decoder and door opener
*
* Josh Myer <josh@joshisanerd.com>
*
* 20090103 rev 0 -- ugly and unfortunate, but mostly functional (my first arduino code)
*
* Hook a debounced switch up to digital pin 2, like you would for the Button demo.
*
* This code reads morse code from digital02, turning it into ASCII characters.
* If you key in "SOS" (... --- ...), it will turn on the LED on digital13.
*
* The intended application is to let me key in a password at my apartment's
* front gate and have it automatically let me into the building, instead of
* fumbling around for keys.
*
* There's still lots of stuff to do and clean up, but I wanted to share the idea
* and the current implementation to help spur people on.
*/

#include <avr/pgmspace.h>
#include <string.h>

int ledPin = 13;                // choose the pin for the LED
int inputPin = 2;               // choose the input pin (for a pushbutton)
int val = 0;                    // variable for reading the pin status

#define THRESHOLD 3
#define DELAY_TIME 10 // ms

int n_since_zero = 0;
int n_in_zero = 0;

#define NCHARS 26+10+3

char morse_chars[NCHARS]  = {
 'A',
 'B',
 'C',
 'D',
 'E',
 'F',
 'G',
 'H',
 'I',
 'J',
 'K',
 'L',
 'M',
 'N',
 'O',
 'P',
 'Q',
 'R',
 'S',
 'T',
 'U',
 'V',
 'W',
 'X',
 'Y',
 'Z',
 '0',
 '1',
 '2',
 '3',
 '4',
 '5',
 '6',
 '7',
 '8',
 '9',
 '.',
 ',',
 '?',

};

char* morse_strings[NCHARS] = {
 ".-",
 "-...",
 "-.-.",
 "-..",
 ".",
 "..-.",
 "--.",
 "....",
 "..",
 ".---",
 "-.-",
 ".-..",
 "--",
 "-.",
 "---",
 ".--.",
 "--.-",
 ".-.",
 "...",
 "-",
 "..-",
 "...-",
 ".--",
 "-..-",
 "-.--",
 "--..",
 "-----",
 ".----",
 "..---",
 "...--",
 "....-",
 ".....",
 "-....",
 "--...",
 "---..",
 "----.",
 ".-.-.-",
 "--..--",
 "..--..",
};



#define PAUSE 0
#define DIT 1
#define DAH 2
#define DDLEN 5

char passwd[] = "SOS";

char chars_rx[10];
int char_cursor= 0;

int ditsdahs[DDLEN];
int dd_cursor = 0;

void setup() {
 pinMode(ledPin, OUTPUT);      // declare LED as output
 pinMode(inputPin, INPUT);     // declare pushbutton as input

 for (int i = 0; i < DDLEN; i++) {
   ditsdahs[i] = 0;
 }
 dd_cursor = 0;


 for (int i = 0; i < 10; i++) {
   chars_rx[i] = 0;
 }
 char_cursor = 0;


 Serial.begin(9600);
}

void dd_print() {
 Serial.print("  > DD BUF: ");
 for(int i = 0; i < DDLEN; i++) {
   Serial.print(ditsdahs[i]);
   Serial.print(", ");
 }
 Serial.println();
}

boolean dd_eq(char*buf) {
 if(ditsdahs[0] == PAUSE) return false;

 int i;

 for(i = 0; ditsdahs[i] != PAUSE && i < DDLEN; i++) {
   if (ditsdahs[i] == DIT && buf[i] == '-') {
     return false;
   }
   if (ditsdahs[i] == DAH && buf[i] == '.') {
     return false;
   }
 }

 /*
 Serial.print("Got to the end of ");
  Serial.print(buf);
  Serial.print("; checking strlen=");
  Serial.print(strlen(buf));
  Serial.print(" == i=");
  Serial.println(i);
  */
 if (i != strlen(buf)) return false;

 return true;
}

void dd_decode() {
 for(int i = 0; i < NCHARS; i++) {
   /*
   Serial.print("dd_decode: check i=");
    Serial.print(i);
    Serial.print(" , string=\"");
    Serial.print(morse_strings[i]);
    Serial.print("\", c=");
    Serial.print(morse_chars[i]);
    Serial.println();
    */
   if (dd_eq(morse_strings[i])) {

     char c = morse_chars[i];

     char_emit(c);


     break;
   }
 }

 for (int i = 0; i < DDLEN; i++) {
   ditsdahs[i] = 0;
 }
 dd_cursor = 0;
}

void check_passwd() {

 Serial.print("Check password: ");
 Serial.print(chars_rx);
 Serial.println();

 if (0 != strstr(chars_rx, passwd)) {
   digitalWrite(ledPin, HIGH);
 }

 /*
 for (int i = 0; i < 10; i++) {
  chars_rx[i] = 0;
  }
  char_cursor = 0;
  */
}

void char_emit(char c) {
 Serial.print("Got a char: c=");

 Serial.println(c);


 chars_rx[char_cursor] = c;
 char_cursor++;

 if (char_cursor >= strlen(passwd)) {
   check_passwd();
 }
}

void dd_emit(int v) {
 ditsdahs[dd_cursor] = v;
 dd_cursor++;

 dd_print();

 if (v == PAUSE)
   dd_decode();
}

void dit()   {
 dd_emit(DIT);
 Serial.println("DIT");
}
void dah()   {
 dd_emit(DAH);
 Serial.println("DAH");
}
void pause() {
 dd_emit(PAUSE);
 Serial.println("PAUSE");
}

void loop(){
 val = digitalRead(inputPin);  // read input value

 if (val == LOW) {
   if (n_since_zero > 5) {
     /*  Serial.print("CROSS LOW AFTER ");
      Serial.println(n_since_zero);
      */
     if (n_since_zero > 16) {
       dah();
     }
     else {
       dit();
     }


   }

   n_since_zero = 0;
   n_in_zero++;
 }
 else {
   n_since_zero++;
 }

 if (n_in_zero == 20) {
   pause();
 }

 if (n_since_zero > THRESHOLD) {
   if (n_in_zero > 0) {
     // Serial.print("CROSS HIGH AFTER ");
     //Serial.print(" > ");
     // Serial.println(n_in_zero);

     //      Serial.println("**************************");
   }

   n_in_zero = 0;
   // digitalWrite(ledPin, HIGH);
 }
 else {
   // digitalWrite(ledPin, LOW);
 }

 delay(DELAY_TIME);
}
362  Forum 2005-2010 (read only) / Exhibition / Re: temperature logger on: August 04, 2010, 12:40:59 am
Are you able to share your c# code for the serial comms and the graphing, as I would like to use that sort of thing for testing and graphing data coming out of my arduino - I could see that being very handy for a number of projects, just to graph data and easily send fixed data to the arduino.

Thanks
363  Forum 2005-2010 (read only) / Exhibition / Re: Prototype SPI Relay Shield on: July 26, 2010, 05:56:44 pm
Correct, as I said, its a prototype.
That said, unless there are tiny relays available then fitting more on a 'standard' shield size PCB is going to be an issue. Surface Mount will make more room, but really its the relays that take up the room.

If more relays were needed and a relay board was made with a header and ribbon cable or something from the Arduino, then obviously you could have as many relays as you wanted.

This was for me to simply experiment with and to get some more IO on the arduino and 4 relay outputs without using too many of the arduino's IO, except for the pins required for the SPI.

So yes, the board simply provides 4 relays via a SPI expander, which could be easily done without the SPI expander, however it then has 12 extra IO for use for other things.

Its a prototype so will be far from what everyone will ideally want.
364  Forum 2005-2010 (read only) / Exhibition / Re: Prototype SPI Relay Shield on: July 26, 2010, 02:54:46 pm
@Salvador - Thanks very much for teaching me how to suck eggs.

The current draw on each output of the MCP23S17 is approximately 4.2mA based on my calculation of the base resistor and the operating voltage.

If anyone knows how to be condecending and basically an ass, you have taken the cake. No idea where your attacking reply came from but you can keep them to yourself from now on.

I am an automation engineer, not a preschooler.

WanaGo

365  Forum 2005-2010 (read only) / Exhibition / Re: Prototype SPI Relay Shield on: July 25, 2010, 03:25:19 am
The MCP23S17 is driving transistors, which are driving the relay coils, so the MCP23S17 isn't having to source the current required to switch the relays.
366  Forum 2005-2010 (read only) / Exhibition / Re: Prototype SPI Relay Shield on: July 22, 2010, 01:55:39 pm
Hi,
Haha yeah - 5A.
And no, the limit isnt 4 relays - however the limit for fitting it on a standard size shield like this is I would think. If I didnt have the SPI Expander then I may be able to fit 2 more, but then it would be IO driven rather than SPI driven and so that takes pins away from other shields.
In theory you can have as many relays as you have SPI outputs, and for each SPI expander you can have 16, and you can have multiple SPI expanders.
I cant recall off the top of my head what the coil current draw is of each of these relays, however its not much - but if you started having like 15 Relays then USB powered wouldnt be possible I think.

Cheers  ;D
367  Forum 2005-2010 (read only) / Exhibition / Prototype SPI Relay Shield on: July 22, 2010, 06:02:16 am
Hey

Whether or not anyone is interested in this, I am not sure, but just something I put together as a test more than anything, and to practice my PCB designing skills again. Its been a while.

I wanted to make a shield that featured a few small relays so I could switch larger devices, as I couldnt find anyone else who had really done this (no doubt there is however).
I then thought about making a shield with a MCP23S17 SPI IO Expander, as I brought 5 of this a while ago but hadnt played with them yet. I was using Bascom-AVR at that stage and hadnt purchased my first Arduino.

Anyway, I then though - instead of taking up IO from the Arduino to drive these relays, why dont I do this with some of the outputs from the SPI expander.

So what I have made is a prototype PCB with a MCP23S17 chip, of which 4 of the outputs drive 4 BC337 transistors, which in turn drive the coils of 4 5volt relays, which are capable of switching 5 Amps at 230VAC.

The rest of the IO pins I have just made available on header pins, which could be used for anything.

As I say, this is a prototype, and I didnt have enough 0.1" header pins to fully populate the board, but all the functional pins are connected. The others would just be pass through, however the relays are quite tall and so this board would most likely need to be the top most board if stacking anyway.

Designed in Protel DXP 2004 and printed by a company here in NZ who prints single and double sided boards however doesnt have the technology to do via's etc, so these are all done manually. Cheap service though and much better than etching my own boards. And since its a prototype I didnt bother getting them to cut the board out completely, but as you will see it is designed with the proper contours that other arduino shields have.

If/When I make a proper version, it will all be reshuffled as some components arent in ideal places - but its a prototype. Not much room on a shield footprint when using through hole components and relays!

Here are a few snaps incase anyone is interested, sorry the lighting in this room is dim at best and the flash is a bit bright.









This is the test code I used if anyone is interested.

Code:
/*Code modified from code I found on http://spikenzielabs.com/SpikenzieLabs/Project_64.html
  Purely to test if the hardware works, which it does.
  Code turns on and off each relay each second
*/

#define            MCP23S17      B01001100      // MCP23017 SPI Address

#define            IOCON            0x0A            // MCP23017 Config Reg.

#define            IODIRA            0x00            // MCP23017 address of I/O direction
#define            IODIRB            0x01            // MCP23017 1=input

#define            IPOLA            0x02            // MCP23017 address of I/O Polarity
#define            IPOLB            0x03            // MCP23017 1= Inverted

#define            GPIOA            0x12            // MCP23017 address of GP Value
#define            GPIOB            0x13            // MCP23017 address of GP Value

#define            GPINTENA      0x04            // MCP23017 IOC Enable
#define            GPINTENB      0x05            // MCP23017 IOC Enable

#define            INTCONA            0x08            // MCP23017 Interrupt Cont
#define            INTCONB            0x09            // MCP23017 1= compair to DEFVAL(A or B) 0= change

#define            DEFVALA            0x06            // MCP23017 IOC Default value
#define            DEFVALB            0x07            // MCP23017 if INTCONA set then INT. if diff.

#define            GPPUA            0x0C            // MCP23017 Weak Pull-Ups
#define            GPPUB            0x0D            // MCP23017 1= Pulled HIgh via internal 100k

#define            OLATA            0x14
#define            OLATB            0x15

#define            INTFA            0x0E
#define            INTFB            0x0F

#define            INTCAPA         0x10
#define            INTCAPB            0x11

// SPI
#define         SS              4                // Pin mapping to Arduino = SELECT
#define         MOSI            6                // Pin mapping to Arduino = Master Out Slave In
#define         SCLK            7                // Pin mapping to Arduino = Serial clock
#define         MISO            5                // Pin mapping to Arduino = Master IN slave OUT

int rx_data = 0;
int buttonPress    = 0;
int error_flag = 0;
int COLUMN = 0;

void setup()
{
  Serial.begin(9600);                    

  pinMode(SS, OUTPUT);
  pinMode(MOSI, OUTPUT);
  pinMode(SCLK, OUTPUT);
  pinMode(MISO, INPUT);

  digitalWrite(SS, HIGH);
  digitalWrite(SCLK, LOW);
  digitalWrite(MOSI, LOW);

  delay(1000);                               // This delay seems important for the MCP23S17 power-up
  SPI_portexpanderinit();
}

void loop()
{
  //Turns on and off each relay in turn
  
  SPI_TX(MCP23S17, GPIOA, B00000000);
  SPI_TX(MCP23S17, GPIOB, B00000000);
  delay(1000);
  SPI_TX(MCP23S17, GPIOA, B00000001);
  delay(1000);
  SPI_TX(MCP23S17, GPIOA, B00000000);
  delay(1000);
  SPI_TX(MCP23S17, GPIOA, B00000010);
  delay(1000);
  SPI_TX(MCP23S17, GPIOA, B00000000);
  delay(1000);
  SPI_TX(MCP23S17, GPIOB, B10000000);
  delay(1000);
  SPI_TX(MCP23S17, GPIOB, B00000000);
  delay(1000);
  SPI_TX(MCP23S17, GPIOB, B01000000);
  delay(1000);
}

void SPI_portexpanderinit()
{
  // --- Set I/O Direction
  SPI_TX(MCP23S17,IODIRB,B00000000);                                 // MCP23S17 port B = OUTPUT
  SPI_TX(MCP23S17,IODIRA,B00000000);                                 // MCP23S17 port A = OUTPUT
  
  //  --- Clear ALL Bits of GPIOA and GPIOB
  SPI_TX(MCP23S17,GPIOB,B00000000);                                  // MCP23S17 Clear port B
  SPI_TX(MCP23S17,GPIOA,B00000000);                                  // MCP23S17 Clear port A
  
}

void SPI_TX(int device, int regadd, int tx_data)
{
  digitalWrite(SS, LOW);                                       // Select the Chip

  device = device & B11111110;                                 // Clear last bit for a write

  SPI8BITTXLOOP(device);                                       // SPI Device

  SPI8BITTXLOOP(regadd);                                       // SPI REGISTER ADDRESS

  SPI8BITTXLOOP(tx_data);                                      // Data

  digitalWrite(SS, HIGH);                                      // Done UN-Select the Chip
}

void SPI8BITTXLOOP(int data)
{
  int i = 0;
  int temp = 0;
  
  for(i=1; i < 9; i = i +1)
  {
    temp = (data >> 7);                                        // Test bit 7 of DATA
    temp = temp & 1;
    if (temp == 0)                                             // SET or CLEAR MOSI
    {
      digitalWrite(MOSI, LOW);
    }
    else
    {
      digitalWrite(MOSI, HIGH);
    }
    digitalWrite(SCLK, HIGH);                                   // SET SCLK
    digitalWrite(SCLK, LOW);                                    // Clear Clock
    data = data << 1;                                           // Shift data LEFT by 1
  }
}
368  Forum 2005-2010 (read only) / News / Re: Premier-Farnell to offer own version of Arduino on: July 19, 2010, 02:23:43 am
Yeah

Here is the mega



Here is the nano

369  Forum 2005-2010 (read only) / News / Re: Premier-Farnell to offer own version of Arduino on: July 19, 2010, 02:02:59 am
Yeah I noticed this when ordering a few parts from Farnell yesterday.

Here is a pick of one of them.



Its still cheaper for me to import one from the USA though, go figure.

370  Forum 2005-2010 (read only) / Frequently-Asked Questions / Re: Something new ? on: November 14, 2010, 04:21:28 am
I had a bit of a hunt too and havent found anything yet.

I did find this though, but yeah - no AU's.

http://www.stkcheck.com/evs/atmel/atmelheader2.asp?mfg=atmel&part=ATmega1284P
371  Forum 2005-2010 (read only) / Frequently-Asked Questions / Re: upload sketch from arduino board to ide on: July 22, 2010, 07:44:24 pm
To me that makes the most sense if someone wanted to takle this. It may not suit the main purpose of this thread though as I am assuming it is to recover code that hasnt been saved.

Future wise though, if someone wanted a project and could figure out how to upload something other than compiled code to the AVR's flash. No idea if this is possible or not - I am by no means at the level needed with AVR's to even attempt testing that.

But as I say, if uncompiled code could be loaded into the AVR then it can easily be extracted again and run in the IDE. I guess the uncompiled verison of the code would be considerable even when compressed, as I wouldnt think it would be anywhere near as small as compiled code. So only really an option for larger AVR's, or via SD card etc.

More to ponder, im sure someone has run out of projects and would like to experiment smiley...

Anyone...  8-)
372  Forum 2005-2010 (read only) / Frequently-Asked Questions / Re: upload sketch from arduino board to ide on: July 20, 2010, 11:51:02 pm
This type of 'Upload' is common to PLC's (Programmable Logic Controllers - Industrial control systems basically), however isnt done by reverse compiling the code.

The code is written to flash like normal, but the uncompiled code is also able to be uploaded to memory and saved as a file basically. Then when an upload is done, the file is basically downloaded off the processor and opened with the editor.

Most likely not possible with AVR's as the memory may be too small and most likely wouldnt support this, however could most likely be done if you attached a SD sheild (or the like) - however the IDE would need to be modified in order to get the automated upload of the uncompiled code to SD, most likely.

Something to ponder on.

I am an Automation Engineer by trade, and use PLC's everyday, and this feature is very appealing especially if your backup is lost or you 'inherit' a PLC which was programmed some time ago and you have no source code.
373  Forum 2005-2010 (read only) / Frequently-Asked Questions / Re: Arduino Processor Loading and Baud Calculation on: July 08, 2010, 06:41:31 am
Ok, I understand.

The Serial.print statement, is it possible to group a bunch of data all together and then send it using one print statement, rather than having to have say 10 print statements, one after each other?

ie in Bascom AVR, you can do the following:
Print Command1 ; Command2 ; "," ; Value ; "," ; Value2 ; "," ; Index

whereas in Arduino IDE it seems I have to do the following:
Serial.print(Command1);
Serial.print(Command2);
Serial.print(",");
Serial.print(Value1);
Serial.print(",");
Serial.print(Value2);
Serial.print(",');
Serial.print(Index);

I dont know though if multiple print commands is slowing the processing down, or if its purely the fact that I am actually sending more data?

I am just trying to figure out how I should do this, as I need to sample these pulses as they come in, then perform a calculation on them, and then send the result out to the serial port. But I just dont know if I am doing it the most efficient way or if there is a better way to do it...

hmm.
374  Forum 2005-2010 (read only) / Frequently-Asked Questions / Re: Arduino Processor Loading and Baud Calculation on: July 08, 2010, 03:32:39 am
Audio processing?
Im not doing anything with audio frequencies - merely counting pulses to determine RPM, Km/h etc.

The ISR is only incrementing a count, which is then processed in the main loop.

RE the baud rate, if I have 20 character bytes to send each loop, then how is it I calculate which baud rate I ideally need to handle all the data, without overflowing the buffer etc..

Thanks
375  Forum 2005-2010 (read only) / Frequently-Asked Questions / Arduino Processor Loading and Baud Calculation on: July 08, 2010, 12:01:12 am
Hello

I am just trying to determine what the loading on my arduino processor currently is, as I want to know if I have enough 'cpu' time to do some more stuff.

Basically I have the following bit of code at the start of my loop, to capture the current time and compare it against the time of the last loop, so I can see the time per loop effectively.

Code:
unsigned long Time;
unsigned long PrevTime;
unsigned int ScanTime;

void loop()
{
  PrevTime = Time;
  Time = micros();
  ScanTime = Time - PrevTime;
  */blah blah/*
}

I also have some LCD code, and some Keypad code, and also sending some data (Sine wave values at the moment) to the serial port at 9600 baud, every loop. The Sine Wave values are just for the sake of having data to send to the serial, this will be replaced with the data I talk about at the end of this speech!

What the scan time is telling me is it is taking approximately 30ms per loop.

I just wanted to run a few calculations I made by you to see if I am correct...

30 ms/loop = 0.03 seconds/loop
1 / 0.03 = 33.3 loops/second

16Mhz CPU = 16,000,000 'cycles'/second
16,000,000 / 33.3 = 480480.48 'cycles'/loop

That is quite a few...

The next calculation is how much data I am sending over the serial link.
I have it set on 9600 baud, which is 9600 bits/second.

Each loop of my sketch, I am writing up to 20 characters onto the serial port. Whether or not this is correct or not, im not sure... but 20 characters is 20 bytes, which is 160 bits.
I am currently running 33.3 loops/second, and therefore:
160 bits * 33.3 times second = 5328 bits/second

So in theory I am within my limits of 9600 baud. Is this correct?

If I was to halve my scan time however, by say taking out my LCD and the print statements to it, then my loops/second would increase... so say 15ms/loop now, which is 0.015 seconds/loop, which is 66.6 loops/second. So therefore 160 bits * 66.6 = 10666 bits/second.
So does that mean that 9600 baud will no longer cope with the amount of data I am sending it?

Is this how you calculate this all out?

What I want to add onto the project is 2 high speed pulse trains into the external interrupts. One of the pulse trains is going to be coming in at about 340 pulses per second.

Is this going to cope..?

Sorry for the long post, but hopefully someone will spend the time to read and point me in the right direction.

Thanks
Pages: 1 ... 23 24 [25]