Electric Imp Stuff You Should Know
How do I send info to the Imp from a webpage? How do I send info from a webpage to the Imp?

PR's Tumblrdome
No title available
Color Me Curious
hello vonnie
No title available
Claire Keane
untitled
The Bowery Presents
EXPECTATIONS
Sade Olutola
ojovivo
macklin celebrini has autism
No title available

bliss lane
Show & Tell

shark vs the universe
Monterey Bay Aquarium
YOU ARE THE REASON

❣ Chile in a Photography ❣
Cosimo Galluzzi

seen from United States

seen from Türkiye
seen from Colombia

seen from Costa Rica

seen from Ireland
seen from Belgium
seen from Türkiye

seen from United States
seen from United States
seen from Argentina
seen from Chile

seen from Brunei

seen from Malaysia
seen from United States

seen from Malaysia
seen from Netherlands
seen from United States
seen from Ukraine
seen from Canada
seen from Indonesia
@rtwrkio-blog
Electric Imp Stuff You Should Know
How do I send info to the Imp from a webpage? How do I send info from a webpage to the Imp?
Content for AVR C Programming Web App
Input/Output 1
Input/Output 2
Interrupts
Timers
ADC
EEPROM
USART
PWM
Assembly
Bit-manipulation
Mer om Interrupts
Doxygen is a documentation system for C++, C, Java, Objective-C, Python, IDL (Corba and Microsoft flavors), Fortran, VHDL, PHP, C#, and to some extent D.
This happens to be the first in a series of posts that document building a “simple” DHTML application using the jQuery Mobile framework and a CouchDB back-end.
Tips for using AppLaud Cloud
Check out AppLaud Cloud
Files loaded into the assets folder (accessed with "/android_asset/") will only be available after doing a build.
If a new build seems like it is not updating the changes you have made to your code you will have to delete the bin folder and then build again
In order to access files you place in the assets folder you will have to use an absolute address in the following form '/android_asset/mysong.mp3'
To change the name of the app that is displayed under the icon you have to change the app_name variable in strings.xml
Like plugins and assets resources, splash screen configuration will work with AppLaud Cloud only when you do Package -> Build, and not from the AppLaud App run project feature.
jqMath is a JavaScript module that makes it easy to put formatted mathematical expressions in web pages.
I have not tried it yet but it is supposedly fast compared to other options and also quite simple to use. Should play well with jQuery Mobile and phonegap.
My first Google Apps Script
The following script will update a spreadsheet every time a user completes a form. The data will be sorted descending by time. This is handy since the default sort order for Google Spreadsheets is ascending.
function onSubmit(event) { var range = SpreadsheetApp.getActiveSheet().getRange("A2:U4000"); range.sort({column: 1, ascending: false}); // Sorter alle felter synkende etter kolonne A // NB! Funksjonen virker ikke med getDataRange() }
I also tried running this function by only getting the ranges A2:A4000. This turned out to be a very bad idea since this only sorted column A and not the other columns. I also tried getting the whole range of the active sheet by using getDataRange() but this gave an error message.
Edit: The reason getDataRange gave an error message was because the top row was used as "table headers" i.e. Timestamp, Name, Message and sorting these would distort the data. The solution is to select the active range and then offset the selected data by one row so that the headers are excluded. Improved code below.
function onSubmit(event) { var range = SpreadsheetApp.getActiveSheet().getDataRange().offset(1,0); range.sort({column: 1, ascending: false}); // Sorter alle felter synkende etter kolonne A //getDataRange().offset(1,0) ekskluderer første rad. }
Project Euler Problem 8
I have been having a lot of fun learning JavaScript with Codecademy and decided to try and solve Problem 8 with JavaScript. The problem is as follows:
Find the greatest product of five consecutive digits in the 1000-digit number. 73167176531330624919225119674426574742355349194934 96983520312774506326239578318016984801869478851843 85861560789112949495459501737958331952853208805511 12540698747158523863050715693290963295227443043557 66896648950445244523161731856403098711121722383113 62229893423380308135336276614282806444486645238749 30358907296290491560440772390713810515859307960866 70172427121883998797908792274921901699720888093776 65727333001053367881220235421809751254540594752243 52584907711670556013604839586446706324415722155397 53697817977846174064955149290862569321978468622482 83972241375657056057490261407972968652414535100474 82166370484403199890008895243450658541227588666881 16427171479924442928230863465674813919123162824586 17866458359124566529476545682848912883142607690042 24219022671055626321111109370544217506941658960408 07198403850962455444362981230987879927244284909188 84580156166097919133875499200524063689912560717606 05886116467109405077541002256983155200055935729725 71636269561882670428252483600823257530420752963450
My strategy is to place the digits in a string and then read the numbers with array notation. At the same time I will have to convert each digit to a number in order to be able to do mathematical operations.
// Project Euler Problem 8 var numbers = "7316717653133062491922511967442657474235534919493496983520312774506326239578318016984801869478851843858615607891129494954595017379583319528532088055111254069874715852386305071569329096329522744304355766896648950445244523161731856403098711121722383113622298934233803081353362766142828064444866452387493035890729629049156044077239071381051585930796086670172427121883998797908792274921901699720888093776657273330010533678812202354218097512545405947522435258490771167055601360483958644670632441572215539753697817977846174064955149290862569321978468622482839722413756570560574902614079729686524145351004748216637048440319989000889524345065854122758866688116427171479924442928230863465674813919123162824586178664583591245665294765456828489128831426076900422421902267105562632111110937054421750694165896040807198403850962455444362981230987879927244284909188845801561660979191338754992005240636899125607176060588611646710940507754100225698315520005593572972571636269561882670428252483600823257530420752963450"; var i,j = 0; var highestProduct = 1; var currentProduct = 1; var length = numbers.length; // Outer loop through whole array for(j=0 ; j < (length-5) ;j++ ){ // Loop five numbers at a time for( i = j ; i < (j+5); i++ ){ currentProduct *= parseInt(numbers[i]); } if(currentProduct > highestProduct){ highestProduct = currentProduct; console.log(highestProduct); } currentProduct = 1; }
The output from the code is as follows
882 1764 6048 15552 16128 18144 40824
The answer to challenge 8 is therefore 40824
Julia is a promising new language developed for scientific computation. Description from the source:
We want a language that’s open source, with a liberal license. We want the speed of C with the dynamism of Ruby. We want a language that’s homoiconic, with true macros like Lisp, but with obvious, familiar mathematical notation like Matlab. We want something as usable for general programming as Python, as easy for statistics as R, as natural for string processing as Perl, as powerful for linear algebra as Matlab, as good at gluing programs together as the shell. Something that is dirt simple to learn, yet keeps the most serious hackers happy. We want it interactive and we want it compiled.
Project Euler Problem 5
2520 is the smallest number that can be divided by each of the numbers from 1 to 10 without any remainder. What is the smallest positive number that is evenly divisible by all of the numbers from 1 to 20?
/* Project Euler 5 Optimized 2520 is the smallest number that can be divided by each of the numbers from 1 to 10 without any remainder. What is the smallest positive number that is evenly divisible by all of the numbers from 1 to 20? This optimized version counts myDivisor from 20 and down because a lot fewer numbers are divisible by 20,19,18,17... than 1,2,3,4... */ #include<stdio.h> #include <time.h> int main(){ // Start of timing clock_t start = clock(), diff; unsigned long long i = 2520; unsigned long long myDivisor; for(i; ; i++){ myDivisor = 20; while(i%myDivisor == 0){ // while the remainder equals zero // If myDivisor counts to 1 the answer is reached if(myDivisor == 1){ printf("Project Euler problem 5: %llu",i); break; // Break out of while loope } myDivisor--; // Decrement myDivisor } if(myDivisor == 1){break;} // Break out of for loop } // End of timing diff = clock() - start; int msec = diff * 1000 / CLOCKS_PER_SEC; printf("\nTime taken %d s %d ms", msec/1000, msec%1000); printf("\nPress Return to Exit..."); getchar(); return 0; }
The time it takes for the code to find the answer on my computer: 2.720s.
I also made another version of this which counts myDivisor from 1-20. This took 8.225s to complete.
User bitRAKE posted a well-thought out solution that uses mathematical identities instead of brute force:
This does not require programming at all. Compute the prime factorization of each number from 1 to 20, and multiply the greatest power of each prime together: 20 = 2^2 * 5 19 = 19 18 = 2 * 3^2 17 = 17 16 = 2^4 15 = 3 * 5 14 = 2 * 7 13 = 13 11 = 11 All others are included in the previous numbers. ANSWER: 2^4 * 3^2 * 5 * 7 * 11 * 13 * 17 * 19 = 232 792 560
Resources Project Euler Discussion
Timing code in C
After coming up with a couple of brute force solutions to Project Euler Problem 5 i decided to time the two options. After trying several different methods, with sub-optimal results, I came over a post on stackoverflow with a simple, portable and accurate solution which will be my go-to way of timing code until something better arrives:
#include <time.h> clock_t start = clock(), diff; ProcessIntenseFunction(); diff = clock() - start; int msec = diff * 1000 / CLOCKS_PER_SEC; printf("Time taken %d s %d ms", msec/1000, msec%1000);
Resources Best timing method in C
EDIT 1: The timing code is not as portable as I first thought. I compiled and ran the same code on my Samsung Galaxy SII using c4Droid and the reported result was 1.858s. By observation I could tell that it took at least 30 seconds. The hunt for optimal timing continues.
EDIT 2: I did some more searching and found another way to do timing that also works on my android. This new way works by using the gettimeofday() function also available in the timer.h library. I timed it using a stopwatch while the code ran on my Galaxy SII to confirm the result. The code ran for 106.226808s before outputing the answer to Project Euler problem 5. Here is the code for future reference:
// Timing C code with gettimeofday() #include <time.h> // Initialize timing variables of type timeval struct timeval begin, end; // Start timing gettimeofday(&begin, NULL); /* Code to time */ // End timing gettimeofday(&end, NULL); // Calculate timing double elapsed = (end.tv_sec - begin.tv_sec) + ((end.tv_usec - begin.tv_usec)/1000000.0); // Output timing printf("Code took %f seconds to complete", elapsed);
Resources How to get the running of time of my program with gettimeofday
EDIT 3: The gettimeofday function is not available on Windows systems. Conclusion: use method 1 for Windows and method 2 for Unix.
Dean Camera has been posting on the AVRFreaks forum for several years and has gained respect for his well written tutorials. Now they are all available at his website Four Walled Cubicle as handy PDFs.
Reading and writing to eeprom with the avr/eeprom.h library
This is an example of reading and writing to eeprom without having to control the interrupts and timing manually. The avr/eeprom.h library greatly simplifies the code.
/* * Oving4b.c * * Created: 08.02.2012 10:20:03 * Author: ketile * Hensikt: Skrive og lese til eeprom med * avr/eeprom.h biblioteket. * */ #include <avr/io.h> #include <avr/eeprom.h> int main(void) { while(1) { for(uint8_t i=1; i<=16; i++){ eeprom_write_byte((uint8_t*)i,i); } for(uint8_t i=1; 1<=16; i++){ uint8_t readByte = eeprom_read_byte((uint8_t*)i); } } }
Resources http://www.nongnu.org/avr-libc/user-manual/group__avr__eeprom.html http://www.societyofrobots.com/member_tutorials/node/309 http://www.avrfreaks.net/index.php?name=PNphpBB2&file=viewtopic&t=38417
Optimization in AVR Studio 5
This is the C code to be optimized
/* * Oving4.c * * Created: 08.02.2012 08:38:11 * Author: ketile * Hensikt: Lag et program i C som bruker EE_READY (EERE) * interruptet til å skrive til lokasjonene 0x01 - 0x10 * i eeprom. */ #include <avr/io.h> #include <avr/interrupt.h> // Deklarer funksjoner void EEput (uint16_t address, uint8_t data); uint8_t EEget (uint16_t address); // Deklarer variabler uint8_t data = 0; int main(void) { while(1) { // Lagre til eeprom for (uint8_t i=1; i<=16; i++){ data++; EEput(i, data); } // Les fra eeprom for (uint16_t i=1; i<=16; i++){ EEget (i); } } } // Funksjon for å lagre til EEPROM void EEput (uint16_t address, uint8_t data){ /* Vent til nåværende skriving er ferdig */ while(EECR & (1<<EEPE)); /* Send verdier til adresse- og dataregister */ EEAR = address; EEDR = data; /* Skriv logisk 1 EEMPE */ EECR |= (1<<EEMPE); /* Start eeprom write by setting EEPE */ // NB! Dersom det tar mer enn 4 klokkesykluser // mellom disse operasjonene vil den feile. // Bruk optimalisering høyere enn 0 i kompilatoren // for å unngå dette. EECR |= (1<<EEPE); } // Funksjon for å lese fra EEPROM uint8_t EEget (uint16_t address){ /* Vent til nåværende skriving er ferdig */ while(EECR & (1<<EEPE)); /* Bestem adresseregister */ EEAR = address; /* Les fra eeprom ved å skrive til EERE */ EECR |= (1<<EERE); /* Returner data fra adresseregisteret */ return EEDR; }
Compiled code before optimization (level0):
while(EECR & (1<<EEPE)); 000000AF NOP No operation while(EECR & (1<<EEPE)); 000000B0 LDI R24,0x3F Load immediate 000000B1 LDI R25,0x00 Load immediate 000000B2 MOVW R30,R24 Copy register pair 000000B3 LDD R24,Z+0 Load indirect with displacement 000000B4 MOV R24,R24 Copy register 000000B5 LDI R25,0x00 Load immediate 000000B6 ANDI R24,0x02 Logical AND with immediate 000000B7 ANDI R25,0x00 Logical AND with immediate 000000B8 SBIW R24,0x00 Subtract immediate from word 000000B9 BRNE PC-0x09 Branch if not equal EEAR = address; 000000BA LDI R24,0x41 Load immediate 000000BB LDI R25,0x00 Load immediate 000000BC LDD R18,Y+2 Load indirect with displacement 000000BD LDD R19,Y+3 Load indirect with displacement 000000BE MOVW R30,R24 Copy register pair 000000BF STD Z+1,R19 Store indirect with displacement 000000C0 STD Z+0,R18 Store indirect with displacement EEDR = data; 000000C1 LDI R24,0x40 Load immediate 000000C2 LDI R25,0x00 Load immediate 000000C3 LDD R18,Y+1 Load indirect with displacement 000000C4 MOVW R30,R24 Copy register pair 000000C5 STD Z+0,R18 Store indirect with displacement EECR |= (1<<EEMPE); 000000C6 LDI R24,0x3F Load immediate 000000C7 LDI R25,0x00 Load immediate 000000C8 LDI R18,0x3F Load immediate 000000C9 LDI R19,0x00 Load immediate 000000CA MOVW R30,R18 Copy register pair 000000CB LDD R18,Z+0 Load indirect with displacement 000000CC ORI R18,0x04 Logical OR with immediate 000000CD MOVW R30,R24 Copy register pair 000000CE STD Z+0,R18 Store indirect with displacement EECR |= (1<<EEPE); 000000CF LDI R24,0x3F Load immediate 000000D0 LDI R25,0x00 Load immediate 000000D1 LDI R18,0x3F Load immediate 000000D2 LDI R19,0x00 Load immediate 000000D3 MOVW R30,R18 Copy register pair 000000D4 LDD R18,Z+0 Load indirect with displacement 000000D5 ORI R18,0x02 Logical OR with immediate 000000D6 MOVW R30,R24 Copy register pair 000000D7 STD Z+0,R18 Store indirect with displacement
Click Project > Project Properties or ALT+F7 in AVR Studio to access the compiler optimization options.
Code after optimization (level2):
while(EECR & (1<<EEPE)); 00000088 SBIC 0x1F,1 Skip if bit in I/O register cleared 00000089 RJMP PC-0x0001 Relative jump EEAR = address; 0000008A OUT 0x22,R25 Out to I/O location 0000008B OUT 0x21,R24 Out to I/O location EEDR = data; 0000008C OUT 0x20,R1 Out to I/O location EECR |= (1<<EEMPE); 0000008D SBI 0x1F,2 Set bit in I/O register EECR |= (1<<EEPE); 0000008E SBI 0x1F,1 Set bit in I/O register
Highlighting code
A simple but great website called hilite.me will be my go to solution for posting code snippets in Tumblr posts. There are other ways of achieving the same result but the ease of use is what attracts me the most to this service. Here is an example of some C syntax I have saved at codepad.org:
#include<stdio.h> // Tell opp alle tall fra 1 til 1000 // Velg ut kun tallene som er delelige med 3 og 5 // Summer tallene som tilfredstiller kriteriene // Skriv ut svaret int main (void){ int i = 0; int total = 0; for (i=0; i<1000; i++){ if((i%3==0) || (i%5==0)){ total += i; } } printf ("Svaret er: %d", total); return 0; }
Here is an example of the same code displayed with the default tumblr <pre> and </pre> tags. It is apparent that when using the HTML editor by disabling Rich Text that these tags do not work as intended.
#include // Tell opp alle tall fra 1 til 1000 // Velg ut kun tallene som er delelige med 3 og 5 // Summer tallene som tilfredstiller kriteriene // Skriv ut svaret int main (void){ int i = 0; int total = 0; for (i=0; i<1000; i++){ if((i%3==0) || (i%5==0)){ total += i; } } printf ("Svaret er: %d", total); return 0; }