Show Posts
Pages: [1] 2
1  Using Arduino / Programming Questions / Re: Problems with bitWrite on: May 08, 2012, 01:53:12 pm
Thanks a bunch, I didn't realize that.  Now that I've removed the byte in front of KMAP in READ() it starts working correctly, thank you!
2  Using Arduino / Programming Questions / Re: Problems with bitWrite on: May 08, 2012, 11:50:37 am
I just did so, uploaded to the board, and it still gives me 0 in KMAP and "All conditions are false." in BOOL.  I've also tried moving the digitalRead outside of the function like so:
Code:
byte n = digitalRead(in);
bitWrite(KMAP, x, n);

Which did not work either.
3  Using Arduino / Programming Questions / Re: Problems with bitWrite on: May 08, 2012, 11:13:42 am
So putting the binary value of x onto PORTC doesn't turn the pins on and off? I'm not sure how this works then, I thought that it would turn be like if x is 101 in binary, then A0 would be high, A1 would be low, and A2 would be high.  Anyway, this is my entire code.
Code:
#include <LiquidCrystal.h>
#include <ST7565.h>
#include <SPI.h>
#define in 10
#define button 9
#define SW1 8
byte KMAP/*2][4]*/;
String BOOL = "";
String Old = "";
byte t;
ST7565 glcd(9, 8, 7, 6, 5);
String BL = ("A'B'C'","A'B'C","A'BC'","A'BC","AB'C'","AB'C","ABC'","ABC");
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

void setup() 
/* This section sets up the intial conditions required to execute
 /  all subsequent functions, mostly setting the pins as input
 /  and output and intializing the LCD libraries.
 */
{

  //pinMode(outC, OUTPUT);              //Set these pins to output
  //pinMode(outB, OUTPUT);
  //pinMode(outA, OUTPUT);
  pinMode(in, INPUT);                 //Set these pins to input
  digitalWrite(in, HIGH);
  pinMode(button, INPUT);
  digitalWrite(button, HIGH);         //Turn on internal pullup resistor
  Serial.begin(9600);                 //Start serial communication
  Serial.println("Serial begin.");    //Print the test line
  Serial.println(freeRam());
  //glcd.begin(0x18);
  lcd.begin(16, 2);
  //glcd.st7565_init();
  //glcd.st7565_command(CMD_DISPLAY_ON);
  //glcd.st7565_command(CMD_SET_ALLPTS_NORMAL);
  pinMode(A0, OUTPUT);
  pinMode(A1, OUTPUT);
  pinMode(A2, OUTPUT);
  //lcd.print("Hello World");

}
void loop() 
{
  if (digitalRead(button) == LOW) {
    digitalWrite(13, HIGH);
    READ();
    CONV();
  }
  else {
  digitalWrite(13, LOW);
  }
  DISP();
  //glcd.display();
}
   
  /*digitalWrite(13, LOW);
  if (digitalRead(SW1) == LOW)  {
    DISPT();
  }
  else if (digitalRead(SW1) == HIGH)  {
    DISPB();
  }*/



void READ() 
/* This section is the read and store section; it interacts with
 /  the outside world via the designated output and input pins.
 /  This section will write to each output, counting up in binary
 /  with the MSB called A, and the LSB called C.
 */
{
 
  byte KMAP = B00000000;
  byte x;
  for (x = 0; x < 8; x++)  {
    PORTC = x;
    delay(2);
    bitWrite(KMAP, x, digitalRead(in));
  }
 
}

void CONV() 
/* This is the Karnaugh mapping section, which checks for contiguous
 /  sections of true outputs on a grid created in the array KMAP.
 /  This simplifies the expression by eliminating variables whose level
 /  is irrelevant because the output is true when it is true or false.
 /  Simplified expressions are added to string BOOL.
 */
{
  BOOL = "";
  byte TEMP = KMAP;
  if (KMAP == 255)  {
    BOOL = "All conditions are true.";
    t = 1;
    return;
  }
  if (KMAP == 0)  {
    BOOL = "All conditions are false.";
    t = 1;
    return;
  }
  if (TEMP & 90)  {
    BOOL += "C' + ";
    TEMP &= 165;
    if (TEMP == 0)  {
      return;
    }
  }
  if (TEMP & 165)  {
    BOOL += "C + ";
    TEMP &= 90;
    if (TEMP == 0)  {
      return;
    }
  }
  if (TEMP & 15)  {
    BOOL += "A' + ";
    TEMP &= 240;
    if (TEMP == 0)  {
      return;
    }
  }
  if (TEMP & 240)  {
    BOOL += "A + ";
    TEMP &= 15;
    if (TEMP == 0)  {
      return;
    }
  }
  if (TEMP & 51)  {
    BOOL += "B' + ";
    TEMP &= 204;
    if (TEMP == 0)  {
      return;
    }
  }
  if (TEMP & 204)  {
    BOOL += "B + ";
    TEMP &= 51;
    if (TEMP == 0)  {
      return;
    }
  }
  if (TEMP & 3)  {
    BOOL += "A'B' + ";
    TEMP &= 252;
    if (TEMP == 0)  {
      return;
    }
  }
  if (TEMP & 12)  {
    BOOL += "A'B + ";
    TEMP &= 243;
    if (TEMP == 0)  {
      return;
    }
  }
  if (TEMP & 48)  {
    BOOL += "AB' + ";
    TEMP &= 207;
    if (TEMP == 0)  {
      return;
    }
  }
  if (TEMP & 192)  {
    BOOL += "AB + ";
    TEMP &= 63;
    if (TEMP == 0)  {
      return;
    }
  }
  if (TEMP & 5)  {
    BOOL += "A'C' + ";
    TEMP &= 250;
    if (TEMP == 0)  {
      return;
    }
  }
  if (TEMP & 10)  {
    BOOL += "A'C + ";
    TEMP &= 245;
    if (TEMP == 0)  {
      return;
    }
  }
  if (TEMP & 80)  {
    BOOL += "AC' + ";
    TEMP &= 175;
    if (TEMP == 0)  {
      return;
    }
  }
  if (TEMP & 160)  {
    BOOL += "AC + ";
    TEMP &= 95;
    if (TEMP == 0)  {
      return;
    }
  }
  if (TEMP & 17)  {
    BOOL += "B'C' + ";
    TEMP &= 238;
    if (TEMP == 0)  {
      return;
    }
  }
  if (TEMP & 34)  {
    BOOL += "B'C + ";
    TEMP &= 221;
    if (TEMP == 0)  {
      return;
    }
  }
  if (TEMP & 68)  {
    BOOL += "BC' + ";
    TEMP &= 187;
    if (TEMP == 0)  {
      return;
    }
  }
  if (TEMP & 136)  {
    BOOL += "BC + ";
    TEMP &= 119;
    if (TEMP == 0)  {
      return;
    }
  }
  if (TEMP & 1)  {
    BOOL += "A'B'C' + ";
    TEMP &= 254;
    if (TEMP == 0)  {
      return;
    }
  }
  if (TEMP & 2)  {
    BOOL += "A'B'C + ";
    TEMP &= 253;
    if (TEMP == 0)  {
      return;
    }
  }
  if (TEMP & 4)  {
    BOOL += "A'BC' + ";
    TEMP &= 251;
    if (TEMP == 0)  {
      return;
    }
  }
  if (TEMP & 8)  {
    BOOL += "A'BC + ";
    TEMP &= 247;
    if (TEMP == 0)  {
      return;
    }
  }
  if (TEMP & 16)  {
    BOOL += "AB'C' + ";
    TEMP &= 239;
    if (TEMP == 0)  {
      return;
    }
  }
  if (TEMP & 32)  {
    BOOL += "AB'C + ";
    TEMP &= 223;
    if (TEMP == 0)  {
      return;
    }
  }
  if (TEMP & 64)  {
    BOOL += "ABC' + ";
    TEMP &= 191;
    if (TEMP == 0)  {
      return;
    }
  }
  if (TEMP & 128)  {
    BOOL += "ABC + ";
    TEMP &= 127;
  }
}

void DISP()
{
  if (!t)  {
    BOOL[BOOL.length() - 2] = NULL;
  }
 
  if (BOOL != Old)  {
    Serial.println(BOOL);
    Old = BOOL;
    lcd.println(BOOL);
    Serial.println(KMAP);
  }

}

//void DISPT()  {
// 
//  // Displays the truth table for the logic circuit.
//  //glcd.drawchar(
//  for (byte x = 0; x < 8; x++)  {
//    glcd.drawstring(x, 0, (char*)BL[x]);
//  }
//  /*glcd.drawstring(1, 0, "A'B'C");
//  glcd.drawstring(2, 0, "A'BC'");
//  glcd.drawstring(3, 0, "A'BC");
//  glcd.drawstring(4, 0, "AB'C'");
//  glcd.drawstring(5, 0, "AB'C");
//  glcd.drawstring(6, 0, "ABC'");
//  glcd.drawstring(7, 0, "ABC");*/
// 
//  byte y;
//  for (y = 0; y < 8; y++)  {
//    glcd.drawchar(y, 8, bitRead(KMAP, y));
//  }
//  /*
// 
//  byte x;
//  byte y;
//  for (x = 0; x < 2; x++)  {
//    for (y = 0; y < 3; y++)  {
//      glcd.drawchar(y, x, KMAP[x][y]);
//    }
//    glcd.drawchar(y, x, KMAP[x][y]);
//  }*/
//}
//
//void DISPB()  {
//  // Displays the boolean expression for the logic cicuit.
//  int L = BOOL.length() - 2;
//  int l;
//  byte m;
//  for (l = 0; l < L; l++)  {   
//    glcd.drawchar(m, 0, BOOL[l]);
//    m++;
//    Serial.print(BOOL[l]);
//    Serial.print(" ");
//  }
//}

int freeRam()  {
 
  extern int  __bss_end;
  extern int  *__brkval;
  int free_memory;
  if((int)__brkval == 0) {
    free_memory = ((int)&free_memory) - ((int)&__bss_end);
  }
  else {
    free_memory = ((int)&free_memory) - ((int)__brkval);
  }
  return free_memory;
}
4  Using Arduino / Programming Questions / Re: Problems with bitWrite on: May 08, 2012, 11:01:01 am
Sorry, yes, so it toggles outputs based on x while counting up, then reads an input pin and attempts to store the output into bit x of byte KMAP, but even when I tell it to print the value that it is reading into bit x, it always comes up as 1, and even though therefore KMAP should contain 255, it contains 0.  I am confused as to why KMAP contains 0 even when I'm telling it to write 1's or anything else to the bits.
5  Using Arduino / Programming Questions / Problems with bitWrite on: May 08, 2012, 10:48:56 am
Hello, I've been trying to store some values in a byte using bitWrite, but it doesn't seem to be working.
Code:
{
  byte KMAP = B00000000;
  byte x;
  for (x = 0; x < 8; x++)  {
    PORTC = x;
    delay(2);
    bitWrite(KMAP, x, digitalRead(in));
  }
}
This is my code, and on the surface, I can't see anything wrong with it?  Help would be appreciated immensely.
6  Using Arduino / Programming Questions / Re: function, Does void x() need on: April 11, 2012, 01:53:30 pm
It sounds like the problem might be in his use of the Elapsed library, as he's using that for his timer?  I'm not sure where to find the library to download and test, unfortunately, and the program will not compile without it.  However, I still believe that the function called needs to be of type int, to get a value returned to the main function, and so far the comments have not led me to believe otherwise, however, how would a return work inside a void, if the function is expected to have no return?
7  Using Arduino / Programming Questions / Re: Truncating a string on: April 11, 2012, 01:49:40 pm
Thank you, that did the trick.  I did modify the code, because I forgot that there was still another " " at the end after "+" so I subtracted 3 from the length and set the character there to NULL, ending the string.  Thank you much, I kept trying to set it to " " and then trimming it, with no success.  The full code will be posted when it's finished, and will be made available to everyone as a tool, I'm actually very excited to release it as it's my first contribution to the open source and electronic hobbyist community.  Again, thank you for pointing that out to me.
8  General Category / General Discussion / Re: If a program could program itself?.... (AI) on: April 11, 2012, 01:35:37 pm
Here's the real kicker of a question:  If the program could write itself, what would it write itself to do?  What function would it accomplish?
9  Using Arduino / Programming Questions / Re: function, Does void x() need on: April 11, 2012, 01:30:57 pm
Here's a question, where does the Elapsed.h come from, as in, what library are you using?
10  Using Arduino / Programming Questions / Re: voltage controlled blinking leds? on: April 11, 2012, 01:26:59 pm
Just throwing in some insight to the comment posted, I believe what he means is that when the Arduino is calling a delay(x), the code is just waiting for that time to elapse.  It can't execute any other code while it waits, so even if you push the button, if it's currently waiting that x time, it won't get that button press.  A solution is provided somewhere that explains how to replace delay(x) with millis(x) timers, allowing the rest of the code to run while the timer is also running.  By removing the delays from your code, you make sure that when the button is pressed, the Arduino is not in a "standstill" condition, waiting for the delay(x) to finish.
11  Using Arduino / Programming Questions / Re: function, Does void x() need on: April 11, 2012, 01:17:26 pm
Correct me if I'm wrong, as I'm new to programming, but if you want a return from a function, shouldn't you change void to the type of output you expect?  The way I understand it, the "void" at the beginning of a function says that the function is expected to return nothing.  Maybe if you change the void RelayDelayOn() into int RelayDelayOn()?

EDIT:

As an addendum, the more I think about it, the more this makes sense.  If the original function isn't expecting a return from the function it's calling, it will always return nothing, most likely even if you specify inside the called function to return a value.  Try changing the type of function from void to int, and see if that doesn't fix the problem.
12  Using Arduino / Programming Questions / Truncating a string on: April 11, 2012, 01:05:35 pm
Hello, I'm working on a project that reads conditions from an array and adds output to a string based on combinations of true conditions.  In order to add together many different pieces of information, I added in a short " + " to the end of each of the pieces of information that are added to the string.  This program runs through one hundred some odd lines of code, piecing together the string as it goes.  Now I come to the final output, but I can't figure out how to truncate the end of the string, such that the combination text (" + ") is removed, seeing as there is no more possible data to add.  I have tried to measure the length of the string via string.length(); and then shift the address over to the + to write over it by doing thus:
Code:
int L = string.length();
L--;
string[L] = " ";
string = string.trim();
However, the compiler tells me that I have an invalid conversion from 'const char*' to 'char' and will not compile.  Is there a simple way that I can remove the last n characters from a string?
13  Using Arduino / Programming Questions / Re: Newbie Code for Proofreading on: February 19, 2011, 03:22:13 pm
Ah, I see what you mean.  Well, I'm unsure of what time lags would be perceptible to a user.  I understand that there will be some amount of time for the encoder to be read, the serial data to be calculated and transmitted, and the DAC to update, however I don't know how much time this will take or how long it has to take before the user feels like it's updating overly slowly.  Like playing a video game and having to wait for loading screens.  I want it to be as smooth and seamless as possible, while being slow enough that there is little chance for mistakes.  What kind of time difference from user input to measurable output would seem sluggish?
14  Using Arduino / Programming Questions / Re: Newbie Code for Proofreading on: February 19, 2011, 03:14:07 pm
I'm not sure what you mean by that.  I just don't want to screw up my $12 DAC or $15 opamp.  I want my interface to be responsive and to make sure that the DAC doesn't get loaded with erroneous data and try to output something I didn't tell it to.  I'm not sure what you want for performance criteria.
15  Using Arduino / Programming Questions / Re: Newbie Code for Proofreading on: February 19, 2011, 03:02:18 pm
There's a timing diagram in the datasheet, but I have no idea how to read this information.  I'm using an AD5504 from Analog Devices, datasheet here. http://www.analog.com/static/imported-files/data_sheets/AD5504.pdf
Pages: [1] 2