Show Posts
Pages: [1] 2
1  Using Arduino / Programming Questions / Re: A function that won't work as expected. on: April 17, 2013, 03:39:47 am
Thank you.
My first example does the same as Henry_Best. And after a nights sleep, I realise I should change my code accordingly for the other examples to be on the safe side. Then I can use a byte variable  and I will also safe a few bytes of flash doing it.

Just about the [n+1] as PeterH mentioned. The coding is correct in this case.
I have three temperature sensors. One for air, two telescopes. I have only two heaters. My Sketch compare scope and air temperatures and adjust heaters accordingly.
I build my Sketch functionality in blocks, so I started by defining the air sensor as tempSens[0] and scope sensors as tempSens[1] and tempSens[2] as I tested and implemented. I call the function adjustTemp() at set times with millis() and to save code, I used a loop to check and adjust both sensors and heaters. Then I have several arrays that is connected to a specific sensor, like calibrate[], sensorAttached[] heaterPower[] etc, and of course they all start counting at 0. So tempSens[1] is linked to sensorAttached[0], tempdiff[0] etc. Of course with hindsight I should have used tempSens[2] for air, so that I can drop the [n+1] altogether! But for now I reckoned my project can add [n+1] faster and cheaper than I can correct my code.  smiley-wink

You've both provided me with quite some insight!
2  Using Arduino / Programming Questions / Re: Why the big difference in sketch size when using byte and integers? on: April 16, 2013, 04:18:53 pm
Ah! Thank you.
It of course makes sense about the extra operations to use an integer. I suspect a "float" will be much worse, but not tested yet! Now I understand why a short piece of code using a float, is so much bigger than using integers in much longer code. A valuable lesson indeed.


3  Using Arduino / Programming Questions / Why the big difference in sketch size when using byte and integers? on: April 16, 2013, 03:53:36 pm
Do this:
In any sketch change a single variable type between a byte and integer.

I get 8 bytes difference after verifying it. In my case from 3320 bytes down to 3312 bytes. I thought an integer is only two bytes so code size should go up to 3313 from byte to integer?
4  Using Arduino / Programming Questions / A function that won't work as expected. on: April 16, 2013, 03:37:30 pm
I have run into a problem with a function I have. I use Arduino as ISP to program an Attiny 45. Below is my current fix that is working - proving that my library work. The scopeSens1.getValue(samples, variation) is a simple routine that defines a pin, read the analogvalue and does some filtering to return an average whereby outlying values is discarded in calculating the average. I call it many times in the rest of my program.

Code:
//This function while run when there is no scope sensors attached. This is a manual mode. Left button increase temp by 25%
//right button decrease temp by 25%
void manualControl() {
  byte manualPower = 0;
  fastPulse(0); fastPulse(1);  //indicate with LED manual mode is active
  while(1) {
 
  if (scopeSens1.getValue(samples, variation) >=maxSensOut) {
    if (manualPower <= 230) {   
      manualPower += 25;
    }
    else {
      manualPower = 255;
    }
  }
  if(scopeSens2.getValue(samples, variation) >=maxSensOut) {
    if(manualPower >= 25) {
      manualPower -= 25;
    }
    else {
      manualPower = 0;
    }
  }

  analogWrite(heaterPin[0], manualPower);
  analogWrite(heaterPin[1], manualPower);
  delay(250);
  }
}

As you can see in the working code I used a byte value for power and I make sure that it does not go out of bounds, i.e. 0 - 255 only since it is used for analogWrite.

And here is the problematic code I first used:
Declaring "manualPower" as a byte below, fail, because the value goes out of bounds. But at least then code causes the output to step up and down as expected, but then "rolls over" because it can become out of range for a byte. (A valuable lesson I learned the hard way!  smiley-mr-green)

Anyway, so I changed the variable to an integer as below. And then it all goes wrong. Pressing a button to step the power up, both outputs goes full high immediately, releasing the outputs goes low immediately. Is this normal behaviour?

Code:
//This function while run when there is no scope sensors attahced. This is a manual mode. Left button increase temp by 25%
//right button decrease temp by 25%
void manualControl() {
  int manualPower = 0;
  fastPulse(0); fastPulse(1);
  while(1) {
 
  if (scopeSens1.getValue(samples, variation) >=maxSensOut) {
 
      manualPower += 25;

    if (manualPower > 255) {
      manualPower = 255;
    }
  }
  if(scopeSens2.getValue(samples, variation) >=maxSensOut) {
    if(manualPower >= 25) {
      manualPower -= 25;
    }
    if (manualPower < 0) {
      manualPower = 0;
    }
  }

  analogWrite(heaterPin[0], manualPower);
  analogWrite(heaterPin[1], manualPower);
  delay(250);
  }
}


And here is a similiar piece of code I use in the same Sketch that work as expected.
In this case "powerLevel" does the same. I've higlighted the specifics in red below. It is under the comment, "Power handling statements."


Code:
void adjustTemp(int n) {
  int powerLevel = heaterPower[n];
  //next two "if" statements set logic when a sensor is not attached. Possibly future can have the logic in the getTemp() function.
  //this is only when one scope Sensor is missing, it will set it the same as plugged in sensor
  if(sensorAttached[n] == false && n == 0) {   //if sensor 1 is not attached, set its value to the attached sensor.
    tempSens[n+1] = tempSens[2];
  }
  if(sensorAttached[n] == false && n == 1) {    //if sensor 2 is not attached, set its value to the attached sensor.
    tempSens[n+1] = tempSens[1];
  }
 
  tempSens[n+1] -= tempDiff[n];  //subtract the required difference to "load" the scope sensor. Thus scope sensor appear to be "colder"
  tempSens[n+1] += calibrationValue[n]; //add the initial calibration value


 //Power handling statements below   
  [color=red][font=Verdana]if (tempSens[n+1] >= tempSens[0]) {      //will decrease power in steps of 25 or 10%
    powerLevel = powerLevel - 25; 
    if (powerLevel < 0) {
      powerLevel = 0;[/font][/color]
    }
  }
 
  if (tempSens[n+1] < tempSens[0])  {  //increase power in steps of 25 or 10%
   [color=red] powerLevel += 25;
    if (powerLevel > 255) {
      powerLevel = 255;[/color]
    } 
  }
  //Below "if" will set heater outputs to 50% permanent on, when no scope sensors is connected.
  if (sensorAttached[0] == false && sensorAttached[1] == false) {
    powerLevel = 128;
  }

 
// heaterPower[n] = powerLevel;   // -- previous method without full power override
//set full power override mode.
 if (heaterFullPower[n] == true) {
   analogWrite(heaterPin[n], 255);   //write full power to heater
 }
 else {
   heaterPower[n] = powerLevel;
   analogWrite(heaterPin[n], heaterPower[n]);
 }
}

I would be glad to get some idea what is the difference between the two functions, and which one is the better way of ensuring steps does not overrun the max value of analogWrite().
Regards
PS. I am unable to "Copy for Forum" from the IDE. Lots of complaints about Java dispatchthread...
5  Using Arduino / Installation & Troubleshooting / Re: Compilation became incredibly slow on: April 15, 2013, 08:55:34 am
Execution is normal. When I click compile, it becomes slow. The same sketch that took a few seconds to compile now take several minutes. I did re-install Jave, unblocked Arduino in my Firewall etc, But since two weeks ago, prior to upgrading from 1.0.1 to 1.0.4 compiling takes much longer than usual.
Thank you
6  Using Arduino / Installation & Troubleshooting / Re: Compilation became incredibly slow on: April 15, 2013, 05:00:41 am
Same problem.
I set compiling to verbose and see lots of these lines
Quote
warning: this is the location of the previous definition

and these kinds
Quote
warning: "BIN" redefined
In file included from c:/documents and settings/jacques/my documents/arduino/arduino-1.0.4-windows/arduino-1.0.4/hardware/tools/avr/lib/gcc/../../avr/include/avr/iotn45.h:38,

and these kind of things, I guess referring to a problem within libraries etc. that came with Arduino.
Quote
C:\Documents and Settings\JACQUES\My Documents\Arduino\arduino-1.0.4-windows\arduino-1.0.4\hardware\tiny\cores\tiny\wiring_pulse.c:53: warning: cast to pointer from integer of different size

And even these kinds
Quote
c:/documents and settings/jacques/my documents/arduino/arduino-1.0.4-windows/arduino-1.0.4/hardware/tools/avr/lib/gcc/../../avr/include/avr/delay.h:36:2: warning: #warning "This file has been moved to <util/delay.h>."

Is that what is making my code so slow? I am using stock standard IDE 1.0.4.


7  Development / Other Software Development / Re: ATtiny85 @ 16mhz with internal High Frequency PLL Clock on: April 15, 2013, 04:15:29 am
Will this work with the Attiny45?
8  Development / Other Software Development / Re: CodeBlocks Arduino IDE - real C++ IDE for Arduino development (rev.20130401) on: April 15, 2013, 04:02:43 am
Thanks for checking out my code IJM. Is there any specific settings you use? I did not change anything from the download.

Perhaps my code must be in a specific file structure or location? (A previous version of C++ IDE did not like spaces in the file paths, but so far I did not have that problem with C::B or Arduino.) I use the same "Sketches" folder as my Arduino and the "standard" files in the same Arduino "Sketches" folder does generate the hex file and I can upload it directly with Arduino builder, even without the specific #include <Arduino.h> line. If you can genererate the hex, then obviously I am doing something wrong.

Regarding porting to the Attiny, I am already using the Arduino as an ISP sucessfully. I did not make myself clear. Apologies for that. What I like to know if it is possible to set C::B in a similar way to program the Attiny. The builder does have an option "ArduinoISP" but I don't have a clue what to do from there, except I get the same "cannot generate hex" problem selecting it. I want to use a specific Attiny library and use the Attiny at 8MHz internal oscillator. I don't have any AVR experience, but my guess is it will be in that fashion through the AVR interface? My current workaround is to write in C::B, save as .ino, re-open Arduino, load code, upload code. In this case I find it easier to just use the Arduino IDE, but it has become blazingly slow to compile.

Regards
9  Development / Other Software Development / Re: CodeBlocks Arduino IDE - real C++ IDE for Arduino development (rev.20130401) on: April 11, 2013, 07:10:37 am
I also get "Error generating hex file" trying to use Arduino builder.

The blink example compile and upload to my Uno, so I guess the environment is ok.

Using ArduinoBuilder.exe fails at 92% build. I did add the line #include <Arduino.h> to my project. Using the Arduino IDE my example work fine.
Here is my project code. It will be a simple test to compare battery discharge levels for my daughter's science project.

Quote
#include <Arduino.h>
//unsigned long int timer;
//unsigned long int timeOut;
//unsigned long timeNow;
int batteryStartVoltage;
int batteryCurrentVoltage;
int batterySteps = 20;

void setup() {
  pinMode(A0, INPUT);// put your setup code here, to run once:
  Serial.begin(9600);
  batteryStartVoltage = analogRead(A0);
  Serial.print("Start voltage: ");
  Serial.print(batteryStartVoltage);
  Serial.print("  Start time: ");
  Serial.println(millis());
  Serial.println("***************");
}

void loop() {
  // put your main code here, to run repeatedly:
//  timeNow = millis();
  batteryCurrentVoltage = analogRead(A0);

  if(batteryStartVoltage - batteryCurrentVoltage >= batterySteps) {
   Serial.print("Time:  ");
   Serial.print(millis());
   Serial.print("    Voltage: ");
   Serial.println(batteryCurrentVoltage);
  Serial.println("***************");
  batteryStartVoltage = batteryCurrentVoltage;
  }

}


Does anyone know how to use the Attiny45/85 hardware with codeblocks when using the Arduino as ISP? I am working on three projects that will end up on Attiny45.

Regards
10  Using Arduino / Displays / Geocaching with morse code on: December 11, 2012, 09:16:32 am
I hope this is something that someone will find usefull.

I am building a Geocache that will send the final cache location coordinates via morse code. I used a bare bones Attiny45 programmed with the Arduino ISP to accomplish my design.

The circuit have two LED's, one for "dit" and one for "dah" as well as one tone output and a serial output for reading the EEPROM. There is one input that will decide whether the coordinates will be pulsed out or the EEPROM byte will be pulses and send out via serial. The EEPROM byte is incremented each time the circuit is powered on. A magnetic switch will be used for switching the morse flasher on or off.
After a set amount of repeats, the Attiny will go into a sleep mode until the next restart just in case some cacher forget to remove the magnet.

I don't have a circuit diagram yet, not anything useable on this forum in any case. The box is an electrical outlet box, with a dummy front. The two LEDS is hot-glued unto to holes, this I have an opaque waterproof LED view holes. I can add pictures, but I think most folks will be happy with just seeing the code.
Perhaps there is some tweaks I can do to the code to make it work better.

I planned to post the code, but the forum said I am past the 9500 character limit. So I only add the main block.
If interested, I will try and make a plan to add more code.
Regards

Quote
/*
Morse code generator by Jacques Bayman
Will be used in a puzzle geocache where the final
coordinates will be send via morse code to the geocacher
Coordinates will be stored in a constant array.
 >> Add possible battery monitor on analogue to check for low battery 
 >> need hardware V-ref  -- unsure if worth it, will play with it though
 
*/


#define dotPeriod 250
#define dashPeriod (dotPeriod*3)
#define relaxTime (dotPeriod)
//#define letterSpace (dotPeriod*2)
#define wordSpace (dotPeriod*6)
#define ledPin_dot 1           //an LED on Attiny pin 1 or physical pin 6
#define ledPin_dash 2          //an LED on Attiny pin 2 or physical pin 7
#define freq 987        
#define tonePin 0             //a speaker on Attiny pin 0 or physical pin 5
#define ROM_Pin 4             //this is used to check if serial comms is required to read EEPROM
                              //physical pin 3. This order so can be initialised in loop
#define serial_out 3         //the serial TX pin physical pin 2
#define timeToStop 1        //used to stop LED's and speaker if the someone forget to remove the magnet.
                             //set a max for how many time the loop must run. This is to save some battery life

#include <EEPROM.h>
#include <avr/sleep.h>  //used to reduce power on the AVR - this where timeToStop comes in.
int romByte;
int stopCounter = 0;
const int phrase[] = {3,4};//,2,3}  //enter a numerical code here. Only
                              //numbers allowed
  
void setup() {
  Serial.begin(9600);
// The loop below set all pins except ROM_Pin as outputs. Hopefully shorter code  
  for(int i = 0; i < 4; i++) {
    pinMode(i, OUTPUT);
    digitalWrite(i, HIGH);
  }
  digitalWrite(tonePin, LOW);  // used to lower the tone pin back to low - not absolutely neccesarry
  pinMode(ROM_Pin, INPUT);     // this pin check if its the ROM being read. Active high means yes its the ROM
  
  warble();   //call a tone function to let user know the system is working. Also add a convenient delay
              //to the system making sure power switch is in correct place before attempting to increment the
              // ROM
              
//Increment the rom with every power up.  
 romByte = EEPROM.read(0);
  if (romByte != 255) {    //at 255, the ROM should go back to zero
    romByte++;
  } else {
    romByte = 0;
  }
  EEPROM.write(0, romByte);
}

void loop() {
  int i = 0;
                   
 //play thecoordinates in morse code while the ROM_Pin is low
  while (digitalRead(ROM_Pin) == LOW) {
    playLetter(phrase);
    delay(wordSpace);
    i++;

    // sizeof() return the number of bytes in an array, thus to get the correct number in an array, divide by
    //the sizeof an int. An int is two bytes. Also see serialOn function where it is also used to read ROM size.
    if (i >= sizeof(phrase) / sizeof(int)) {   //begin all over if there is something going on
      i = 0;
      stopCounter++;
      if (stopCounter >= timeToStop) {
   digitalWrite(tonePin, LOW);  //strictly not needed, but I like to make sure...
 //  Below code refers to this page http://www.gammon.com.au/forum/?id=11497 it is used to reduce power in case
 //  someone forgets to remove the magnet from the switch
   ADCSRA = 0;    // this will turn of the ADC - it does use quite a bit of power
   set_sleep_mode(SLEEP_MODE_PWR_DOWN); 
   sleep_enable();
   sleep_cpu();
      }
    }
  }
  serialOn();   // will only execute when the ROM_Pin is HIGH
}


11  Using Arduino / General Electronics / Re: Hi frequency noise on op-amp on: May 31, 2011, 04:15:58 pm
Mmmm, now that I think about it, it may be 16Mhz... smiley-confuse I may have very well measured only half a cycle, thus the 33Mhz, quite common with me. Will re-check tomorrow if I get some time. Thanks
12  Using Arduino / General Electronics / Hi frequency noise on op-amp on: May 31, 2011, 04:02:12 pm
I've build a small op-amp circuit that is being driven by the stock standard "tone follower" sketch in the examples. No problem there. As expected it outputs a square wave. (Arduino Uno)

I've noticed a very high frequency on the positive pulses with my oscilloscope. I got about 33Mhz 2mV perfect sine superimposed on the trace. Even straight from the board I get this noise. My scope's grounded properly and I use GND on the Arduino as my scope ground as well. I'm sure this is the clock frequency of the UNO.

What is a quick and safe way of blocking the stray signal from my output? Even the blinky LED have this signal superimposed on the pulses. Its no serious matter right now as I'm mostly fooling around, but it did give me some problems driving my op amp. (For some reason, that signal goes in one side and out the other, although I think that is more to do with my breadboard layout than anything else.)
Thank you
13  Using Arduino / Sensors / Re: Tilt only sensor for telescope. on: May 26, 2011, 03:11:27 pm
Thanks. I have some of these lying around somewhere. Lots of usefull stuff in old DVD players! I'm more inclined to use an LED - LDR combination and then the analogue read routine. I'm way too much of a noob to actually write my own code, so unless I copy and modify, I'm lost in any case.
14  Using Arduino / Sensors / Re: Rotary encoders and interrupts on: May 26, 2011, 03:40:23 am
Did anyone perhaps tried an optical mouse to read a shaft? I.e. just mount the sensor part of the mouse above the shaft, and as it turn, use the output to determine its direction and speed? Can one actually make the Arduino to read a mouse? (I'm toying with the idea to read the shaft of my telescope, which is about 20mm diameter.)
15  Using Arduino / Sensors / Re: Tilt only sensor for telescope. on: May 26, 2011, 03:35:46 am
Thanks for the ROHM link willnue. Its a very interesting device.
As for the pot, it was my more or less my original plan. I'm trolling for fresh ideas. Folks can come up with incredibly good ideas. Sometimes a nudge in the right direction is all that is needed.

I remember many moons ago I fixed an old electric organ. The volume control was as expected on the foot pedal. What I did not expect was that there was a lightbulb on one side with a triangular slit on the foot pedal, and on the other side there was a photo resistor. As you push the pedal deeper, the wider section of the slit moves past the bulb and more light falls on the photoresistor, thus increasing the volume. I would have used a pot if I designed it, but this solution was quite novel. (Of course in the good old days a major problem was scratchy sound when you turn a volume control. The cause was dust inside the pot. This volume control clearly would never scratch due to dust.) Of course I don't want light when stargazing, but maybe an infrared led, receiver and a slit in a piece of plastic... Perhaps explore along that line? Don't know what I do want to do. I've got time to explore other ideas.

If I knew how to get the output on an optical mouse to be interpreted by the Arduino, I may stick the sensor on the shaft, and use that output to drive my tilt sensor. I think I love the new ideas more than the actual solution...  smiley-wink
Pages: [1] 2