Arduino Code shaving

Dear LMRs,

This code below is by ICStation

http://www.instructables.com/id/APR9600-Voice-Recording-and-Playback-System-with-J/

They use a code works like this : when you press button 1 , Arduino instructs Audio record chip APR9600 to play first recorded msg whaich is (First floor) and so other buttons.

I know this is an ideal example of using matrix instead of line by line. but how ?

#define uchar unsigned char 
#define uint unsigned int 
uint Key_1=30;
uint Key_2=31;
uint Key_3=32;
uint Key_4=33;
uint Key_5=34;
uint Key_6=35;
uint Key_7=36;
uint Key_8=37;
uint Code_1=3;
uint Code_2=4;
uint Code_3=5;
uint Code_4=6;
uint Code_5=7;
uint Code_6=8;
uint Code_7=9;
uint Code_8=10;

void setup()
{
pinMode(Key_1,INPUT);
pinMode(Key_2,INPUT);
pinMode(Key_3,INPUT);
pinMode(Key_4,INPUT);
pinMode(Key_5,INPUT);
pinMode(Key_6,INPUT);
pinMode(Key_7,INPUT);
pinMode(Key_8,INPUT);
pinMode(Code_1,OUTPUT);
pinMode(Code_2,OUTPUT);
pinMode(Code_3,OUTPUT);
pinMode(Code_4,OUTPUT);
pinMode(Code_5,OUTPUT);
pinMode(Code_6,OUTPUT);
pinMode(Code_7,OUTPUT);
pinMode(Code_8,OUTPUT);
digitalWrite(Code_1,HIGH);
digitalWrite(Code_2,HIGH);
digitalWrite(Code_3,HIGH);
digitalWrite(Code_4,HIGH);
digitalWrite(Code_5,HIGH);
digitalWrite(Code_6,HIGH);
digitalWrite(Code_7,HIGH);
digitalWrite(Code_8,HIGH);
digitalWrite(Key_1,HIGH);
digitalWrite(Key_2,HIGH);
digitalWrite(Key_3,HIGH);
digitalWrite(Key_4,HIGH);
digitalWrite(Key_5,HIGH);
digitalWrite(Key_6,HIGH);
digitalWrite(Key_7,HIGH);
digitalWrite(Key_8,HIGH);
}

void loop()
{
if(digitalRead(Key_1)==0)
{
delay(10);
if(digitalRead(Key_1)==0)
{
digitalWrite(Code_1,LOW);
digitalWrite(Code_2,HIGH);
digitalWrite(Code_3,HIGH);
digitalWrite(Code_4,HIGH);
digitalWrite(Code_5,HIGH);
digitalWrite(Code_6,HIGH);
digitalWrite(Code_7,HIGH);
digitalWrite(Code_8,HIGH);
delay(10);
}
if(digitalRead(Key_1)!=0)
{
digitalWrite(Code_1,HIGH);
digitalWrite(Code_2,HIGH);
digitalWrite(Code_3,HIGH);
digitalWrite(Code_4,HIGH);
digitalWrite(Code_5,HIGH);
digitalWrite(Code_6,HIGH);
digitalWrite(Code_7,HIGH);
digitalWrite(Code_8,HIGH);
delay(10);
}
}

if(digitalRead(Key_2)==0)
{
delay(10);
if(digitalRead(Key_2)==0)
{
digitalWrite(Code_1,HIGH);
digitalWrite(Code_2,LOW);
digitalWrite(Code_3,HIGH);
digitalWrite(Code_4,HIGH);
digitalWrite(Code_5,HIGH);
digitalWrite(Code_6,HIGH);
digitalWrite(Code_7,HIGH);
digitalWrite(Code_8,HIGH);
delay(10);
}
if(digitalRead(Key_2)!=0)
{
digitalWrite(Code_1,HIGH);
digitalWrite(Code_2,HIGH);
digitalWrite(Code_3,HIGH);
digitalWrite(Code_4,HIGH);
digitalWrite(Code_5,HIGH);
digitalWrite(Code_6,HIGH);
digitalWrite(Code_7,HIGH);
digitalWrite(Code_8,HIGH);
delay(10);
}
}

if(digitalRead(Key_3)==0)
{
delay(10);
if(digitalRead(Key_3)==0)
{
digitalWrite(Code_1,HIGH);
digitalWrite(Code_2,HIGH);
digitalWrite(Code_3,LOW);
digitalWrite(Code_4,HIGH);
digitalWrite(Code_5,HIGH);
digitalWrite(Code_6,HIGH);
digitalWrite(Code_7,HIGH);
digitalWrite(Code_8,HIGH);
delay(10);
}
if(digitalRead(Key_3)!=0)
{
digitalWrite(Code_1,HIGH);
digitalWrite(Code_2,HIGH);
digitalWrite(Code_3,HIGH);
digitalWrite(Code_4,HIGH);
digitalWrite(Code_5,HIGH);
digitalWrite(Code_6,HIGH);
digitalWrite(Code_7,HIGH);
digitalWrite(Code_8,HIGH);
delay(10);
}
}

if(digitalRead(Key_4)==0)
{
delay(10);
if(digitalRead(Key_4)==0)
{
digitalWrite(Code_1,HIGH);
digitalWrite(Code_2,HIGH);
digitalWrite(Code_3,HIGH);
digitalWrite(Code_4,LOW);
digitalWrite(Code_5,HIGH);
digitalWrite(Code_6,HIGH);
digitalWrite(Code_7,HIGH);
digitalWrite(Code_8,HIGH);
delay(10);
}
if(digitalRead(Key_4)!=0)
{
digitalWrite(Code_1,HIGH);
digitalWrite(Code_2,HIGH);
digitalWrite(Code_3,HIGH);
digitalWrite(Code_4,HIGH);
digitalWrite(Code_5,HIGH);
digitalWrite(Code_6,HIGH);
digitalWrite(Code_7,HIGH);
digitalWrite(Code_8,HIGH);
delay(10);
}
}

if(digitalRead(Key_5)==0)
{
delay(10);
if(digitalRead(Key_5)==0)
{
digitalWrite(Code_1,HIGH);
digitalWrite(Code_2,HIGH);
digitalWrite(Code_3,HIGH);
digitalWrite(Code_4,HIGH);
digitalWrite(Code_5,LOW);
digitalWrite(Code_6,HIGH);
digitalWrite(Code_7,HIGH);
digitalWrite(Code_8,HIGH);
delay(10);
}
if(digitalRead(Key_5)!=0)
{
digitalWrite(Code_1,HIGH);
digitalWrite(Code_2,HIGH);
digitalWrite(Code_3,HIGH);
digitalWrite(Code_4,HIGH);
digitalWrite(Code_5,HIGH);
digitalWrite(Code_6,HIGH);
digitalWrite(Code_7,HIGH);
digitalWrite(Code_8,HIGH);
delay(10);
}
}

if(digitalRead(Key_6)==0)
{
delay(10);
if(digitalRead(Key_6)==0)
{
digitalWrite(Code_1,HIGH);
digitalWrite(Code_2,HIGH);
digitalWrite(Code_3,HIGH);
digitalWrite(Code_4,HIGH);
digitalWrite(Code_5,HIGH);
digitalWrite(Code_6,LOW);
digitalWrite(Code_7,HIGH);
digitalWrite(Code_8,HIGH);
delay(10);
}
if(digitalRead(Key_6)!=0)
{
digitalWrite(Code_1,HIGH);
digitalWrite(Code_2,HIGH);
digitalWrite(Code_3,HIGH);
digitalWrite(Code_4,HIGH);
digitalWrite(Code_5,HIGH);
digitalWrite(Code_6,HIGH);
digitalWrite(Code_7,HIGH);
digitalWrite(Code_8,HIGH);
delay(10);
}
}

if(digitalRead(Key_7)==0)
{
delay(10);
if(digitalRead(Key_7)==0)
{
digitalWrite(Code_1,HIGH);
digitalWrite(Code_2,HIGH);
digitalWrite(Code_3,HIGH);
digitalWrite(Code_4,HIGH);
digitalWrite(Code_5,HIGH);
digitalWrite(Code_6,HIGH);
digitalWrite(Code_7,LOW);
digitalWrite(Code_8,HIGH);
delay(10);
}
if(digitalRead(Key_7)!=0)
{
digitalWrite(Code_1,HIGH);
digitalWrite(Code_2,HIGH);
digitalWrite(Code_3,HIGH);
digitalWrite(Code_4,HIGH);
digitalWrite(Code_5,HIGH);
digitalWrite(Code_6,HIGH);
digitalWrite(Code_7,HIGH);
digitalWrite(Code_8,HIGH);
delay(10);
}
}

if(digitalRead(Key_8)==0)
{
delay(10);
if(digitalRead(Key_8)==0)
{
digitalWrite(Code_1,HIGH);
digitalWrite(Code_2,HIGH);
digitalWrite(Code_3,HIGH);
digitalWrite(Code_4,HIGH);
digitalWrite(Code_5,HIGH);
digitalWrite(Code_6,HIGH);
digitalWrite(Code_7,HIGH);
digitalWrite(Code_8,LOW);
delay(10);
}
if(digitalRead(Key_8)!=0)
{
digitalWrite(Code_1,HIGH);
digitalWrite(Code_2,HIGH);
digitalWrite(Code_3,HIGH);
digitalWrite(Code_4,HIGH);
digitalWrite(Code_5,HIGH);
digitalWrite(Code_6,HIGH);
digitalWrite(Code_7,HIGH);
digitalWrite(Code_8,HIGH);
delay(10);
}
}
}

I hope I am answering your question as you have posed it.

 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
// commented out by birdmun 1-12-2015
// After reading http://forum.arduino.cc/index.php?topic=43636.0
//~ const int Key_1=30;
//~ const int Code_1=3;

// This array holds the pin numbers for Keys 1-8 in order
const int KeyArray[8] = {30, 31, 32, 33, 34, 35, 36, 37};

// This array holds the pin numbers for Codes 1-8 in order
const int CodeArray[8] = {3, 4, 5, 6, 7, 8, 9, 10};

// If PinToChange is < 1 or > 8, all pins set HIGH
void SetCodeOutput(int PinToChange) {
PinToChange -= 1; // Convert number to array position
for (int i = 0; i < 8; i++) {
if (i == PinToChange) {
i++;
}
if (i < 8) {
digitalWrite(CodeArray[i], HIGH);
}
}
if ((PinToChange > -1) && (PinToChange < 8)) {
digitalWrite(CodeArray[PinToChange], LOW);
}
}

void setup() {
// commented out by birdmun on 1-12-2015
// INPUTs do not need to be specifically set
//~ pinMode(Key_1,INPUT);
for (int i = 0; i < 8; i++) {
pinMode(CodeArray[i], OUTPUT);
digitalWrite(KeyArray[i], HIGH);
}
SetCodeOutput(-1); // -1 is outside of the array range. All pins set HIGH.
// commented out by birdmun on 1-12-2015
// replaced by for loop
//~ pinMode(Code_1,OUTPUT);
//~ digitalWrite(Code_1,HIGH);
//~ digitalWrite(Key_1,HIGH);

}

void loop() {
boolean FirstCheck, SecondCheck; // for debounce code
for (int i = 0; i < 8; i++) {
FirstCheck = digitalRead(KeyArray[i]);
delay(10);
SecondCheck = digitalRead(KeyArray[i]);
if ((FirstCheck == LOW) && (SecondCheck == LOW)) {
SetCodeOutput(i + 1); // Remember to send the actual Code number not the array position.
}
if ((FirstCheck == LOW) && (SecondCheck == HIGH)) {
SetCodeOutput(-1); // Set all Code pins HIGH
}
delay(10);
}
}

Hopefully what I have done here makes sense and answers your question. I had to verify it a number of times before I got the syntax correct. :slight_smile: I have spent too much time away from coding. There are some things that could be done better. This was just a quick run through to clean up the code you posted. bdk6 and/or mogul and/or basile could probably make this look like a silk purse from my sows ear of code. :slight_smile:

Yup

Yes, that is exactly what I meant. No doubts some people are more code-skilled than others, probably as you mentioned some guys can even optimize further. But you got the question answered to the point. Many thanks indeed.

Possible code rewrite

This is a possible code rewrite. I do not have the hardward to test but it compiles ok.

/

  Notes: 1. Takes advantage of the fact that the keys and code are in sequential order
 */
typedef unsigned char uint8_t;

#define KEY_BASE 30
#define CODE_BASE 3

uint8_t curKey, prevKey;

void setup()
{
    int
i;
    
    /
      Setup the “Key” and “Code” IO pins.
     */
    for (i = 0; i < 8; i++)
    {
pinMode(KEY_BASE + i, INPUT); // set “Key” pins to inputs
pinMode(CODE_BASE + i, OUTPUT); // set “Code” pins to outputs
digitalWrite(CODE_BASE + i,HIGH); // set all outputs HIGH
digitalWrite(KEY_BASE + i,HIGH); // enable the internal pullups in inputs
    }

    /
      Initialize local variables for key debounce
     */
    curKey = 0xFF;
    prevKey = 0xFF;
}

/
  This is a slow way to read the keys. It does tak advantage that the Key
  values are sequentaial starting with 30.
 /
uint8_t readKeys(void)
{
    uint8_t
key, i;

    key = 0;
    for (i = 0; i < 0; i++)
    {
if (digitalRead(i + 30) != 0) key |= (1 << i);
    }
    return(key);
}

void loop()
{
    uint8_t
tempKey, i, mask;
    
    curKey = readKeys();
    if (curKey != prevKey)
    {
/
  Debounce the key inputs.
 /
delay(30);
tempKey = readKeys();
/
  Check to see if there has been a change. If so, set the previous active
  pins high and set the new one low.
 */
if (tempKey == curKey)
{
    mask = 0x01;
    for (i = 0; i < 8; i++)
    {
if ((prevKey & mask) == 0) digitalWrite((i + 3), HIGH);
if ((curKey & mask) == 0) digitalWrite((i + 3), LOW);
mask = mask << 1;
    }
    prevKey = curKey;
}
    }
    delay(10);
}

That’s a good optimization
That’s a good optimization for this particular case, but I prefer the arrays because it makes it easier to change one of the pins or keys later in time if he has to use a particular pin for PWM or something.

When I’m dealing with embedded stuff I try to code such that it’s relatively easy to port to a another processor if need be, or to repurpose my code to use a different character set (is EBCIDC even a thing any more?).

Code style

27 ways to skin a cat. I suspect “preferences” are based on experience, immediate work requirements, and religion. None of which are quantifable. I typically like algorithms that are processor independent and I/O that is target specific. I am relatively new to the Arduino environment, Aug 2014, and it does not fit very well with my experience or religion (no requirements)  so I tend to minimalize the the magical overhead. Just using the hidden include files and generic I/O libraries irks me.

On the plus side, I learned hoe to export code to HTML and import to a forum post. I usually for command line with emacs and make. Found a good use for the Arduino IDE. Wonder if it will pretty format a generic C program.

27 ways to skin a cat.well a

27 ways to skin a cat.well a highschool teacher made them 360…he said each one of us has his brain installed different angle from the other…thats why people who talk backwards we tell them: your brain is installed the opposite way…that never meant they re wrong, they may find a better, easier or just a different way.and we learn from everyone…thanks indeed.

Random speak not lift buttons

It occured to me, now after completeing the mechanics for this cyborg. I need (him) to speak as if he is thinking…Load a few phrases into the APR9600 and then make Arduino call any of those phrases by RandomSeed function

============================

randPin= random (9);

if (randPin<9) randPin++;//pin 7 isn’t hooked to anything here so add 1 to get 8 or 9

digitalWrite (randPin, LOW);

delay (10000);// each of the loaded phrases is less than 10 sec long

 

digitalWrite (randPin, HIGH);

delay (3000);//delay 3 sec between one phrase and another–or do we make this random also ?

======================

So arduino will trigger any msg pin on the APR 9600 (pins 1 to 8 ) 

what do you think ??