Show Posts
Pages: [1] 2
1  International / Scandinavia / Re: Använda Arduino i en kommersiell produkt? on: January 07, 2014, 04:30:56 pm
Aha då kanske jag också borde läsa på...
Jag tänker mig att biblioteken som finns nedladdningsbara är fria att använda. Det andra har skrivit är fritt att använda så länge man modifierat det till viss del så man kan se det som att man använd annans kod som idébank eller som hjälp. Jag har ju svårt att se att någon lägger fram kod, ritning och allt till en idé som är så bra/färdig att man kan sno hela konceptet rakt av utan att personen själv tjänat en hacka på idén först.
2  International / Scandinavia / Re: Använda Arduino i en kommersiell produkt? on: January 06, 2014, 05:25:01 am
Jag förstår inte frågan. Varför skulle du behöva lägga ut någonting alls för allmännheten?
3  International / Scandinavia / Re: Börja om från begyndelsen on: January 31, 2011, 03:36:09 am
Helsvensk men har inte så svårt att förstå er andra skandinaver.
Läser här lite då och då och har elektroniken som en hobby jag tar mig tid till ibland.
Aktuella projekt nu är en energimätare till huset som ritar ut elförbrukningen på en 128x64 lcd och en fartloggare som loggar hur fort bilarna kör förbi på vägen utanför.

4  Forum 2005-2010 (read only) / Syntax & Programs / Re: Problems with a big array on: September 08, 2009, 03:05:23 am
Thanks. Now I don't need to figure out what's wrong anymore. I have to find another solution.

/Sweeduino
5  Forum 2005-2010 (read only) / Syntax & Programs / Problems with a big array on: September 08, 2009, 02:33:55 am
I want to store a int value for each minute the last 24 hours. For this i need a array with 1440 int. This doesnt seem to work. I set up the array and then I use a for-loop to set each value to 0 initially. But the program never starts. How can I use a array of this size?
Mu code in itself is about 8k so there should be room left.

Ideas anyone?

/Sweeduino
6  Forum 2005-2010 (read only) / Development / Re: Circuit to swap to battery power on: January 21, 2010, 08:40:02 am
Sorry to disturb your conversation but I'm following it with great interrest. Could you supply some type of schematic for me to understand it better?
7  Forum 2005-2010 (read only) / Interfacing / Re: arduino speed gate on: December 28, 2009, 02:10:42 pm
Here is what I've made: http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1258832174/0#0

/Ulf
8  Forum 2005-2010 (read only) / Exhibition / Re: My electrical power meter on: February 13, 2010, 03:08:27 pm
You're right CRS8291. the variable sensorstate is obselete. It must be some old stuff left in the code. Im sure its possible to tidy it up even more.
Glad you could use my code for your meter.

9  Forum 2005-2010 (read only) / Exhibition / Re: My electrical power meter on: December 01, 2009, 02:35:17 am
Sure, here comes a schematic:

The light resistor is connected to 5v and pin 3 and. And there is a 4 Mohm something resistor connected to pin 3 and ground. So if the light resistor sees darkness pin 3 is connected to ground and if it sees light pin 3 is connected to 5v. That vay i can read changes from LOW to HIGH on pin 3.
10  Forum 2005-2010 (read only) / Exhibition / Re: My electrical power meter on: November 29, 2009, 01:46:33 pm
Not a bad idea! Will look into it. Thanks for the idea.

/Ulf
11  Forum 2005-2010 (read only) / Exhibition / Re: My electrical power meter on: November 24, 2009, 07:31:34 am
Your friend is right. I was looking for a box to make it more finished but instead i screwed i on the wall so it feels pretty finished.

Next project could be to add a thermometer to see how the power usage is correlated to outside temperature. Add some memory and a real time clock to see when the most power is used.

But instead I've started a project to measure the speed of cars passing by on the street.
12  Forum 2005-2010 (read only) / Exhibition / Re: My electrical power meter on: November 24, 2009, 02:37:01 am
Thanks for the positive feedback, in makes me even more proud.
I learned the hard way to comment code when I studied. A professor told me to comment the some code two weeks after I wrote it. It took me four times the time to comment it than to write the code.

The light resistor is connected to 5v and pin 3 and. And there is a 4 Mohm something resistor connected to pin 3 and ground. So if the light resistor sees darkness pin 3 is connected to ground and if it sees light pin 3 is connected to 5v. That vay i can read changes from LOW to HIGH on pin 3.
13  Forum 2005-2010 (read only) / Exhibition / Re: My electrical power meter on: November 23, 2009, 05:19:16 am
Code part three (last part):

Code:
 if (key == 3)       //Button corresponding to weeks is pressed, display last weeks usage and rating. Works the same as for minutes                  
  {
    if(weekhaspassed)
    {
      sprintf(value, "%u", daysvalue/1000);
      b=strlen(value);
      for (int i=0; i<b; i++)
        result[i]=value[i];

      if(daysvalue<1000000)
      {
        result[b]=',';
        result[b+1]=((daysvalue%1000)/100)+48;  //Get first decimal
      }
      if(daysvalue<100000)
        result[b+2]=((daysvalue%100)/10)+48;  //Get second decimal
      if(daysvalue<10000)
        result[b+3]=(daysvalue%10)+48;  //Get third decimal
    }
    else
    {
      sprintf(value, "%d", sensortickcounter/1000); //Convert powerusage to string
      b=strlen(value);
      for(int i=0; i<b; i++)
        result[i]=value[i];

      if(sensortickcounter<1000000)
      {
        result[b]=',';
        result[b+1]=((sensortickcounter%1000)/100)+48;  //Get first decimal
      }
      if(sensortickcounter<100000)
        result[b+2]=((sensortickcounter%100)/10)+48;  //Get second decimal
      if(sensortickcounter<10000)
        result[b+3]=(sensortickcounter%10)+48;  //Get third decimal
    }

    result[5]='k';
    result[6]='W';
    result[7]='h';

//    result[b+8]=48+clockweek;  //Only for testing

    if(weekhaspassed)
      sprintf(powervalue, "%u", (daysvalue/(24*7)));  //More than week hour since the start has passed so daysvalue is ok
    else
    {
      sprintf(powervalue, "%u", ((daysvalue*60)/(clockday*24*60+clockhour*60+clockmin+1)));  //Less than on week has passed so daysvalue is too small
      result[14-strlen(powervalue)]='c';
    }
    a=strlen(powervalue);
    for (int i=1; i<=a; i++)
      result[15-i]=powervalue[a-i];
    result[15]='W';

    lcd.cursorTo(1, 0);
    lcd.printIn(msgs[3]);
    lcd.cursorTo(2, 0);
    lcd.printIn(result);
  }

  if (key == 4)       //Button corresponding to four weeks is pressed, display last four weeks usage and rating. Works the same as for minutes            
  {                  
    if(weekshaspassed)
    {
      sprintf(value, "%u", weeksvalue/1000);
      b=strlen(value);
      for (int i=0; i<b; i++)
        result[i]=value[i];

      if(weeksvalue<1000000)
      {
        result[b]=',';
        result[b+1]=((weeksvalue%1000)/100)+48;  //Get first decimal
      }
      if(weeksvalue<100000)
        result[b+2]=((weeksvalue%100)/10)+48;  //Get second decimal
      if(weeksvalue<10000)
        result[b+3]=(weeksvalue%10)+48;  //Get third decimal
    }
    else
    {
      sprintf(value, "%d", sensortickcounter/1000); //Convert powerusage to string
      b=strlen(value);
      for(int i=0; i<b; i++)
        result[i]=value[i];

      if(sensortickcounter<1000000)
      {
        result[b]=',';
        result[b+1]=((sensortickcounter%1000)/100)+48;  //Get first decimal
      }
      if(sensortickcounter<100000)
        result[b+2]=((sensortickcounter%100)/10)+48;  //Get second decimal
      if(sensortickcounter<10000)
        result[b+3]=(sensortickcounter%10)+48;  //Get third decimal
    }

    result[5]='k';
    result[6]='W';
    result[7]='h';

    if(weekshaspassed)
      sprintf(powervalue, "%u", (weeksvalue/(24*7*4)));  //More than four weeks since the start has passed so weeksvalue is ok
    else
    {
      sprintf(powervalue, "%u", ((weeksvalue*60)/(clockweek*7*24*60+clockday*24*60+clockhour*60+clockmin+1)));  //Less than four weeks has passed so weeksvalue is too small
      result[14-strlen(powervalue)]='c';
    }
    a=strlen(powervalue);
    for (int i=1; i<=a; i++)
      result[15-i]=powervalue[a-i];
    result[15]='W';

    lcd.cursorTo(1, 0);
    lcd.printIn(msgs[4]);
    lcd.cursorTo(2, 0);
    lcd.printIn(result);
  }
  timingflag= !timingflag;  //Change flag for next run in the loop
}

int get_key(unsigned int input)  //Converting input reading to corresponding button number (1-5), return -1 if no button pressed
{
  int k;
  for (k=0; k<NUM_KEYS; k++)
  {
    if (input < adc_key_val[k])
      return(k);
  }
  if (k >= NUM_KEYS)
    k=-1;     // No key pressed or an error occurred
  return(k);
}

void updateclock()  //Updating clock 0:0:0:0:0 -> 0:0:0:0:1 and so on
{
  timecounterthen=timecounternow;  //Reset time counter

  if (clocksec>=5)  //if more than 50 (seconds are only stored as 0-5) seconds have passed seconds are set to 0
  {
    clocksec=0;
    minutehaspassed=true;  //One minute has passed since the start so set flag true
    if( clockmin>=59)  //if more than 59 minutes have passed minutes are set to 0
    {
      clockmin=0;
      hourhaspassed=true;  // One hour has passed since the start so set flag true
      if(clockhour>=23)  //if more than 23 hours have passed hours are set to 0
      {
        clockhour=0;
        dayhaspassed=true;  // One day has passed since the start so set flag true
        if (clockday>=6)  //if more than 6 days have passed days are set to 0
        {
          clockday=0;
          weekhaspassed=true;  // One week has passed since the start so set flag true
          if(clockweek>=3)  //if more than 3 veeks have passed weeks are set to 0
          {
            clockweek=0;
            weekshaspassed=true;  // Four week has passed since the start so set flag true
          }
          else clockweek=clockweek+1;  //Less than 4 weeks have passed so update weeks by one
        }
        else
        {
          clockday=clockday+1;  //Less than 7 days have passed so update days by one
        }
      }
      else
      {
        clockhour=clockhour+1;  //Less than 24 hours have passed so update hours by one
      }
    }
    else
    {
      clockmin=clockmin+1;   //Less than 60 minutes have passed so update minutes by one
    }
  }
  else
  {
    clocksec=clocksec+1;   //Less than 60 days have passed so update seconds by ten
  }
}

void sensortick() //The interrupt routine
{
  powercounternow=micros();  //At what time did we se the pulse?
  //Serial.println(powercounternow-powercounterlasttime);
  sensortickcounter=sensortickcounter+1;  //Update number of pulses seen
  powerrating=3600000000/(powercounternow-powercounterlasttime);  //Calculate length of pulse and update current powerrating
  powercounterlasttime=powercounternow;  //Last time we saw a pulse was now
  sensorstate = LOW;
}
14  Forum 2005-2010 (read only) / Exhibition / Re: My electrical power meter on: November 23, 2009, 05:18:08 am
Code part two:
Code:
void loop()
{
  char result[17]="                ";  //The empty string that all printing is fed to
  timecounternow=millis();  //Read clock
  if ((timecounternow-timecounterthen)>=(9918+(timingflag*6))) //Has 10 seconds passed since last clock reading? (9915 is too little and 9916 is too much...)
  {
    updateclock();  //Update clock

      lastsecondsticks=sensortickcounter-lastsecondstickscounter;  //Update last ten seconds pulse counter
    lastsecondstickscounter=sensortickcounter;

    secondsvalue=secondsvalue-seconds[clocksec]+lastsecondsticks;  //Last minutes powerusage is updated
    seconds[clocksec]=lastsecondsticks;  //The array with last minutes powerusage is updated

    secondspowerrating=secondspowerrating-secondsrating[clocksec]+powerrating/6;  //Last minutes powerrating is updated
    secondsrating[clocksec]=(powerrating/6);  //The array with last minutes powerrating is updated

    minutesvalue=minutesvalue-minutes[clockmin]+secondsvalue;  //Last hours powerusage is updated
    minutes[clockmin]=secondsvalue;  //The array with last hours powerusage is updated

    hoursvalue=hoursvalue-hours[clockhour]+minutesvalue;  //Last days powerusage is updated
    hours[clockhour]=minutesvalue;  //The array with last days powerusage is updated

    daysvalue=daysvalue-days[clockday]+hoursvalue;  //Last weeks powerusage is updated
    days[clockday]=hoursvalue;  //The array with last weeks powerusage is updated

    weeksvalue=weeksvalue-weeks[clockweek]+daysvalue;  //Last four weeks powerusage is updated
    weeks[clockweek]=daysvalue;  //The array with last four weeks powerusage is updated
  }

  adc_key_in=analogRead(0);  //Read value to see if a button is pressed
  key=get_key(adc_key_in);  //Convert reading to a button number

  if (key == -1)  //No button is pressed, display total usage and current rating                         
  {                  
    sprintf(value, "%u", sensortickcounter/1000); //Convert powerusage value to string
    b=strlen(value);  //Get length of string containing powerusage
    for (int i=0; i<b; i++)  // Write powerusage to the output string
      result[i]=value[i];
    if(sensortickcounter<1000000)
    {
      result[b]=',';
      result[b+1]=((sensortickcounter%1000)/100)+48;  //Get first decimal
    }
    if(sensortickcounter<100000)
      result[b+2]=((sensortickcounter%100)/10)+48;  //Get second decimal
    if(sensortickcounter<10000)
      result[b+3]=(sensortickcounter%10)+48;  //Get third decimal

    result[5]='k';  //Add kWh to the output string
    result[6]='W';
    result[7]='h';

//    result[b+8]=48+clocksec;  //Print what "10 second" we are at right now. Only for testing

    sprintf(powervalue, "%d", powerrating); //Convert long powerrating to string
    a=strlen(powervalue);  //Get length of string containing powerrating
    for (int i=1; i<=a; i++)  // Write powerrating to the output string
      result[15-i]=powervalue[a-i];
    result[15]='W';  //Add W to the output string

    lcd.cursorTo(1, 0);  //Go to start of line one
    lcd.printIn("Totalt   Just nu");
    lcd.cursorTo(2, 0);  //Set cursor at start of line two
    lcd.printIn(result);  //Print powerusage and powerrating on LCD
  }

  if (key == 0)  //Button corresponding to minutes is pressed, display last minutes usage and rating
  {
    if(minutehaspassed)      
      sprintf(value, "%d", secondsvalue); //Convert powerusage to string
    else
      sprintf(value, "%d", sensortickcounter); //Convert powerusage to string

    b=strlen(value);  //Get length of string containing powerusage
    for (int i=0; i<b; i++)  // Write powerusage to the output string
      result[i]=value[i];  //Add Wh to the output string
    result[b]='W';
    result[b+1]='h';

    result[b+3]=48+(clockmin/10);  //Print what minute we are at right now. Only for testing
    result[b+4]=48+(clockmin%10);

    if(minutehaspassed)
      sprintf(powervalue, "%u", secondspowerrating); //Convert long powerusage to string
    else
      sprintf(powervalue, "%u", ((secondsvalue*360)/(clocksec)));  //Less than on hour has passed so minutesvalue is too small


    a=strlen(powervalue);  //Get length of string containing powerusage
    for (int i=1; i<=a; i++)  // Write powerusage to the output string
      result[15-i]=powervalue[a-i];  //Add W to the output string
    result[15]='W';

    lcd.cursorTo(1, 0);  //Go to start of line one
    lcd.printIn(msgs[0]);    
    lcd.cursorTo(2, 0);  //Set cursor at start of line two
    lcd.printIn(result);  //Print powerusage and powerrating on LCD
  }

  if (key == 1)       //Button corresponding to hours is pressed, display last hours usage and rating. Works the same as for minutes
  {                  
    if(hourhaspassed)
    {
      sprintf(value, "%d", minutesvalue/1000);
      b=strlen(value);
      for(int i=0; i<b; i++)
        result[i]=value[i];

      if(minutesvalue<1000000)
      {
        result[b]=',';
        result[b+1]=((minutesvalue%1000)/100)+48;  //Get first decimal
      }
      if(minutesvalue<100000)
        result[b+2]=((minutesvalue%100)/10)+48;  //Get second decimal
      if(minutesvalue<10000)
        result[b+3]=(minutesvalue%10)+48;  //Get third decimal

    }
    else
    {
      sprintf(value, "%d", sensortickcounter/1000); //Convert powerusage to string

      b=strlen(value);
      for(int i=0; i<b; i++)
        result[i]=value[i];

      if(sensortickcounter<1000000)
      {
        result[b]=',';
        result[b+1]=((sensortickcounter%1000)/100)+48;  //Get first decimal
      }
      if(sensortickcounter<100000)
        result[b+2]=((sensortickcounter%100)/10)+48;  //Get second decimal
      if(sensortickcounter<10000)
        result[b+3]=(sensortickcounter%10)+48;  //Get third decimal
    }

    result[5]='k';
    result[6]='W';
    result[7]='h';

//    result[b+8]=clockhour+65;  //Only for testing

    if(hourhaspassed)
      sprintf(powervalue, "%u", minutesvalue);  //More than one hour since the start has passed so minutesvalue is ok
    else
    {
      sprintf(powervalue, "%u", ((minutesvalue*60)/(clockmin+1)));  //Less than on hour has passed so minutesvalue is too small
      result[14-strlen(powervalue)]='c';
    }
    a=strlen(powervalue);
    for(int i=1; i<=a; i++)
      result[15-i]=powervalue[a-i];
    result[15]='W';

    lcd.cursorTo(1, 0);
    lcd.printIn(msgs[1]);
    lcd.cursorTo(2, 0);
    lcd.printIn(result);
  }
  if (key == 2)       //Button corresponding to days is pressed, display last days usage and rating. Works the same as for minutes
  {                  
    if(dayhaspassed)
    {
      sprintf(value, "%d", hoursvalue/1000);
      b=strlen(value);
      for (int i=0; i<b; i++)
        result[i]=value[i];

      if(hoursvalue<1000000)
      {
        result[b]=',';
        result[b+1]=((hoursvalue%1000)/100)+48;  //Get first decimal
      }
      if(hoursvalue<100000)
        result[b+2]=((hoursvalue%100)/10)+48;  //Get second decimal
      if(hoursvalue<10000)
        result[b+3]=(hoursvalue%10)+48;  //Get third decimal

    }
    else
    {
      sprintf(value, "%d", sensortickcounter/1000); //Convert powerusage to string
      b=strlen(value);
      for(int i=0; i<b; i++)
        result[i]=value[i];

      if(sensortickcounter<1000000)
      {
        result[b]=',';
        result[b+1]=((sensortickcounter%1000)/100)+48;  //Get first decimal
      }
      if(sensortickcounter<100000)
        result[b+2]=((sensortickcounter%100)/10)+48;  //Get second decimal
      if(sensortickcounter<10000)
        result[b+3]=(sensortickcounter%10)+48;  //Get third decimal
    }

    result[5]='k';
    result[6]='W';
    result[7]='h';

//    result[b+8]=48+clockday;  //Only for testing

    if(dayhaspassed)
      sprintf(powervalue, "%u", (hoursvalue/24));  //More than one day since the start has passed so hoursvalue is ok
    else
    {
      sprintf(powervalue, "%u", ((hoursvalue*60)/(clockhour*60+clockmin+1)));  //Less than on day has passed so hoursvalue is too small
      result[14-strlen(powervalue)]='c';
    }
    a=strlen(powervalue);
    for (int i=1; i<=a; i++)
      result[15-i]=powervalue[a-i];
    result[15]='W';

    lcd.cursorTo(1, 0);
    lcd.printIn(msgs[2]);
    lcd.cursorTo(2, 0);
    lcd.printIn(result);
  }
15  Forum 2005-2010 (read only) / Exhibition / Re: My electrical power meter on: November 23, 2009, 05:16:13 am
And the code part one:

Code:
#include <LCD4Bit_mod.h>  //Acess to LCD library
#include <stdio.h>  //Needed for sprintf conversion from long to char *
#include <string.h> //Needed for getting string lengths
LCD4Bit_mod lcd = LCD4Bit_mod(2);  //Set LCD as two line display

unsigned int timecounternow;  //Timecounters for checking if ten seconds have passed
unsigned int timecounterthen=0;
volatile unsigned long powercounternow;  //Counts time between pulses in milliseconds
volatile unsigned long powercounterlasttime=0;
byte clocksec, clockmin, clockhour, clockday, clockweek; // to keep track of time
volatile byte sensorstate = LOW;  //Start with sensor in LOW
volatile unsigned long sensortickcounter=0;  //Counts powerpulses
volatile unsigned int powerrating=0;  //Last powermeasurement

unsigned int lastsecondsticks=0; //Last minutes power usage (blinks seen last ten seconds)
unsigned long lastsecondstickscounter=0; //Counter to store time we read last ten seconds power usage

char msgs[5][17]={
  " Senaste minuten", "  Senaste timmen", "  Senaste dygnet", "  Senaste veckan", "Senaste 4 veckor"}; // Array with messages that can be displayed

int  adc_key_val[5]={
  30, 150, 360, 535, 760};  //Array for button readings
int NUM_KEYS=5;  //Number of keys available
int adc_key_in;  //To read key with
int key=-1;  // Key pressed right now, -1 if no key pressed or faulty reading

int seconds[6];  //Array that holds each powerreading done during the last minute
int minutes[60];  //Array that holds each powerusage reading done during the last hour
int hours[24];  //Array that holds each powerusage reading done during the last day
unsigned long days[7];  //Array that holds each powerusage reading done during the last week
unsigned long weeks[4];  //Array that holds each powerusage reading done during the last four weeks

unsigned int secondsvalue=0;  //Powerusage during the last minute
unsigned long minutesvalue=0;  //Powerusage during the last hour
unsigned long hoursvalue=0;  //Powerusage during the last day
unsigned long daysvalue=0;  //Powerusage during the last week
unsigned long weeksvalue=0;  //Powerusage during the last four weeks

unsigned int secondsrating[6];  //Array that holds each powerrating reading done during the last minute
unsigned long secondspowerrating=0;  //Powerrating during the last minute

char powervalue[6];  //Char array to hold power value for printing on LCD
char value[6];  //Char array to hold power usage for printing on LCD
byte a,b;  //Used to get length from strings

byte minutehaspassed=false;  //Flag to see if one minute or more has passed since start, set to false from start
byte hourhaspassed=false;  //Flag to see if one hour or more has passed since start, set to false from start
byte dayhaspassed=false;  //Flag to see if one day or more has passed since start, set to false from start
byte weekhaspassed=false;  //Flag to see if one week or more has passed since start, set to false from start
byte weekshaspassed=false;  //Flag to see if four weeks or more has passed since start, set to false from start
byte timingflag=false;  //Flag that switches between 0 and 10. Used to get tome to be more accurate

void setup()
{
  attachInterrupt(1, sensortick, RISING);  //Set upp interrupt with sensor. If we detect a change from LOW to HIGH we call sensortick
  timecounterthen=millis();  //Arduino is running, start timing now.
  clocksec=0;  //Clock is 0:0:0:0:0 (w:d:h:m:s)when started
  clockmin=0;
  clockhour=0;
  clockday=0;
  clockweek=0;
  lcd.init();  //Initialize LCD
  lcd.clear();  //Clear LCD

  for (int i=1; i<6 ;i++)  //Each array that holds powerusage is set to contain 0 in every place
    seconds[i]=0;
  for (int i=0; i<60 ;i++)
    minutes[i]=0;
  for (int i=0; i<24 ;i++)
    hours[i]=0;
  for (int i=0; i<7 ;i++)
    days[i]=0;
  for (int i=0; i<4 ;i++)
    weeks[i]=0;
  for (int i=0; i<6 ;i++)  //Array that holds powerrating is set to contain 0 in every place
    secondsrating[i]=0;
  seconds[0]=powerrating;  //We have started so set the first seconds powerrating
  //  Serial.begin(9600);
}
Pages: [1] 2