Show Posts
Pages: [1]
1  Using Arduino / Motors, Mechanics, and Power / Re: DC motor PWM MOSFET problem writing code to allow five different speed. on: January 02, 2013, 11:07:40 am
I know because the simulator displays a value next to the output pin. When I used a piece of source code which cycled the motor speed periodically it displayed the expected values.

Ok so something like

Code:
{
 if Serial.available(){
    byte incomingbyte = Serial.read();
 motorSpeed = (incomingbyte -48)*51;
    analogWrite(motorPin, motorSpeed);
   
 
}}



in my void loop should rectify this?
2  Using Arduino / Motors, Mechanics, and Power / DC motor PWM MOSFET problem writing code to allow five different speed. on: January 02, 2013, 11:00:39 am
Hi

I'm trying to write a piece of code which will allow me to enter a number from 0-5 on my computer keyboard which will then through the use of PWM select drive my motor at given speed.

Code:

//Program by Jeremy Blum
//www.jeremyblum.com
//Test Motor Speed Using a Transistor

//Define Pins
int motorPin = 6;
int motorSpeed = 0;
void setup()
{
  Serial.begin(9600);
  //Set the PWM Motor pin as an output
  pinMode(motorPin, OUTPUT);
}

void loop()
{
 
    byte incomingbyte = Serial.read(); // this is value entered in serial monitor
 motorSpeed = (incomingbyte -48)*51; // subtracting 48 to get decimal value. Multiply by 51 as 255/51 = 5
    analogWrite(motorPin, motorSpeed); // write to the motor pin the motorSpeed
   
 
}}


This is for a university project so unfortunately I only have an arduino simulator here at home to test with but I'm having the following problems:

This doesn't seem to drive the motor at the desired speed
It doesn't loop

Also I want to hold the speed selected until a new value is entered how would this be achieved?

Hope this is clear, thank you for your help
3  Using Arduino / Programming Questions / Re: Using a temperature sensor I2C. Where do registers come into code? on: December 12, 2012, 07:21:33 pm
My completed code if it's of interest / use to other people looking at this topic for help.
Code:
// This code reads a temperature sensor using an I2C bus. Presents data in serial monitor.
// Uses wire library
// Uses arduino, I2C bus and temperature sensor
// RICHARD NASH 13/12/12

#include <Wire.h>
int I2C_address = 0b1001000; // define I2C Address


void setup()
{
  Wire.begin();        // join i2c bus (address optional for master)
  Serial.begin(9600);  // start serial for output
}

void loop()
{
 
   Wire.beginTransmission(I2C_address); // transmit to I2C
    Wire.write(0b00000000);              // sends one byte.   
  Wire.endTransmission();    // stop transmitting
 
 
  Wire.requestFrom(I2C_address, 2);    // request 2 bytes from address. Because sensor produces 9 bit
  while(Wire.available())    // slave may send less than requested
  {
    int temp1 = Wire.read(); // receive a byte as integer
    int temp2 = Wire.read(); // receive a byte as integer Can change resolution of by chaning 2nd and 3rd bit of the register pointers.
   
    int a = temp1 <<1; // bitshift 1 to the left to allow for 9bit data from temp sensor
    int b = temp2 >>7; //bitshift 7 to the right to allow for 9 bit data from temp sensor.
   
    int tempRaw = a|b;  // define int rawtemp as the combination of integer a and b. We now have 9 bits of data.
   
    int tempC = tempRaw * 0.5 ;// 0.5 = 2^-1 since T = C x 2^n where c is the binary code value, and n is dependent on resolution used. tempC is temperature in celsius
 
   
    Serial.println(tempC);         // print the character
  }
  delay(500);
}
4  Using Arduino / Programming Questions / Re: Converting Binary from a Sensor into decimal on: December 12, 2012, 07:13:47 pm
That will multiply the binary value by 0.5 though, do I not need to convert it to decimal first?
Or does the later serial print do that for me?

Thanks
5  Using Arduino / Programming Questions / Converting Binary from a Sensor into decimal on: December 12, 2012, 06:57:23 pm
I have a temperature sensor which will be presenting data in the serial monitor.

To convert from the raw data the eqn is as follows:

T = C x 2n

C = the sensor output in decimal ( the output is a 9bit and I have it in the form of a 16 bit integer)
n is defined by what resolution you are getting from the sensor ( 9, 10, 11, 12) in my case n = -1

My question is this how do I convert my integer "temp" into a decimal to then use in this formula.

Thanks
6  Using Arduino / Programming Questions / Re: Using a temperature sensor I2C. Where do registers come into code? on: December 06, 2012, 12:22:33 pm
Thanks very much I'll post back with my finished code later on.

I appreciate your patience with a useless mechanical engineer currently getting lost in the world of programming
7  Using Arduino / Programming Questions / Re: Using a temperature sensor I2C. Where do registers come into code? on: December 06, 2012, 12:12:30 pm
Ok then two more things to clarify from your repsonses, but I think it's becoming clear.

Where you've put 0x00 I'll put 0000000 (the register pointer for ambient temp register?)

I want all of the first byte and to "add" the first bit of the second byte on the end?

What's the function called to join the two pieces of data after I've done the bit shifting?
8  Using Arduino / Programming Questions / Re: Using a temperature sensor I2C. Where do registers come into code? on: December 06, 2012, 11:57:09 am
I have the data sheet I know the register pointer I need, I'm just not clear on where this goes into the code. I want to read from the register for ambient temperature and the end result at this stage only needs to be printing the temp in degrees celsisu on the serial monitor.

My second question may not have been clear enough:
Do you have any guidance on using bit shift, is the bit in the examples in the IDE entitled strings relevant?

Should my order be something like this:
I2C transmits two bytes

then I do the bitshifting ( I want the first byte and the first bit of the second byte?)

the the maths to get it from raw data into celsius

then I print?
9  Using Arduino / Programming Questions / Using a temperature sensor I2C. Where do registers come into code? on: December 06, 2012, 11:37:43 am
Hi

I'm working programming a Leonardo board and I have a MCP9801 attached to a shield I have been given for this project.

Code:
#include <Wire.h>
int I2C_address = 0b1001000 // define address of bus

void setup()
{
  Wire.begin();        // join i2c bus (address optional for master)
  Serial.begin(9600);  // start serial for output
}

void loop()
{
 
   Wire.beginTransmission(I2C_address); // transmit to I2C
    Wire.write(0b00000000);              // sends one byte 
  Wire.endTransmission();    // stop transmitting
 
 
  Wire.requestFrom(I2C_address, 2);    // request 2 bytes from address.
  while(Wire.available())    // slave may send less than requested
  {
    int temp1 = Wire.read(); // receive a byte as character
    int temp2 = Wire.read(); // receive a byte as character
   
    Serial.println(temp);         // print the character
  }

  delay(500);
}


1) I know what register the ambient temperature is on on the sensor  but where in this code do I need to use this information
2) My lecturer talked about taking two bytes from the sensor and bit shifting to get 9 bit from the sensor, Why would I do that? and indeed what's the best way?
3) Finally there's some simple maths to perform on the output to convert it from Raw data to a temperature as defined in the data sheet for the temp sensor, What would be the best way to do this?

Thank you for your help, I hope this is clear
10  Using Arduino / Programming Questions / Re: Vague project brief from a lecturer... on: December 06, 2012, 05:31:26 am
Code:
#include <Wire.h>
int I2C_address = 0b1001000;


void setup()
{
  Wire.begin();        // join i2c bus (address optional for master)
  Serial.begin(9600);  // start serial for output
}

void loop()
{
  Wire.requestFrom(I2C_address, 1);    // request 1 byte from address.

  while(Wire.available())    // slave may send less than requested
  {
    char c = Wire.read(); // receive a byte as character
    Serial.print(c);         // print the character
  }

  delay(500);
}
I think this is getting there, I think I have to now specify which register I want to take data for, my temp sensor has 4 registers. At what point in this code do I say which Register I want to take data from?

Moderator edit: CODE TAGS
11  Using Arduino / Programming Questions / Re: Vague project brief from a lecturer... on: December 06, 2012, 05:04:49 am
Sorry but what's the IDE?
12  Using Arduino / Programming Questions / Vague project brief from a lecturer... on: December 06, 2012, 04:54:08 am
Hi

I'm not particularly good at programming and electronics and out lecturer has given us some vague brief.

We're using Leonardo boards and the temp sensor is an MCP9801

The bit I'm struggling most with is:
Interface the I2C Temperature sensor

What do I do? and what does he mean be interface?

This is a component leading up to the next few instructions if that helps. They are as follows:
Check the setup of the ADC channels, how can you make them the most sensitive for your
application
- Compare the analogue and digital temperature sensor and discuss their advantages /
disadvantages. (What temperature resolution can you archive, how do you set up for the
widest range of temperature.)
- Report temperature data back to your PC

Thanks
Pages: [1]