Show Posts
Pages: [1] 2 3 4
1  Using Arduino / Networking, Protocols, and Devices / Re: TWI with BH1750FVI light sensor in C on: March 15, 2014, 03:47:10 pm
Those are libraries I wrote myself or, in the case of the twi file, an approved avr library. The digital and analog libraries are unused, I just included them by habit. I'm not sure what you're talking about with init(), but I have run many programs like this one that read analog pins, read/write digital pins, etc. It's programmed just like if you were to do something on your computer in C, except I have to get it to the atmega 328p via avr.
2  Using Arduino / Networking, Protocols, and Devices / TWI with BH1750FVI light sensor in C on: March 15, 2014, 01:46:24 pm
 
So I am trying to get this light sensor to work with the Arduino Nano:
https://www.fasttech.com/products/1012006

I have to program it in pure C, no Arduino IDE or libraries, per stipulation for my EE senior design class.  I've been wading through the data sheet and examples for two days, and I still haven't gotten it to work.  My code runs all the way through, but it prints out a blank space when I try to print my result.  When I did it with the Arduino Libraries, it spat out about 240 Lux.  But somewhere, somehow, I'm screwing up the C implementation. Has anyone else had any luck with TWI in C to read data from a sensor?

Thanks for any help, here is my code. 


Code:
/*
 * main.c
 *
 *  Created on: Mar 13, 2014
 *      Author: hollis
 */
#include <inttypes.h>
#include <avr/io.h>
#include <util/delay.h>
#include <stdio.h>
#include <stdlib.h>
#include <serialLib.h>
#include <analogLib.h>
#include <digitalLib.h>
#include </usr/avr/include/util/twi.h>
#include </usr/avr/include/avr/interrupt.h>


void USART_init(void);
unsigned char USART_receive(void);
void USART_send( unsigned char data);
void USART_putstring(char* StringPtr);
void init_aio();
int read_analog();
void output(int pin, int state);

#define SCL_CLOCK 400000L

unsigned char intensity = 0;

int value = 0;
int buffer = 1000;


int main(void){
int i;
USART_init();

PORTC &= (1<<PC4);
PORTC &= (1<<PC5);

unsigned char address = 0x23;

//Initialize TWI
TWSR = 0;
TWBR = ((F_CPU/SCL_CLOCK-16)/2);

uint8_t   twst;

//Send START condition
TWCR = (1<<TWINT) | (1<<TWSTA) | (1<<TWEN);

while(!(TWCR & (1<<TWINT)));  //Wait for transmission to complete
//USART_putstring("I got here.\n\r");
twst = TW_STATUS & 0x08;  //Check status register

if ( (twst != TW_START) && (twst != TW_REP_START)) //Mask prescaler
return 1;

TWDR = address; //Send address, then interrupt flag and enable bit
TWCR = (1<<TWINT) | (1<<TWEN);

while(!(TWCR & (1<<TWINT))); //Wait for ACK (Transmission complete)
//USART_putstring("I got here too.\n\r");

if(TWINT) //Determines resolution (I think)
TWDR = 0x10;

TWCR = (1<<TWINT) | (1<<TWEN); //Clears interrupt

/*twst = TW_STATUS & (0x20 + 0x28 + 0x30); //Check TWSR
if ( (twst == TW_MT_SLA_ACK) || (twst == TW_MR_SLA_ACK) ){ //Mask prescaler
return 1;
}

USART_putstring("I got here also.\n\r");

TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWEA); //Read data, look for ACK
while(!(TWCR & (1<<TWINT)));
USART_putstring("I got here also too.\n\r");

//intensity = TWDR;*/

TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWSTO); //Send stop signal

while(TWCR & (1<<TWSTO)); //Wait for stop signal to finish


while(1){

//Send START condition
TWCR = (1<<TWINT) | (1<<TWSTA) | (1<<TWEN);

while(!(TWCR & (1<<TWINT)));  //Wait for transmission to complete
//USART_putstring("I got here.\n\r");
twst = TW_STATUS & 0x08;  //Check status register

if ( (twst != TW_START) && (twst != TW_REP_START)) //Mask prescaler
return 1;

TWDR = address + 1; //Send address, then interrupt flag and enable bit
TWCR = (1<<TWINT) | (1<<TWEN);

while(!(TWCR & (1<<TWINT))); //Wait for ACK (Transmission complete)
//USART_putstring("I got here too.\n\r");

twst = TW_STATUS & (0x38 + 0x40 + 0x48); //Check TWSR
if ( (twst == TW_MT_SLA_ACK) || (twst == TW_MR_SLA_ACK) ){ //Mask prescaler
return 1;
}

//USART_putstring("I got here also.\n\r");

TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWEA); //Read data, look for ACK
while(!(TWCR & (1<<TWINT)));
//USART_putstring("I got here also too.\n\r");

//i = 0;
//i <<= 8;
i = TWDR;



TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWSTO); //Send stop signal

while(TWCR & (1<<TWSTO)); //Wait for stop signal to finish


intensity = i/1.2;
//intensity = itoa(intensity, value, buffer);
USART_send(intensity);
USART_putstring("\n\r");
//USART_putstring("I got here\n\r");


}


}
3  Using Arduino / Sensors / Re: Weight sensor sensitive to grains? (15.42 grain = 1 gram) on: June 16, 2012, 04:07:16 pm
Thanks, that looks promising.  Any other ideas?
4  Using Arduino / Sensors / Re: Weight sensor sensitive to grains? (1 grain = 15.42 grams) on: June 02, 2012, 10:28:33 pm
Grains.  1 gram = 15.42 grains.  subject is backwards, sorry.
5  Using Arduino / Sensors / Weight sensor sensitive to grains? (15.42 grain = 1 gram) on: June 02, 2012, 07:31:34 pm
I am looking for a weight sensor that can sense grains within 1/100 of a grain.  I'm measuring gun powder, and it needs to be accurate.  All the ones I'm finding are sensitive within a gram or two, but that's 15 to 30 times on grain, which is 100 times my tolerance.  So 1500 to 3000 times my tolerance is not acceptable. I know they make them, because they make digital scales to weight the powder, but I can't find a data sheet on any of them to find the type of load cell/sensor they used.  Any ideas?
6  Using Arduino / Programming Questions / Re: 4X4 Matrix keypad help on: May 07, 2012, 04:29:52 pm
Awesome!  That was it!  I'm going to have to go find my professor from last year that told me that pins are defaultly set to outputs!  Thanks a lot!
7  Using Arduino / Programming Questions / Re: 4X4 Matrix keypad help on: May 07, 2012, 04:12:26 pm
Ahhhh.  Thanks.  I put those in.  The only thing is, in the picture in that link, they show 3k resistors.  All I have are 220, 470, 1k, and 10k.  I just used 1k resistors, will that do?  Because now, none of my buttons are registering.  They all show 0 and don't oscillate, but they won't change when pressed either.  Could that be because of too low of pull down resistors?  Thanks for the help.
8  Using Arduino / Programming Questions / Re: 4X4 Matrix keypad help on: May 07, 2012, 03:52:26 pm
No resistors on the outputs.  I've tried 1k resistors on the inputs, but that didn't change anything, so right now I have them off.  The data sheet showed wiring for the BASIC Stamp and the Propeller, but I didn't know which would pertain to the Arduino. I'm guessing they need to be in there, so I'll put them back, but I still have the same problems.
9  Using Arduino / Programming Questions / Re: Morse Code programming on: May 07, 2012, 03:44:21 pm
If I'm not mistaken, you need "()" when you call your subroutines.  For instance, when you wish to run void dot(), your code should read: dot();
10  Using Arduino / Programming Questions / 4X4 Matrix keypad help on: May 07, 2012, 03:40:39 pm
Right now, I'm just trying to get my key pad to work. 
http://www.parallax.com/Portals/0/Downloads/docs/prod/hardware/27899-4x4matrixmembranekeypad-v1.2.pdf
That is the data sheet to it.  I'm trying to do like the data sheet says, pulling columns high one at a time and reading the rows, but it doesn't seem to be working.  I have some buttons reading high all the time (1, *, D, 5, and smiley-cool, and A won't register at all.  Others, when pressed, send all of the readings into spazzes, oscillating between 0 and 1.  Please take a look and see if you can see any obvious errors please.

Code:
int orange = 13; // colors correspond to the colors on the data sheet
int green = 12;
int purple = 11;
int blue = 10;
int yellow = 9;
int red = 8;
int lightblue = 7;
int pink = 6;
int key1 = 0;    // the keys
int key2 = 0;
int key3 = 0;
int key4 = 0;
int key5 = 0;
int key6 = 0;
int key7 = 0;
int key8 = 0;
int key9 = 0;
int key0 = 0;
int keyA = 0;
int keyB = 0;
int keyC = 0;
int keyD = 0;
int keystr = 0;  // this is *
int keylb = 0;   // this is #

void setup(){
  Serial.begin(9600);       // printing to LCD screen to see status's
  Serial.print(12, BYTE);
  Serial.print(17, BYTE);
  Serial.print(24, BYTE);
  Serial.print(128, BYTE);
  Serial.print("A= ");
  Serial.print(132, BYTE);
  Serial.print("B= ");
  Serial.print(136, BYTE);
  Serial.print("C= ");
  Serial.print(140, BYTE);
  Serial.print("D= ");
  Serial.print(144, BYTE);
  Serial.print("*= ");
  Serial.print(148, BYTE);
  Serial.print("#= ");
  Serial.print(152, BYTE);
  Serial.print("1= ");
  Serial.print(156, BYTE);
  Serial.print("2= ");
  Serial.print(160, BYTE);
  Serial.print("3= ");
  Serial.print(164, BYTE);
  Serial.print("4= ");
  Serial.print(168, BYTE);
  Serial.print("5= ");
  Serial.print(172, BYTE);
  Serial.print("6= ");
  Serial.print(176, BYTE);
  Serial.print("7= ");
  Serial.print(180, BYTE);
  Serial.print("8= ");
  Serial.print(184, BYTE);
  Serial.print("9= ");
  Serial.print(188, BYTE);
  Serial.print("0= ");
  digitalWrite(yellow, LOW);    // I did this to see if it'd keep pins from starting HIGH
  digitalWrite(red, LOW);
  digitalWrite(lightblue, LOW);
  digitalWrite(pink, LOW);
  pinMode(orange,INPUT);        // Setting rows to inputs
  pinMode(green,INPUT);
  pinMode(purple,INPUT);
  pinMode(blue,INPUT);
}

void loop(){
  digitalWrite(pink, HIGH);     //Sending 1st column high
  keyA = digitalRead(orange);   //Reading rows
  keyB = digitalRead(green);
  keyC = digitalRead(purple);
  keyD = digitalRead(blue);
  if(digitalRead(orange)==HIGH){    //changing key status's based off readings
    keyA = 1;
  }
  if(digitalRead(green)==HIGH){
    keyB = 1;
  }
  if(digitalRead(purple)==HIGH){
    keyC = 1;
  }
  if(digitalRead(blue)==HIGH){
    keyD = 1;
  }
  digitalWrite(pink, LOW);        //Sending 1st column back LOW
  digitalWrite(lightblue, HIGH);  //Sending 2nd column HIGH
  keyB = digitalRead(orange);     //Reading rows
  key6 = digitalRead(green);
  key5 = digitalRead(purple);
  key4 = digitalRead(blue);
  if(digitalRead(orange)==HIGH){  //Changing key status's based off row readings
    keyB = 1;
  }
  if(digitalRead(green)==HIGH){
    key6 = 1;
  }
  if(digitalRead(purple)==HIGH){
    key5 = 1;
  }
  if(digitalRead(blue)==HIGH){
    key4 = 1;
  }
  digitalWrite(lightblue, LOW);   //Sending 2nd column low
  digitalWrite(red, HIGH);        //Sending 3rd column HIGH
  keyC = digitalRead(orange);     //Reading rows
  key9 = digitalRead(green);
  key8 = digitalRead(purple);
  key7 = digitalRead(blue);
  if(digitalRead(orange)==HIGH){  //Changing key status's based off readings of rows
    keyC = 1;
  }
  if(digitalRead(green)==HIGH){
    key9 = 1;
  }
  if(digitalRead(purple)==HIGH){
    key8 = 1;
  }
  if(digitalRead(blue)==HIGH){
    key7 = 1;
  }
  digitalWrite(red, LOW);         //Sending 3rd column low
  digitalWrite(yellow, HIGH);     //Sending 4th column HIGH
  keyD = digitalRead(orange);     //Reading rows
  keylb = digitalRead(green);
  key0 = digitalRead(purple);
  keystr = digitalRead(blue);
  if(digitalRead(orange)==HIGH){  //Changing status's based off row readigns
    keyD = 1;
  }
  if(digitalRead(green)==HIGH){
    keylb = 1;
  }
  if(digitalRead(purple)==HIGH){
    key0 = 1;
  }
  if(digitalRead(blue)==HIGH){
    keystr = 1;
  }
  digitalWrite(yellow, LOW);      //Sending 4th column low
 
  Serial.print(130, BYTE);     //Printing results
  Serial.print(keyA);
  Serial.print(134, BYTE);
  Serial.print(keyB);
  Serial.print(138, BYTE);
  Serial.print(keyC);
  Serial.print(142, BYTE);
  Serial.print(keyD);
  Serial.print(146, BYTE);
  Serial.print(keystr);
  Serial.print(150, BYTE);
  Serial.print(keylb);
  Serial.print(154, BYTE);
  Serial.print(key1);
  Serial.print(158, BYTE);
  Serial.print(key2);
  Serial.print(162, BYTE);
  Serial.print(key3);
  Serial.print(166, BYTE);
  Serial.print(key4);
  Serial.print(170, BYTE);
  Serial.print(key5);
  Serial.print(174, BYTE);
  Serial.print(key6);
  Serial.print(178, BYTE);
  Serial.print(key7);
  Serial.print(182, BYTE);
  Serial.print(key8);
  Serial.print(186, BYTE);
  Serial.print(key9);
  Serial.print(190, BYTE);
  Serial.print(key0);   
}
11  Using Arduino / Programming Questions / Re: How to add Ping))) to this code on: May 03, 2012, 01:48:50 pm
Here's the basic code to use the PING)))  The tone is to hook up to a piezo speaker, and it changes pitch based off the distance.  I'm not entirely sure what you want to do with it, but if you needed the code, here you go.

Code:
int tone_freq=0;
  long duration, inches;
  pinMode(3, OUTPUT);
  digitalWrite(3, LOW);
  delayMicroseconds(2);
  digitalWrite(3, HIGH);
  delayMicroseconds(5);
  digitalWrite(3, LOW);
  pinMode(3, INPUT);
  duration = pulseIn(3, HIGH);
  Serial.print(148, BYTE);
  Serial.print(duration);
  inches = (duration/2)/74;
  Serial.print(137, BYTE);
  Serial.print(inches);
  delay(100);
  tone_freq= inches*100;
  tone(2,tone_freq);
12  Using Arduino / Programming Questions / Re: Interrupt based off internal timer? on: March 20, 2012, 05:40:17 pm
Yeah, I figured that one out too.  Thanks for your help though.

Code:
int half_period=200;
int hours = 0;
int hours_up = 7;
int hours_down = 6;
int mins_up = 13;
int mins_down = 12;
int lcd = 1;
int speaker = 10;
int mins = 0;
int reset = 4;
int start = 3;
int x = 1;
int alarm_status = 0;
int i =0;
unsigned long int time = 0;
unsigned long int etime = 0;
void setup(){
  Serial.begin(9600);
  pinMode(speaker,OUTPUT);
  pinMode(1,OUTPUT);
  pinMode(hours_up,INPUT);
  pinMode(hours_down,INPUT);
  digitalWrite(mins_up,LOW);
  pinMode(mins_up,INPUT);
  digitalWrite(mins_down,LOW);
  pinMode(mins_down,INPUT);
  pinMode(reset,INPUT);
  pinMode(start,INPUT);
  Serial.print(12,BYTE);
  Serial.print(17,BYTE);
  Serial.print(24,BYTE);
  Serial.print(128,BYTE);
  Serial.print("Hours:");
  Serial.print(148,BYTE);
  Serial.print("Minutes:");
  time = millis();
  Serial.print(195,BYTE);
  Serial.print(time);
}
void loop(){
  Serial.print(134,BYTE);
  Serial.print(hours   );
  Serial.print(156,BYTE);
  Serial.print(mins  );
  Serial.print(188,BYTE);
  Serial.print(x);
  Serial.print(205,BYTE);
  Serial.print(analogRead(start));

  if(mins<0){  //reset to 0 if goes negative
    mins=0;
  }
  if(mins>60){
    hours = hours+1;
    mins = mins - 60;  //reset to 0 if goes over 60
  }
  if(hours<0){
    hours = 0;        //reset to 0 if goes negative
  }
  if(digitalRead(mins_up)==HIGH){
    mins_add();
    delay(500);  //add to mins if high
  }
  else{
  }

  if(digitalRead(mins_down) == HIGH){
    mins_sub();
    delay(500);  //sub from mins if high
  }
  else{
  }

  if(digitalRead(hours_up)==HIGH){
    hours_add();
    delay(500);  //add to hours if high
  }
  else{
  }

  if(digitalRead(hours_down)==HIGH){
    hours_sub();
    delay(500);   //sub from hours if down
  }
  else{
  }
  if(analogRead(start) <=100 && (hours >= 0 | mins >= 0)){
    if(i==0){
      time = millis();
      i++;
      Serial.print(195,BYTE);
      Serial.print(time);
      Serial.print(175,BYTE);
      Serial.print(i);
    }
    else{}
    etime = millis()-time;
    Serial.print(168,BYTE);
    Serial.print(etime);
    if(etime >= 60000*x){
      mins = mins - 1;
      x= x +1;           //check if told to start and non 0 values for mins and hours
      //keep track of elapsed time, sub from mins after 60 secs
      Serial.print(188,BYTE);
      Serial.print(x);
    }     
      if(mins == 0 && hours != 0){
        hours = hours - 1;
        mins = 59;               //when mins = 0, sub from hours, take mins to 59
      }
      else{
      }

    if(mins <= 0 && hours <= 0){
      alarm();
    }
    else{
    }
  }       

  else {
  }
   //if(digitalRead(reset)==HIGH){
       // digitalWrite(speaker,LOW);
       //
     // }
}
void alarm(){
  digitalWrite(speaker,HIGH);
  delayMicroseconds(half_period);
  digitalWrite(speaker,LOW);
  delayMicroseconds(half_period);
  if(digitalRead(reset) == HIGH){
    digitalWrite(speaker,LOW); 
 etime = 0;
        mins = 0;
        hours = 0;
       i = 0;
  }
}
void mins_add(){
  if(digitalRead(mins_up) == HIGH){
    mins = mins + 1;
    delay(100);
    pinMode(mins_up,OUTPUT);
    digitalWrite(mins_up,LOW);
    pinMode(mins_up,INPUT);
    delay(1000);
  }
  else{
  }
}
void mins_sub(){
  if(digitalRead(mins_down)==HIGH){
    mins = mins - 1;
    delay(100);
    pinMode(mins_down,OUTPUT);
    digitalWrite(mins_down,LOW);
    pinMode(mins_down,INPUT);
    delay(1000);
  }
  else {
  }

}
void hours_add(){
  if(digitalRead(hours_up) ==HIGH){
    hours = hours + 1;
    delay(100);
    pinMode(hours_up,OUTPUT);
    digitalWrite(hours_up,LOW);
    pinMode(hours_up,INPUT);
    delay(1000);
  }
  else {
  }
}
void hours_sub(){
  if(digitalRead(hours_down) == HIGH){
    hours= hours - 1;
    delay(100);
    pinMode(hours_down,OUTPUT);
    digitalWrite(hours_down,LOW);
    pinMode(hours_down,INPUT);
    delay(1000);
  }
  else {
  }
}



13  Using Arduino / Programming Questions / Re: Interrupt based off internal timer? on: March 20, 2012, 03:08:12 pm
Why only post those two lines?  You left out the if statement again.  If it's currently high and I press my reset button it goes from high to low, not low to low.  The second low command is only sent if(digitalRead(reset) == HIGH).  I don't understand why y'all keep over looking that.  The first low is part of the alternation between high to low every 200 microseconds.  Without it, my speaker wouldn't make a noise.  Without the one in the if statement, when I pressed reset it would still make a noise.  That function is right, it's how I want it, it works.  I've actually gotten the entire thing to work now.  My timing problem wasn't in my code, I apparently messed up the pin I was using as an input, because it would just randomly go HIGH for no reason.  I swapped pins, and now everything works fine.  All I accomplished on here was people overlooking my questioning and being formatting nazis.  I admitted I'm not the greatest formatting person, but that didn't seem to stop people from saying "Hey! That won't work!" when it actually does.  I know my way isn't the only way to do things, and those of you who pitched a hissy fit because of "{" on the same line as my loop statement should realize the same.
14  Using Arduino / Programming Questions / Re: Interrupt based off internal timer? on: March 20, 2012, 07:14:44 am
The second digitalWrite(speaker,LOW) is inside an if statement checking to see if reset was pushed.  So if it isn't pushed, I have to alternate the signal to the piezo speaker from high to low every half period.  So that has to be there.  So you're right, that part does matter, just not how you thought it did.
15  Using Arduino / Programming Questions / Re: Interrupt based off internal timer? on: March 19, 2012, 04:28:46 pm
It works with them, not without them.  Just calling them as I see them.  I used to have mins floated, shoulda changed that, thanks.   And I want to break out of that for loop.  I don't need time to constantly increase, I need something to compare etime to.  I know I don't format the best, but how about instead of picking apart stuff that doesn't matter, help me find my time issue.  As far as each new "{" on a new line, I don't particularly like that.  I was taught to do it like I am, so I'm sticking with it.
Pages: [1] 2 3 4