Show Posts
Pages: [1]
1  Using Arduino / Project Guidance / Frequency Division Question on: November 13, 2012, 09:09:12 pm
Hello all,

I'm relatively new to Arduino, but learning fairly quickly.  I was wondering if it is possible to take an input logic signal, and divide it's frequency down.  My desire is to divide the frequency down by a factor of 100.  The input frequency can be as high as 1MHz. 

Any help is appreciated, thank you.
2  Using Arduino / Programming Questions / Frequency and Duty Cycle quesitons on: October 04, 2012, 11:15:53 am
Hello,

I'm wondering if there's an easy way to set the output of the Arduino's frequency to 50Hz. I understand that the output of the PWM pins is around 500Hz? Am I correct?  I'm trying to do this to drive a couple servo motors.  I also need to vary the duty cycle.  I'm not sure if I should be using digitalWrite() or AnalogWrite(). 

Right now I'm just using this:
Code:
void setup()
{
  pinMode(10, OUTPUT);
}

void loop()
{
 
  digitalWrite(10, HIGH);
  delay(10); // Approximately 10% duty cycle @ 1KHz
  digitalWrite(10, LOW);
  delay(10);
}

To output my 50Hz square wave.  Whenever I try to use pushbuttons to simulate a signal to make the duty cycle change, the duty cycle gets all messed up. 

I need to use a 5%, 7.5%, and 10% duty cycle.  Any help would be appreciated.

Thank you.
3  Using Arduino / Programming Questions / PWM and Frequency control problem on: October 03, 2012, 07:36:06 pm
Hi all,

I'm trying to control the PWM on my Arduino by pressing pushbuttons.  My code is as follows:

Code:
int turnleft = A0;
int turnright = A1;
int returnhome = A2;
int val1 = 0;
int val2 = 0;
int val3 = 0;



void setup()
{
  pinMode(A0, INPUT);
  pinMode(A1, INPUT);
  pinMode(A2, INPUT);
  pinMode(10, OUTPUT);
 
 
}

void loop()
{
  val1 = digitalRead(A0);
  val2 = digitalRead(A1);
  val3 = digitalRead(A2);
 
 
  if(val1 == HIGH)
  {
    digitalWrite(10, HIGH);
    delay(3); //Approimately 5% Duty Cycle at 50Hz
    digitalWrite(10, LOW);
    delay(17);
  }
  else
  {
    PORTC = B000000;
  }
 
  if(val2 == HIGH)
  {
   digitalWrite(10, HIGH);
    delay(2); //Approimately 5% Duty Cycle at 50Hz
    digitalWrite(10, LOW);
    delay(18);
  }
  else
  {
   PORTC = B000000; 
  }
 
  if(val3 == HIGH)
{
  digitalWrite(10,HIGH);
  delay(1);
  digitalWrite(10, LOW);
  delay(19);

else
{
  PORTC = B000000;
}

}
   

From what I understand I'm doing something called "bit banging?"  All I need to do is get 50Hz out, (Which it is doing), but I want to vary the duty cycle from 5%, 10%, and 15% by pressing pushbuttons.

When I try and use this code, I have pin 10 hooked up on the oscilloscope, but the duty cycle is varying between the three rapidly, as if all the if statements are true. 

I'm not sure what to do at this point, or what I'm doing wrong.  Any help would be appreciated.

Thank you.
4  Using Arduino / Programming Questions / Rotary Encoder Problem on: September 12, 2012, 07:35:48 pm
Hello, I hope this is the correct place to post this.  Couldn't find anywhere that involved the coding of random parts.

Alright so, I'm trying to use a rotary encoder from spark fun.  Its pretty straightforward, two outputs (A and B) and a SW output. 

Code:

//these pins can not be changed 2/3 are special pins
int encoderPin1 = 4;
int encoderPin2 = 3;
int encoderSwitch = 7;

volatile int lastEncoded = 0;
volatile long encoderValue = 0;

long lastencoderValue = 0;

int lastMSB = 0;
int lastLSB = 0;

void setup() {
  Serial.begin (9600);

  pinMode(encoderPin1, INPUT);
  pinMode(encoderPin2, INPUT);

  digitalWrite(encoderPin1, HIGH); //turn pullup resistor on
  digitalWrite(encoderPin2, HIGH); //turn pullup resistor on
  digitalWrite(encoderSwitch,HIGH);
 
 

  //call updateEncoder() when any high/low changed seen
  //on interrupt 0 (pin 2), or interrupt 1 (pin 3)
  attachInterrupt(0, updateEncoder, CHANGE);
  attachInterrupt(1, updateEncoder, CHANGE);

}

void loop(){
 
if(encoderSwitch > 0)
{
  analogWrite(A0, 255);
}



while(encoderValue  == lastencoderValue)
{
  analogWrite(A3, 255);
  analogWrite(A4, 0);
  analogWrite(A2,0);
}
if (encoderValue > lastencoderValue)
{
analogWrite(A4, 255);
analogWrite(A2, 0);

}
if (encoderValue < lastencoderValue)
{
analogWrite(A2, 255);
analogWrite(A4, 0);

}
 
  Serial.println(encoderValue);
  delay(1000); //just here to slow down the output, and show it will work  even during a delay
}

void updateEncoder(){
  int MSB = digitalRead(encoderPin1); //MSB = most significant bit
  int LSB = digitalRead(encoderPin2); //LSB = least significant bit

  int encoded = (MSB << 1) |LSB; //converting the 2 pin value to single number
  int sum  = (lastEncoded << 2) | encoded; //adding it to the previous encoded value

  if(sum == 0b1101 || sum == 0b0100 || sum == 0b0010 || sum == 0b1011) encoderValue ++;
 
 
 
  if(sum == 0b1110 || sum == 0b0111 || sum == 0b0001 || sum == 0b1000) encoderValue --;
 


  lastEncoded = encoded; //store this value for next time
}

I'm trying to verify that my code works with 4 LED's.  I have a base LED that lights up when the lastencodervalue is equal to the encoded value, and an Up and Down LED for when the encoder is turned CW or CCW.  I also have a switch LED for when the pushbutton is pressed.

Here is my problem(s)

As stated before, I want my LED's to light when they're increasing or decreasing.  A problem I'm having is that the LED will light when turned CW (causing the encoded value to go up, viewable via serial monitor), but when I turn the encoder CCW, the up LED only turns off when the count coming out of the A and B get below Zero.  (Viewable via serial monitor)

Another problem is the switch.  I thought that when the switch is pressed that it would send a high pulse in, so I tried to make a statement that was along the lines of

if (encoderSwitch == HIGH)
{
analogWrite(A0, 255) //supplies 5V to the LED on pin A0 when Pin is pressed
}

But this doesn't yield any results.

Help?

Thank you!
5  Using Arduino / Displays / Re: Serial LCD Error Problem on: September 10, 2012, 07:00:53 pm
I simply downloaded it and copy/pasted the file in libraries. 

Is this not correct?
6  Using Arduino / Displays / Re: Serial LCD Error Problem on: September 08, 2012, 05:52:55 pm
Comes up with the same error.  Thank you for trying to help though!

Any other thoughts?
7  Using Arduino / Displays / Serial LCD Error Problem on: September 08, 2012, 04:18:39 pm
Hello, I'm new here and I hope this is the right place to post this. 

I'm using this code I found to try and use my 16x2 serial LCD I bought off of Sparkfun:

Code:


 

#include <SparkSoftLCD.h>

  // LCD transmit pin
#define LCD_TX 2

SparkSoftLCD lcd = SparkSoftLCD(LCD_TX);

void setup() {

   // setup lcd
 
 pinMode(LCD_TX, OUTPUT);
 lcd.begin(9600);
 lcd.clear();

   // hidden cursor
 lcd.cursor(0);

 show_setup();

 delay(1000);

}

void loop() {

    lcd.clear();
   
      // block-style blinking cursor
    lcd.cursor(2);
   
    lcd.print("Arming");
    delay(1500);
   
    lcd.clear();
   
      // hidden cursor
    lcd.cursor(0);
   
    lcd.print("16");
    lcd.cursorTo(2,1);
    lcd.print("---------------*");
   
    delay(1000);
   
    for( byte i = 15; i >= 1; i-- ) {
       
        lcd.scroll(false);
       
        // as we scroll left, we need to move our cursor to
        // the right to continue to show our countdown
        //
        // in this way, we achieve the sense that a fuse is getting
        // shorter...
        lcd.cursorTo(1,1 + (16 - i) );
        lcd.print(i, DEC);
       
        delay(1000);
     }
   
      // make last bit disappear
    lcd.scroll(false);
    delay(500);
    lcd.clear();
    lcd.cursorTo(2, 10);
    lcd.print("(boom)");
   
    delay(2000);
   
}



void show_setup() {
 
 lcd.print("I am setting up!");

   // send cursor to 2nd row, first column
 lcd.cursorTo(2,1);

 delay(500);

 for (byte i = 0; i <= 15; i++ ) {
     delay(150);
       // scroll display to the right
     lcd.scroll(true);
 }

 delay(1500);
 lcd.print("One more moment");


 for (byte i = 0; i <= 15; i++ ) {
     delay(150);
       // scroll back to the left, revealing our new text
     lcd.scroll(false);
 }

}


I keep getting the error: "SparkSoft LCD Does not name a type."


Any advice?  Thank you!
Pages: [1]