Show Posts
Pages: [1] 2 3
1  Using Arduino / Programming Questions / Re: Interrupts and timer1 pulse output on: January 10, 2013, 09:38:47 am
Well I've mashed this together and it appears to do what I want but is it well written? How should it be written?
Note: There is a bit of code in the 'void loop()' to form a square wave oscillator.
Note 2: I'm trying to create a single period using timer1. These periods are between 432uS and 3.7mS in size and are to control one of the digital pins.

Code:
// Arduino timer CTC interrupt example
//
// avr-libc library includes
#include <avr/io.h>
#include <avr/interrupt.h>
#define LEDPIN 13
#define pulse 7

int grad = 4;
int b = OCR1A;
long previousMillis = 0;
long interval = 1000;
unsigned long currentMillis = millis();
int ledState = LOW;

void setup()
{
  Serial.begin(9600);
  pinMode(pulse, OUTPUT);
  digitalWrite(pulse, LOW);
pinMode(LEDPIN, OUTPUT);
digitalWrite(LEDPIN, LOW);
attachInterrupt(0, start_timer, FALLING);

}

void loop()
{
unsigned long currentMillis = millis();
  if(currentMillis - previousMillis > interval) {    
    previousMillis = currentMillis; // save the last time you blinked the LED    
    if (ledState == LOW)// if the LED is off turn it on and vice-versa:
      ledState = HIGH;
    else
      ledState = LOW;    
    digitalWrite(pulse, ledState); // set the LED with the ledState of the variable:
  }
}

void start_timer()
{
digitalWrite(LEDPIN, HIGH);
cli();          // disable global interrupts
TCNT1 = 0;
TCCR1A = 0;     // set entire TCCR1A register to 0
TCCR1B = 0;     // same for TCCR1B
OCR1A = 62; //3.7 mS
//OCR1A = 11; //432 mS
TCCR1B |= (1 << WGM12); // turn on CTC mode:
TCCR1B |= (1 << CS10); // Set CS10 and CS12 bits for 1024 prescaler:
TCCR1B |= (1 << CS12);
TIMSK1 |= (1 << OCIE1A); // enable timer compare interrupt:
sei(); // enable global interrupts:
}

ISR(TIMER1_COMPA_vect)
{
 digitalWrite(LEDPIN, LOW);
}

Many thanks for any help.
2  Using Arduino / Programming Questions / Re: Interrupts and timer1 pulse output on: January 08, 2013, 12:01:35 pm
PaulS,

I thought a timer was the right thing to use as my understanding is that while using the timer the Arduino could do other tasks.

In the worst case; of the 20mS period between mains cycles 4mS would be used timing the longest pulse leaving 15mS to send data to the PC and accept button imputs to increase/decrease the pulse length. The pulses which range between 432uS and 4mS are to sent to an rotary electromagnetic load used as a brake. There are about 5 other lots of data sent to the PC.

It could well be done like you suggest I just thought I'd try using a timer because it has one. It has proved more difficult than I thought though.

Many thanks
3  Using Arduino / Programming Questions / Re: Interrupts and timer1 pulse output on: January 08, 2013, 11:14:37 am
PaulS, thanks for getting back.

By every cycle I mean, every cycle of the main supply, 50Hz here in the UK.

I detect the zero crossing using an opto-isolator as the volts swing negative and go through zero, at this point I need timer1 to run and one of the Arduino digital outputs go (HIGH), then after a period (between 432uS and 4mS) of time, that same digital output should go LOW.

The Serial.print in the ISR is just there to show that actually went to the ISR and will be removed when I can get it working. Is there a better way?

Sorry if my original post was not clear and I hope this has improved things. I knew what I meant and just assumed others would as well. Sorry again :-(
4  Using Arduino / Programming Questions / Interrupts and timer1 pulse output on: January 08, 2013, 09:31:52 am
Hi, been struggling with this for a few days now. I need to trigger an Interrupt on pin change so have used attachInterrupt(0, st_timer, FALLING) which seems to work. This needs to trigger timer1 to give a single pulse varying between 432 uS and 4mS. The pulses are triggered every 20mS (50Hz).

How do I get timer1 to produce a single pulse which gets triggered every cycle unless I decide to stop them? I don't see this as PWM.

Timer1 use defeats me.

Code:
// Arduino timer CTC interrupt example
//
// avr-libc library includes
#include <avr/io.h>
#include <avr/interrupt.h>
#define LEDPIN 13
#define trigger 2
int d = 0;
void setup()
{
  Serial.begin(9600);
pinMode(LEDPIN, OUTPUT);
//digitalWrite(LEDPIN, HIGH);
pinMode(trigger, INPUT);
digitalWrite(trigger, HIGH);
// initialize Timer1
cli();          // disable global interrupts
TCCR1A = 0;     // set entire TCCR1A register to 0
TCCR1B = 0;     // same for TCCR1B

// set compare match register to desired timer count:
OCR1A = 57;

// turn on CTC mode:
TCCR1B |= (1 << WGM12);

// Set CS10 and CS12 bits for 1024 prescaler:
TCCR1B |= (0 << CS10);
TCCR1B |= (0 << CS12);

// enable timer compare interrupt:
TIMSK1 |= (1 << OCIE1A);

// enable global interrupts:
sei();
attachInterrupt(0, st_timer, FALLING);
}

void st_timer()
{
  Serial.println("st_timer");
  detachInterrupt(0);
  digitalWrite(LEDPIN, !digitalRead(LEDPIN));
  TCCR1B |= (1 << CS10);
  TCCR1B |= (1 << CS12);
}

void loop()
{
// main program
}

ISR(TIMER1_COMPA_vect)
{
  Serial.println("timer1_comp");
digitalWrite(LEDPIN, !digitalRead(LEDPIN));
TIMSK1 = (0 << TOIE1);//disable Timer1 overflow interrupt
TCCR1B |= (0 << CS10);
TCCR1B |= (0 << CS12);
}

Many thanks
5  Using Arduino / Displays / ST7565 Mega 2560 difficulty uploading on: July 18, 2012, 05:07:03 am
Hi,

I have a "Graphic ST7565 Positive LCD (128x64) with RGB backlight" from HobbyTronics and a Mega 2560 but I'm having difficulty uploading the sketch. The full sketch uploads on an UNO.

I have added code line by line to try to find where it fails and it appears to be at glcd.clear(); . Here is the cut down code
Code:
#include <ST7565.h>

#define BACKLIGHT_LED 10

// pin 9 - Serial data out (SID)
// pin 8 - Serial clock out (SCLK)
// pin 7 - Data/Command select (RS or A0)
// pin 6 - LCD reset (RST)
// pin 5 - LCD chip select (CS)
ST7565 glcd(9, 8, 7, 6, 5);

#define LOGO16_GLCD_HEIGHT 16
#define LOGO16_GLCD_WIDTH  16

//######################bitmaps images large block
static unsigned char __attribute__ ((progmem)) Hbmp[]={
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, };
//######################bitmaps images small block
static unsigned char __attribute__ ((progmem)) Lbmp[]={
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0xf8, 0xf8, 0xf8, 0xf8, 0xf8, 0xf8, 0xf8, 0xf8, 0xf8, 0xf8, 0xf8, 0xf8, 0xf8, 0xf8, 0xf8, };

//#####################define sensor pins
int sen1 = 2;
int sen2 = 3;
int sen3 = 4;
int sen4 = A4;
int sen5 = A5;

//#####################define touch screen pins
#define yLow  17
#define yHigh 15
#define xLow  16
#define xHigh 14

const int ymax = 800;
const int ymin = 10;

void setup()   {               
  Serial.begin(9600);
 
  // turn on backlight
  pinMode(BACKLIGHT_LED, OUTPUT);
  digitalWrite(BACKLIGHT_LED, HIGH);
 
 pinMode(sen1, INPUT);
 pinMode(sen2, INPUT);
 pinMode(sen3, INPUT);
 pinMode(sen4, INPUT);
 pinMode(sen5, INPUT);
 
   // initialize and set the contrast to 0x18
  glcd.begin(0x18);
[color=red]glcd.clear();[/color]
 
}

void loop()
{
 
}

This code compiles OK but when Upload is selected it takes for ever and I receive error messages:
Code:
avrdude stk500v2_ReceiveMessage(): timeout
avrdude stk500v2_getsync(): timeout communicating with programmer

I don't understand why it fails to upload with glcd.clear(); to a Mega but uploads and works with an UNO or a Duemilanove.

Any help much appreciated.
6  Using Arduino / Programming Questions / Re: Port Mapping on: October 24, 2011, 10:03:37 am
Thanks to both of you, now I understand a little better.

To jraskell, I had seen "Bit Math Tutorial" but dismissed it before reading further down thinking it not relevant.

 smiley
7  Using Arduino / Programming Questions / Re: Port Mapping on: October 24, 2011, 08:57:06 am
Here http://www.arduino.cc/en/Reference/PortManipulation recommends:
Code:
DDRD = DDRD | B11111100;  // this is safer as it sets pins 2 to 7 as outputs
                 // without changing the value of pins 0 & 1, which are RX & TX
I don't understand the difference between DDRD = DDRD | B00000000; and:
Code:
DDRD = 0x00;

Thanks again

8  Using Arduino / Programming Questions / Re: Port Mapping on: October 24, 2011, 08:32:58 am
PaulS

Thanks for this.

If I change 7 to a high it shows up, great.

What should 0 & 1 be set to? Would xxxxxx01 setting TX to an output and RX to an input be correct?
9  Using Arduino / Programming Questions / Port Mapping on: October 24, 2011, 08:17:04 am
I'm trying to read 6 digital inputs to see if their state changes.

I have digital 2 to 7 inclusive held high or low:
2 = high
3 = high
4 = low
5 = low
6 = high
7 = low

My code is very basic at the moment but I do not understand why the state of 7 is not displayed in Serial Monitor.
Code:
int val = 0;
int prevState = 0;

void setup() {
DDRD = 0x00;
Serial.begin(9600);
}

void loop(){
prevState = val;
val = PIND;
if (val != prevState) {
Serial.println(val,BIN);
}
delay(1000);
}

I see 1001111 in Serial Monitor. I realise the left most two bits are the RX and TX pins so to me are irrelevant  but only 5 data bit are visible.

Why is the bit 7 missing??

Thanks for any help.
10  Using Arduino / Interfacing w/ Software on the Computer / php_serial.class.php problem on: March 04, 2011, 06:09:39 am
I am using the php_serial.class.php script to control an Arduino Duemilanove connected to /dev/ttyUSB0 on my local web server, and controlling it from another PC.

When I start the server the PWR LED light. Then if I try to send commands from the web browser the RX and the L LED's flick momentarily on the Arduino but there is no change of state to the LED's connected to the digital outs.

But if I start the Arduino IDE on the server and its 'Serial Monitor' I can the control the LED's on the digital outs from my web browser, the RX and now the TX (but not the L) LED's flicker and the data returned can be viewed in the 'Serial Monitor'.

If I then close the 'Serial Monitor' and the 'Arduino IDE' I can control the LED's from the web browser.

It seems to me the something is changed once the 'Serial Monitor' has been started. Has anybody else experienced this and what do I need to change. I have looked at many posts and tried some changes but none have overcome the problem?

The server is running Xubuntu 10.04 Kernel 2.6.32-29

Thanks in advance

Tony
11  Forum 2005-2010 (read only) / Syntax & Programs / Re: Arduino ethernet webclient php script and mysql on: January 23, 2011, 04:56:49 pm
Hi,

I can get data by sending command http://192.168.1.19/?a=getTemp1 from my web browser but how do you do it by .php?

I've tried this:
Code:
<html>
The temperature is <?php echo $_GET["http://192.168.1.19/?a=getTemp1"]; ?>
</html>

The webpage then displays:
Code:
The temperature is

but the temp vale is never displayed.

Has anybody an ideas on the code I require?

Thanks
12  Forum 2005-2010 (read only) / Syntax & Programs / Re: Arduino ethernet webclient php script and mysql on: January 09, 2011, 10:49:04 am
@ vinhtvu2. Sorry I haven't been back to this post for a while other matters have cropped up. Your work looks very interesting I'll be back soon.
13  Forum 2005-2010 (read only) / Syntax & Programs / Re: Arduino ethernet webclient php script and mysql on: December 22, 2010, 06:00:01 am
Thanks PaulS,

I took the code from reply #29 which zoomkat kindly supplied. I couldn't understand how it worked, I presume the code goes after this
Code:
for(;;);

    }
but still in the void loop().

This may explain why the Serial Monitor didn't show the results I expected.

Thanks again
14  Forum 2005-2010 (read only) / Syntax & Programs / Re: Arduino ethernet webclient php script and mysql on: December 22, 2010, 05:23:50 am
zoomkat, thanks for info, I've never understood how ethernet works, this is all very interesting but I do have a better understanding.

I'm still not sure about this bit of code:
Code:
if (!client.connected()) {
     client.stop();
    
    Serial.print(readString);
    Serial.println();
    Serial.println();
    
    int d1 = readString.indexOf('<');
    int d2 = readString.indexOf('>');
    readString1 = (readString.substring(d1+1,d2));
    Serial.println(readString1);

    if(readString1 == ("heater1=on")) {
      digitalWrite(7, HIGH);    // set the LED on
    
    }
    
Does it not say:- If the client is not connected stop? But how does it ever manipulate the next lines of code
Code:
Serial.print(readString);
    Serial.println();
    Serial.println();
    
    int d1 = readString.indexOf('<');
    int d2 = readString.indexOf('>');
    readString1 = (readString.substr
etc, etc....
as they are all within the same {  }.

If the client is connected will it not jump straight to here?
Code:
for(;;);
    
    [glow]}[/glow]
 }



15  Forum 2005-2010 (read only) / Syntax & Programs / Re: Arduino ethernet webclient php script and mysql on: December 21, 2010, 03:13:24 pm
I'm afraid this is just not working out for me  :'(
The code in the Arduino is this:
Code:
//zoomkat 12-21-10
//simple ethernet client test code
//for use with IDE 0021 and W5100 ethernet shield
//modify the arduino lan ip address as needed
//open serial monitor to see what the arduino receives
//push the shield reset button to run client again

#include <SPI.h>
#include <Ethernet.h>
String readString, readString1;

byte mac[] = { 0x90, 0xA2, 0xDA, 0x00, 0x11, 0xAC };
byte ip[] = { 192, 168, 1, 19 };
byte server[] = { 192,168,1,20 };

Client client(server, 80);

void setup()
{
  Ethernet.begin(mac, ip);
  Serial.begin(9600);
//pinMode;
pinMode(7,OUTPUT); digitalWrite(7,LOW);  // I use this pin as GND for the LED.
pinMode(8,OUTPUT); // Sample output, unable to use built-in LED at pin 13 because Ethernet Shield uses pins 10,11,12,13.
  
  Serial.println("starting simple arduino client test");
  Serial.println();
  Serial.println("connecting...");

  if (client.connect()) {
    Serial.println("connected");
    client.println("GET /update_db.php?celsius=45 HTTP/1.0");
    client.println();
  } else {
    Serial.println("connection failed");
  }
}

void loop()
{
  if (client.available()) {
    char c = client.read();
    readString += c;
  }
        
  if (!client.connected()) {
     client.stop();
    
    Serial.print(readString);
    Serial.println();
    Serial.println();
    
    int d1 = readString.indexOf('<');
    int d2 = readString.indexOf('>');
    readString1 = (readString.substring(d1+1,d2));
    Serial.println(readString1);
 
    if(readString1 == ("heater1=on")) {
      digitalWrite(7, HIGH);    // set the LED on
    
    }
    
    Serial.println("==================================");
    Serial.println("data from server:");
    Serial.println();
    Serial.println(readString1);
    Serial.println();
    Serial.println("==================================");
    Serial.println();
      
    for(;;);
    
    }
 }
 

First Question.
Can you explain "if (!client.connected()) {
     client.stop();" as used in the above code please. Won't it miss out the next part
Code:
Serial.print(readString);
    Serial.println();
    Serial.println();
    int d1 = readString.indexOf('<');
    int d2 = readString.indexOf('>');
    readString1 = (readString.substring(d1+1,d2));
    Serial.println(readString1);
 
    if(readString1 == ("heater1=on")) {
      digitalWrite(7, HIGH);    // set the LED on

Second Question.
Shouldn't I be able to type
Code:
http://192.168.1.19/<heater1=on>
and hit Enter to turn the LED on?
Pages: [1] 2 3