Show Posts
Pages: [1]
1  Using Arduino / Programming Questions / Re: Non resetting variable on: March 23, 2013, 05:32:25 am
Ok full code show below

void setup(){ // Setup function
  Serial.begin (9600); // Set serial to 9600


static float powerold;

void loop(){ //Main program for AC current test.
  float voltagein = 0; // define voltage input as a floating point variable and set to 0.
  float voltagemeasure = 0; // define measured voltage as a floating point variable and set to 0.
  float voltagesum=0;
  float voltagerms=0;
  float voltageavg=0;
  float currentmeasure = 0; // define measured current as a floating point variable and set to 0.
  float currentavg=0;
  float currentsum=0;
  float currentrms=0;
  float a20current=0; // define acs712 20a as a floating point varible and set to 0.
  float powerin =0;
  float powersum = 0;
  float powerreal =0;
  int current2dp=0;
  int voltage2dp=0;
  int powerreal2dp=0;

  int n=0;

    voltagein=(analogRead(A3)-114); // Set voltagein to the reading from the voltage divider input at pin A4.
    delay(1); // 1ms to make up for the phase shift.
    a20current = analogRead(A1); // Set a5current to the reading from the 5a acs712 input at pin A2.
    if(voltagein > 0){
      voltagemeasure=voltagein; // voltagesum is set to be a running total
      currentmeasure = a20current - 512;
      currentmeasure = abs(currentmeasure);
      voltagesum = voltagesum + voltagemeasure;
      currentsum = currentsum + currentmeasure;
      powerin = (voltagemeasure * currentmeasure);
      powersum = powersum + powerin;

  while (n < 500); // run 500do while cycles.

  currentavg = (currentsum/n); // Calculate average current.
  currentavg = ((currentavg)*0.0489);
  currentrms =(currentavg * 1.01 ); // Covert from average current to current rms.
  Serial.print("Current rms= "); // Print to serial.
  Serial.println(currentrms); // Print the value of current to serial monitor.

  voltageavg = voltagesum/n;
  voltageavg = voltageavg * 2.206;
  voltagerms = 1.11*voltageavg; // Covert from average voltage to voltage rms.
  Serial.print("Voltage rms= "); // Print to serial.

  powerreal = (powersum/n)*0.1030; // 0.0489 for current 2.106 for voltage.
  Serial.print("Power = "); // Print to serial.
  Serial.print("Powerold = "); // Print to serial.

  if(powerreal > 10)
    if((powerreal < (0.95*powerold)) || (powerreal > (1.05*powerold)))

  powerold = powerreal;

  delay (3000); // delay before next set of readings.


void senddata(float currentrms, float voltagerms, float powerreal)
 int current2dp=0;
 int voltage2dp=0;
 int powerreal2dp=0;
 current2dp = currentrms * 100;
 voltage2dp = voltagerms * 100;
 powerreal2dp = powerreal * 100;
 char buffer[5];
 Serial.print(itoa((current2dp), buffer, 10));
 Serial.print(itoa((voltage2dp), buffer, 10));
 Serial.print(itoa((powerreal2dp), buffer, 10));

I think you might want Static....

I did see something about static and gave it ago, but i think i put it in the wrong place after reading that article. Will give it a go inside the function this time smiley
2  Using Arduino / Programming Questions / Non resetting variable on: March 23, 2013, 05:06:35 am

I have tried searching for a bit of help but I think my wording is making it very hard for a search engine but i can't think of a better way to describe it :S

I have a system that measured power useage and then sends the power via powerline communication. However i only want it to send the data if there has been a 5% change. So far I have:

  if(powerreal > 10)
    if((powerreal < (0.95*powerold)))
    if((powerreal > (1.05 * powerold)))

  powerold = powerreal;

  delay (3000); // delay before next set of readings.


The only problem is when void loop starts for the next run powerold is reset to 0, is there anyway for it to hold the old number? It works holding the number if no data is sent, but if data is sent it resets, i think it has something to do with calling another function, however i want to keep everything in separate functions else the program is going to be really messy :S
3  Using Arduino / Installation & Troubleshooting / Arduino 0022 not starting [SOLVED] on: March 03, 2013, 08:46:03 am

I am currently doing a project using mamba shields, these only seem to work with Arduino 0022. I have arduino 0022 running on one laptop (windows XP), but when I try and launch it on another one (windows 7) the initial screen pops up

 but then disappears and nothing is opened. I did some googleing and thought that it might be because of the other computer being windows 7, so i downloaded XP mode for windows 7 and even in that mode the same thing happens. I have tried downloading another version of arduino 0022 and still got the same result. Even when using the file that is used on the working laptop it doesn't work on the other.

Any suggestions welcome.
4  Using Arduino / Networking, Protocols, and Devices / Re: [Help] Mamba Shield on: February 27, 2013, 01:46:03 pm
First time it managed to compile, once uploaded nothing happened.

Seems that I can no longer even get it to compile. I am using the sample code from

I have made some changes:

#define MOSI            11
#define MISO            12
#define SCK             13


#define MOSI            B,3
#define MISO            B,4
#define SCK             B,5

But I now get errors:

If anyone could have a try at compiling, regardless of getting it to work with a mamba shield as I am pretty sure no one actually uses these from what I have seen on the internet.
5  Using Arduino / Networking, Protocols, and Devices / Re: [Help] Mamba Shield on: February 24, 2013, 02:06:25 pm
First time it managed to compile, once uploaded nothing happened. From what I have read else were people were saying that the example code doesn't work for Arduino 1.0. There is a step-by-step guide on the litesprite website which I tried to follow, but again it is for versions before Arduino 1.0

I am still going over the example codes trying to work out what is doing what, and putting serial.print's in to see if it is getting stuck anywhere.

It's a bit confusing as the example programing has some of the bits in that I would of thought to be in a header file, so making it hard to follow the path (for me at least).
6  Using Arduino / Networking, Protocols, and Devices / [Help] Mamba Shield on: February 24, 2013, 10:57:13 am

I am currently doing a project that will include power line communication using two mamba shield:
I am using the Arduino uno R3.

My main problem is that I foolishly went ahead and bought the two mamba shield's before doing too much research on them. Turns out the internet is pretty much empty when it comes to examples/guides. Other breakout boards I have bought I managed to get working through trial and error and looking through example codes, which I can't do with this one as there isn't enough out there.

So I am just looking for anyone with experience using them just to point me in the right direction. Ultimately they will be used to send two sensor readings from one Arduino to the other. With all communication going in the one direction.


7  Using Arduino / Networking, Protocols, and Devices / Re: Wifly - Trouble joining network. on: February 23, 2013, 02:57:42 pm
In one case you use:
set wlan phrase mypassword
and in the other you use: 
set wlan mypassword

Could that be the problem?

Sorry that was a typo in the message, I have now edited the initial post. I have now managed to get connected to a network by changing the join option to option 1. I have also managed to mash together code to set the WiFly up in code without using the Spiuart terminal with manual inputs.

How ever I now have another problem, I can't seem to get the WiFly example for a web client to work. The code i am using is:

#include "WiFly.h"

Client client("", 80);

void setup() {


  if (client.connect()) {
    client.println("GET /search?q=arduino HTTP/1.0");
  } else {
    Serial.println("connection failed");
   Serial.println("End of setup.");

void loop() {
  Serial.println("Start of void loop");
  if (client.available()) {
    char c =;
  if (!client.connected()) {
  Serial.println("End of void loop");

Using the serial prints I can tell that it hangs at the if (client.connect() statement. Hopefully I have provided enough information for someone to be able to shed some light on the problem. I have tried several suggestions on other web client examples and still doesn't work.

EDIT: Solved, was missing just one line that fixed it all.
WiFlyClient client;
8  Using Arduino / Networking, Protocols, and Devices / [Solved] Wifly - Networking problems. on: February 22, 2013, 04:48:30 pm

I bought a wifly shield from sparkfun, and aim to use it post sensor information to the internet.

But the problem is getting the Wifly to connect to a network. When using SpiUartermial example from wifly library I can connect to the network by doing the following in the serial monitor:
set wlan phrase mypassword
join myssid

I then check that I am connected using:

show net

And I am connected.
However when saved and reboot the information is lost. Following this guide with additional settings from the data sheet I put the following commands through the serial monitor:
set ip dhcp 0
set ip address
set ip netmask
set wlan join 4
set wlan ssid myssid  Its a virgin media hub if that changes anything
set wlan auth 4 WAP2 auth used
set wlan chan 11
set wlan phrase mypassword

Then when I check connection  its not connected.

Its very frustrating as I can't move on with the project. I think I would prefer it if it didn't connect either way, then it would be less frustrating.

I have tried doing different dhcp settings, as well as copying ip/netmask/chan shown when connected through the first method.

I hope that is enough information, and I have done hours of internet trawling and can't find the solution.
Pages: [1]