Sketch layout help

Hi all,

I am pretty new to the programming stuff. So far I have managed to display the resuts of an analog input on a 16 x 2 LCD after a lot of pain trying to work out what one I had. All good now, works a charm.

What I want to do now is determine at start-up what the analog value is and if it is in a certain value range set some digital outputs. That I am sure I can do without help but I only want it to perform this at start up and then proceed to the main program. I am not sure where to put it or call it. Does it go in void setup or do I create its own void name and call it from setup or something else?

Its how ever it fits you your brain better. Same as "whom do I marry?"

-jim lee

You can do it in the setup() function or you can do it in any other function.

It is not called a void, it is called a function.

Thanks Larryd.

Does that mean setup is only run at startup?

PS. This is my first real attempt at writing in C++. Qbasic was my last foray into programming. Yes i am an old fart.

Something like this? Runs once at startup:

void setup()
{
  delay(500); //wait to settle
  int analogVal = 0; 
  for(byte i = 0;i < 8;i++)  // get 8 samples from AO
    analogVal += analogRead(A0); // sum of 8 samples
  analogVal /= 8; // average of 8 samples
  if(analogVal >= myVal)
    do_myCommand; // and so on
}

void loop()
{
}

Thank you.

I will build on that.
And yell if I have more problems.

“ Yes i am an old fart.”

Well then, you’ll fit in here quite nicely.

Yeeeehaaaaa.

Thanks guys. Working like a dream. I might have to get a new job as guru programmer. Well may be not.

Next step.

Now that I have it detecting a particular state of the analog input I need to save this so when it restarts it keeps this value. I am assuming I will need to write this to eeprom????

This is what I have at the moment. What I need is eg. if pin 22 is set high then next time it switches on AND sensorval is not within any of those ranges I want pin 22 to be set high. Same if it was pin 23 and so on.

void setup()
{

delay(500); //wait to settle
pinMode(22, OUTPUT);
pinMode(23, OUTPUT);
pinMode(24, OUTPUT);

// Read sensor value
int sensorVal = analogRead (A0);

if (sensorVal > 666 and sensorVal <= 800)
digitalWrite(22, HIGH);
else if (sensorVal > 801 and sensorVal <= 905)
digitalWrite(23, HIGH);
else if (sensorVal > 906 and sensorVal <= 980)
digitalWrite(24, HIGH);
}

Hey, you know something about EEPROM, we thinks you are an expert ;).


There are examples that come with the EEPROM library.

Just remember you can do about 100,000 write cycles to an EEPROM location (then anything within two meters catches fire, just kidding) so only do so when absolutely necessary.

That 666 number is frightening!

You can write the status of the three pins to three EEPROM locations, read them at restart time and return the pins to those conditions.

Tell us a bit more what this is all about.

:sleeping: time.


Some examples for storing data to EEPROM.

//                        E E P R O M W r i t e L o n g ( )
//********************************************************************************
//This function will write a 4 byte (32bit) 'long' to the eeprom at
//the specified address to address + 3.
void EEPROMWriteLong(int address, long value)
{
  //Decomposition from a 'long' to 4 bytes by using bitshift.
  //One = Most Significant Byte, Four = Least Significant Byte
  byte four  = (value & 0xFF);
  byte three = ((value >> 8) & 0xFF);
  byte two   = ((value >> 16) & 0xFF);
  byte one   = ((value >> 24) & 0xFF);

  //Write the 4 bytes into the eeprom memory.
  EEPROM.write(address, four);
  EEPROM.write(address + 1, three);
  EEPROM.write(address + 2, two);
  EEPROM.write(address + 3, one);

} //END of EEPROMWriteLong()

//                        E E P R O M W r i t e I n t ( )
//********************************************************************************
//This function will write a 2 byte (16bit) int to the eeprom at
//the specified address to address + 1.
void EEPROMWriteInt(int address, int value)
{
  //Decomposition from an 'int' to 2 bytes by using bitshift.
  //two = Most Significant Byte, one = Least Significant Byte
  byte one  = (value & 0xFF);         //LSB
  byte two = ((value >> 8) & 0xFF);   //MSB

  //Write the 2 bytes into the eeprom memory.
  EEPROM.write(address, one);
  EEPROM.write(address + 1, two);

} //END of EEPROMWriteInt()

//                         E E P R O M W r i t e B y t e ( )
//********************************************************************************
//This function will write a byte (8 bits) to the eeprom at
//the specified address.
void EEPROMWriteByte(int address, byte value)
{
  //Decomposition from a long to 1 byte by using bitshift.
  byte one = (value);

  //Write the 1 bytes into the eeprom memory.
  EEPROM.write(address, one);

} //END of EEPROMWriteByte()

//                          E E P R O M R e a d L o n g ( )
//********************************************************************************
long EEPROMReadLong(int address)
{
  //Read the 4 bytes from the eeprom memory.
  long four  = EEPROM.read(address);      //LSB
  long three = EEPROM.read(address + 1);
  long two   = EEPROM.read(address + 2);
  long one   = EEPROM.read(address + 3);  //MSB

  //assemble the bytes into an 'long'
  long r = 0;
  r =     (one   << 24);
  r = r + (two   << 16);
  r = r + (three << 8);
  r = r +  four;

  return r;

} //END of EEPROMReadLong()

//                           E E P R O M R e a d I n t ( )
//********************************************************************************
int EEPROMReadInt(int address)
{
  //Read the 2 bytes from the eeprom memory.
  long one  = EEPROM.read(address);      //LSB
  long two  = EEPROM.read(address + 1);  //MSB

  int r = 0;

  //assemble the bytes into an 'int'
  r = r + (two << 8);
  r = r +  one;

  return r;

} //END of EEPROMReadInt()

//                         E E P R O M R e a d B y t e ( )
//********************************************************************************
byte EEPROMReadByte(int address)
{
  //Read the 1 byte from the eeprom memory.
  byte one = EEPROM.read(address);

  //Return the Byte
  return one;

} //END of EEPROMReadByte()

Also, look into EEPROM.get and EEPROM.put

This is a little project I have been trying to get motivated for for quite a while. When this device is initially installed a pot will be set to a certain position. The voltage is read off in the analog I/P and if it is within one of the 3 states it will light an LED. I want it to save this pin number to the EEPROM. When it is powered off and on again I want that same LED to come on UNLESS the voltage is still within one of the 3 levels.

So if it is the first range pin 22 will be high and that LED will come on. If when it restarts the voltage is below that first range I want it to load the saved pin value and light the LED. There is no need to save it again because nothing has changed and if it is in the same range it also won't do anything but if it is in one of the other two ranges then it will save this new value to the EEPROM address. The EEPROM address contents will only change if the voltage falls within one of the three ranges AND is different to what has been previously saved.

Hope that makes sense.

So like a 'mode' switch. Knob labeled "No Change", "Mode 1", "Mode 2", and "Mode 3". Set the knob and turn on the power. If the knob is set to "No Chnge" then use the previous mode.

How I would do it:

  if (analogValue < 666)
  {
    // No Change
    Mode = EEPROM.read(0);
  }
else if  (analogValue < 800)
  {
    Mode = 1;
  }
else if  (analogValue < 905)
  {
    Mode = 2;
  }
else
  {
    Mode = 3;
  }

  EEPROM.update(0, Mode); // Only writes if different

  switch (Mode)
  {
  case 1:  /* do something */ break;
  case 2:  /* do something */ break;
  case 3:  /* do something */ break;
  }

I would make "Mode" an 'enum' (enumerated type) so the values 1, 2, and 3 would have names instead of meaningless numbers.

enum {Mode1, Mode2, Mode3} Mode;

Excellent. Thank you very much. Once again with a couple of tweaks it works a dream.

Next problem is to make this play on a 1.3" OLED display with some fancy graphics. That will be in a few weeks when they arrive.

WHATCH THIS SPACE

Well the next step is a 0.96" mono OLED display. I have it wired and have some code for it but all it is displaying is what looks like a big Qcode thingy. It is not displaying a number (bitmap) like it should be. Any ideas?

You could tell us how you have it wired, perhaps some more information about it. Also tell us what "some code" is.

Here is what I have so far.

This is now not compiling. I forgot to save the version that gave a funny display.

These are the error messages I am getting now. I have also only included one bitmap.

In file included from /home/jason/snap/arduino/current/Arduino/libraries/Adafruit_SSD1306-master/Adafruit_SSD1306.h:41:0,
from /home/jason/snap/arduino/current/Arduino/PI_GPI_ATRE_OLED1/PI_GPI_ATRE_OLED1.ino:2:
/home/jason/snap/arduino/current/Arduino/libraries/Adafruit-GFX-Library-master/Adafruit_GFX.h:81:8: note: candidate: void Adafruit_GFX::drawBitmap(int16_t, int16_t, const uint8_t*, int16_t, int16_t, uint16_t)
void drawBitmap(int16_t x, int16_t y, const uint8_t bitmap[], int16_t w,
^~~~~~~~~~
/home/jason/snap/arduino/current/Arduino/libraries/Adafruit-GFX-Library-master/Adafruit_GFX.h:81:8: note: no known conversion for argument 3 from 'int()' to 'const uint8_t* {aka const unsigned char*}'
/home/jason/snap/arduino/current/Arduino/libraries/Adafruit-GFX-Library-master/Adafruit_GFX.h:83:8: note: candidate: void Adafruit_GFX::drawBitmap(int16_t, int16_t, const uint8_t*, int16_t, int16_t, uint16_t, uint16_t)
void drawBitmap(int16_t x, int16_t y, const uint8_t bitmap[], int16_t w,
^~~~~~~~~~
/home/jason/snap/arduino/current/Arduino/libraries/Adafruit-GFX-Library-master/Adafruit_GFX.h:83:8: note: candidate expects 7 arguments, 6 provided
/home/jason/snap/arduino/current/Arduino/libraries/Adafruit-GFX-Library-master/Adafruit_GFX.h:85:8: note: candidate: void Adafruit_GFX::drawBitmap(int16_t, int16_t, uint8_t*, int16_t, int16_t, uint16_t)
void drawBitmap(int16_t x, int16_t y, uint8_t bitmap, int16_t w, int16_t h,
^~~~~~~~~~
/home/jason/snap/arduino/current/Arduino/libraries/Adafruit-GFX-Library-master/Adafruit_GFX.h:85:8: note: no known conversion for argument 3 from 'int()' to 'uint8_t
{aka unsigned char*}'
/home/jason/snap/arduino/current/Arduino/libraries/Adafruit-GFX-Library-master/Adafruit_GFX.h:87:8: note: candidate: void Adafruit_GFX::drawBitmap(int16_t, int16_t, uint8_t*, int16_t, int16_t, uint16_t, uint16_t)
void drawBitmap(int16_t x, int16_t y, uint8_t *bitmap, int16_t w, int16_t h,
^~~~~~~~~~
/home/jason/snap/arduino/current/Arduino/libraries/Adafruit-GFX-Library-master/Adafruit_GFX.h:87:8: note: candidate expects 7 arguments, 6 provided
exit status 1
no matching function for call to 'Adafruit_SSD1306::drawBitmap(int, int, int (&)(), int, int, int)'

#include <splash.h>
#include <Adafruit_SSD1306.h>

#include <Adafruit_GFX.h>
#include <Adafruit_MonoOLED.h>
#include <Adafruit_SPITFT.h>
#include <gfxfont.h>
#include <Adafruit_SPITFT_Macros.h>

#include <Wire.h>
#include <EEPROM.h>

#define OLED_RESET 4

int myBitmaps ();
int myBitmap1 ();
int myBitmap2 ();
int myBitmap3 ();
int myBitmap4 ();
int myBitmap5 ();
int myBitmap6 ();
int myBitmap7 ();

Adafruit_SSD1306 display(128, 64, &Wire, OLED_RESET);


void setup()
{

delay(500); //wait to settle
pinMode(22, OUTPUT);
pinMode(23, OUTPUT);
pinMode(24, OUTPUT);
  
    // Read sensor value
  int sensorVal = analogRead (A0);
  int Mode;

  //Save Gear to EEPROM 

if (sensorVal < 666 or sensorVal > 980)
  {
    // No Change
    Mode = EEPROM.read(0); //Load gear from EEPROM
  }
else if  (sensorVal < 800)
  {
    Mode = 1; //4th Gear
  }
else if  (sensorVal < 905)
  {
    Mode = 2; //5th Gear
  }
else if (sensorVal < 980)
  {
    Mode = 3; //6th Gear
  }

  EEPROM.update(0, Mode); // Only writes if different

  switch (Mode)
  {
  case 1:  digitalWrite(22, HIGH); break;
  case 2:  digitalWrite(23, HIGH); break;
  case 3:  digitalWrite(24, HIGH); break;
  }
  
  display.begin(SSD1306_SWITCHCAPVCC, 0x3C);

// Bunch of bitmaps in here


void loop()
{
   
    // Read sensor value
  int sensorVal = analogRead (A0);

  if (sensorVal <= 420)
    display.clearDisplay(); //for Clearing the display
    display.drawBitmap(0, 0, myBitmap1, 128, 64, WHITE); 
    display.display();
    
    else if (sensorVal > 421 and sensorVal <= 540)
      display.clearDisplay(); //for Clearing the display
      display.drawBitmap(0, 0, myBitmap2, 128, 64, WHITE); 
      display.display();
    
   else if (sensorVal > 541 and sensorVal <= 665)
    display.clearDisplay(); //for Clearing the display
    display.drawBitmap(0, 0, myBitmap3, 128, 64, WHITE); 
    display.display();
    
   else if (sensorVal > 666 and sensorVal <= 800)
    display.clearDisplay(); //for Clearing the display
    display.drawBitmap(0, 0, myBitmap4, 128, 64, WHITE); 
    display.display();
    
   else if (sensorVal > 801 and sensorVal <= 905)
    display.clearDisplay(); //for Clearing the display
    display.drawBitmap(0, 0, myBitmap5, 128, 64, WHITE); 
    display.display();
    
   else if (sensorVal > 906 and sensorVal <= 980)
    display.clearDisplay(); //for Clearing the display
    display.drawBitmap(0, 0, myBitmap6, 128, 64, WHITE); 
    display.display();
    
   else if (sensorVal > 981 and sensorVal <= 1023)
    display.clearDisplay(); //for Clearing the display
    display.drawBitmap(0, 0, myBitmap7, 128, 64, WHITE); 
    display.display();
    
   delay (100);

}

Wiring is standard. VCC, GND, SDA, SCL

int myBitmaps ();
int myBitmap1 ();
int myBitmap2 ();
int myBitmap3 ();
int myBitmap4 ();
int myBitmap5 ();
int myBitmap6 ();
int myBitmap7 ();

These are declaring functions that return an integer. You are trying to display them as if they were byte arrays. Neither the functions nor the bitmaps are actually defined in your sketch. It is not clear if you meant these to be bitmaps:

byte myBitmaps[] = { lots of values };
byte myBitmap1 [] = { lots of values };
byte myBitmap2 [] = { lots of values };
byte myBitmap3 [] = { lots of values };
byte myBitmap4 [] = { lots of values };
byte myBitmap5 [] = { lots of values };
byte myBitmap6 [] = { lots of values };
byte myBitmap7 [] = { lots of values };

Or maybe functions that return the address of a bitmap:

byte * myBitmaps ();
byte *  myBitmap1 ();
byte *  myBitmap2 ();
byte *  myBitmap3 ();
byte *  myBitmap4 ();
byte *  myBitmap5 ();
byte *  myBitmap6 ();
byte *  myBitmap7 ();

Thank you for that. They are bitmaps. I was just following what people have done in other posts and tutorials.

windoze_killa:
Wiring is standard. VCC, GND, SDA, SCL

Be aware that some Chinese manufacturers apply those designations to some displays that are in fact, not I2C but are SPI driven. I would be checking it with an I2C bus scanning sketch to see if it is even addressable.

Also, that is not a two sided interconnection description. Which specific pins on the Arduino have you connected those pins to, and which board do you have?

I have done that to get the address. It works fine with one of the samples in the adafruit_ssd1306 libs.