Led Bar on Seeeduino Mega ADK

Hi,
I try led bar demo code on arduino 2009 and it works correctly.

What I have to change if I have to use the mega board?

Thank you

Hi,Dear
We have updated our wiki,and you can run this demo on seeeduino and ADK.
/demo/
#if defined(AVR_ATmega1280) || defined(AVR_ATmega2560) // mega

#define PORT_Data PORTH
#define PORT_Clk PORTH

#define DATA_Pin 8 //DATA IN
#define CLK_Pin 9 //CLK IN

#define BIT_Data 0x01<<5
#define BIT_Clk 0x01<<6

#else //168, 328, something else

#define PORT_Data PORTB
#define PORT_Clk PORTB

#define DATA_Pin 8 //DATA IN
#define CLK_Pin 9 //CLK IN

#define BIT_Data 0x01
#define BIT_Clk 0x02

#endif

#define CmdMode 0x0000 //Work on 8-bit mode
#define ON 0x00ff //8-bit 1 data
#define SHUT 0x0000 //8-bit 0 data

//Send 16_bit data
void send16bitData(unsigned int data)
{
for(unsigned char i=0;i<16;i++)
{
if(data&0x8000)
{
PORT_Data |= BIT_Data;
}
else
{
PORT_Data &=~ BIT_Data;
}

    PORT_Clk ^= BIT_Clk;
    data <<= 1;
}

}

//latch routine for MY9221 data exchange
void latchData(void)
{
PORT_Data &=~ BIT_Data;
delayMicroseconds(10);
for(unsigned char i=0;i<8;i++)
{
PORT_Data ^= BIT_Data;
}
}

//Initializing pins
void setup()
{
pinMode(DATA_Pin,OUTPUT); //Data pin
pinMode(CLK_Pin,OUTPUT); //CLK pin
}

//Send 12 road led brightness data
void sendLED(unsigned int LEDstate)
{
unsigned char i;
for(i=0;i<12;i++)
{
if(LEDstate&0x0001)
send16bitData(ON);
else
send16bitData(SHUT);
// if(i!=11)
LEDstate=LEDstate>>1;
}
}

//If you want turn on the first red led,you can do it like this: sendLED(0x0001);
//The second led: sendLED(0x0002);
void loop()
{
unsigned int i=0x0000;
while(i<=0x03ff)
{
send16bitData(CmdMode); //set first LED Bar mode
sendLED(i); //send first LED Bar data
send16bitData(CmdMode); //set second LED Bar mode,if you do not use two LED Bar work together(connect one by one),you can delete this line.
sendLED(i); //send second LED Bar data,if you do not use two LED Bar work together(connect one by one),you can delete this line.
latchData(); //make it come into effect
i=i*2+1;
delay(100);
}
}

Hello Jacket or anyone who can help. I have followed this thread but still can not get my Grove Bar Graph to respond. Below is my code and questions:

/*

I am using the GROVE 4-DIGIT,7-SEGMENT LED DISPLAYS, they are great, and I am trying to use the Grove Bar Graph in
the same way, but I can’t understand how to do.
I did finally get the demo for Bar Graph to run, but do not know how to change the pins
(I want to use 2 or more bar Graph (8 bits is plenty) for each variable I want to display)

  1. WHERE I CAN CHOOSE WHICH DIGITAL PINS TO USE (AS i DO FOR THE 7-SEGMENT DISPLAYS
    for example, I can do this with the 7-Segment displays
    a) #include “TM1637.h” // This is specific for the Groove (Seeed Studio) 4 digit LED displays
    TM1637 tm1637a(39,43); // Format is (clockpin, datapin) for Display_a (Set Temp (analog pot))
    TM1637 tm1637b(47,51);
    AND
  2. WHERE I CAN SEND EITHER AN INTEGER (FOR EXAMPLE 1-10?) THAT WILL LIGHT UP THE BAR GRAPH TO THAT LEVEL

I am sorry, I am new to writing C code, and know very little about ports and bitwise logic, I know I
I can do more with the Grove Bar Graph (it is very nice) but for now I just need something simple.
I got the demo to run, but I do not understand how to send it the data I want, and I am only using
one bar graph (8 bit mode) at a time and do not need the 16 bit.

Can you help with an example of how to use the bar graphs in some way similar to how I am presently using your
Grove 7-Segment displays?

(By the way so far I have not used the Grove Mega shield, I do have it, but have not needed it, I will later as I 
start to add more sensor etc. 

*/

// BELOW IS MY SAMPLE CODE

// The next sevEral lines below is to setup the LCD and the 4 digit LEDs and adding Bar Graph
// First the LCD (using I2C)
#include <Wire.h>
#include “SerialLCD.h”
#include <SoftwareSerial.h> //

// initialize the library
SerialLCD slcd(11,12); // I am also using the Seeed/Grove serial LCD (very nice)

// This is the setup for the 7-Segment displays, they work great :slight_smile:
#include “TM1637.h” // This is specific for the Groove (Seeed Studio) 4-digit 7-Segment LED displays
TM1637 tm1637a(39,43); // Format is (clockpin, datapin) for Display_a
TM1637 tm1637b(47,51); // Format is (clockpin, datapin) for Display_b

#if defined(ARDUINO) && ARDUINO >= 100 // I don’t seem to need the next few lines, but it came with the code for Groove displays
#define printByte(args) write(args);
#else
#define printByte(args) print(args,BYTE);
#endif

// This is for the Bar Graph (Grove),
#include “LED_Bar.h”
LED_Bar myLED;

// I have gotten one demo to run
// (from your post, viewtopic.php?f=17&t=3845
// but I do not understand how to use the bitlogic, and can not modify the demo in any working way.
// Sorry, I am newbie, and very new to writing C-code at all, can you help with mod to the libray for
// simple implementation for beginners like me? I think it will help other newbies too
// Can you please help me to
// 1) two or more Bar Graph, but separately for separate numbers (8 bit is plenty, I don’t need 16 bit)
// 2) then if I can send a variable (integer 4 or 8 bits is enough) value (like read from my analog pot) to the Bar Graph

//*******************************************************************************************************************

void setup()
{

// the next few lines for Bar Graph, I have look for all examples for the Grove Bar Graph
  pinMode(8,OUTPUT); //Data pin
  pinMode(9,OUTPUT); //CLK pin   

myLED.set_LED_Index(0b000001101010101);
delay(3000);

// First setting up the 3/4 digit, 7-Segment displays (Grove parts using I2C type protocol, not the I2C buss itself)
// for display #1 (a)
tm1637a.init();
// tm1637.set(BRIGHT_TYPICAL);//BRIGHT_TYPICAL = 2,BRIGHT_DARKEST = 0,BRIGHTEST = 7;
tm1637a.set(2); // typical brightnes
// tm1637.set(BRIGHTEST);

// for display #2 (b)
tm1637b.init();
tm1637b.set(2); // typical brightnes=2, min brightness=0, max =4-7 (seems to saturate at 5)

Serial.begin(9600); delay(20);

    } //END SETUP

//*******************************************************************************************************************

//*******************************************************************************************************************
//
void loop() // MAIN PROGRAM LOOP
{ //

// TEST for Bar Graph— but so far I have no idea how to tell it which pins to write too, except the complex bit/port version…
for (int i=0;i<=10;i++)
// set the light range ; 0 means no LED will light,
// “3” means the first 3 LED will be light.
{
myLED.set_LED_Range(i); // Nothing happens here, I see no light on Bar Graph (only demo from above link runs)
delay(1000);
}

//*******************************************************************************************************************
// READ POTENTIOMETER

//************************************************************

int PotVal_raw_a=analogRead(A0); // just reading from an analog potentiometer #1 (on A0)

int PotValScaled_a = map(PotVal_raw_a, 0, 1023, 0, 100);

int PotValScaled_CorseLED_a = PotValScaled_a / 10; // Simply removing 1 digit for the LED display

// Writing data to 7-Segment Grove display #1 (a)

      tm1637a.display(0,(PotValScaled_CorseLED_a / 1000) % 10);  //Display of thousands
      tm1637a.display(1,(PotValScaled_CorseLED_a / 100) % 10);   //Display of hundreds
      tm1637a.point(1);
      tm1637a.display(2,(PotValScaled_CorseLED_a / 10) % 10);    //Display of tens
      tm1637a.display(3,(PotValScaled_CorseLED_a) % 10);         //Display of units

// now reading from secnd analog pot

int PotVal_raw_b=analogRead(A1); // just reading from an analog potentiometer #2 (on A1)

int PotValScaled_b = map(PotVal_raw_b, 0, 1023, 0, 100);

int PotValScaled_CorseLED_b = PotValScaled_b / 10; // Simply removing 1 digit for the LED display
// Writing data to 7-Segment Grove display #2 (b)

      tm1637b.display(0,(PotValScaled_CorseLED_b / 1000) % 10);  //Display of thousands
      tm1637b.display(1,(PotValScaled_CorseLED_b / 100) % 10);   //Display of hundreds
      tm1637b.point(1);
      tm1637b.display(2,(PotValScaled_CorseLED_b / 10) % 10);    //Display of tens
      tm1637b.display(3,(PotValScaled_CorseLED_b) % 10);         //Display of units

//*******************************************************************************************************************
} // end loop